如何从 RadGrid 单元格中解析字符串值以用于 RadGrid PreRender 事件?

How to parse a string value out of a RadGrid cell for use in RadGrid PreRender event?

我有一个 RadGrid:

 <telerik:RadGrid ID="rgvBoards" runat="server" AllowPaging="True" AllowSorting="True" GroupPanelPosition="Top" 
                            OnPreRender="rgvBoards_PreRender" DataSourceID="odsBoard">
                            <GroupingSettings CaseSensitive="false"></GroupingSettings>
                            <MasterTableView AutoGenerateColumns="False" DataKeyNames="BoardID" DataSourceID="odsBoard" AllowFilteringByColumn="True" ShowFooter="True">
                                <Columns>                                                                           
                                    <telerik:GridButtonColumn UniqueName="InActivate" ButtonType="ImageButton" ImageUrl="../images/cancelDelete.gif" 
                                        CommandName="InActivate" Display="True">
                                    </telerik:GridButtonColumn>
                                    <telerik:GridButtonColumn UniqueName="Activate" ButtonType="ImageButton" ImageUrl="../images/greenCheck.png"
                                        CommandName="InActivate" Display="True">
                                    </telerik:GridButtonColumn>                                            
                                    <telerik:GridBoundColumn DataField="BoardStatus" AllowFiltering="false" HeaderText="Status" 
                                        UniqueName="BoardStatus">
                                    </telerik:GridBoundColumn>
                                </Columns>
                                <PagerStyle AlwaysVisible="true" PageSizes="10;20;30;999" />
                            </MasterTableView>
                        </telerik:RadGrid>

目标是每行只显示一个按钮。因此,当状态标记为活动 ("A") 时,将显示 "InActivate" 按钮。

我一直在尝试使用 preRender 事件来实现这一点。我知道我的 preRender 事件正在被读取并对网格产生影响,因为无需应用逻辑来读取 BoardStatus 列,我可以控制显示哪些按钮:

    Protected Sub rgvBoards_PreRender(ByVal sender As Object, ByVal e As EventArgs)
            rgvBoards.MasterTableView.GetColumn("InActivate").Display = False
            rgvBoards.MasterTableView.GetColumn("Activate").Display = False
    End Sub

我已经尝试了很多来自 telerik 论坛及其文档的建议,例如:

    Protected Sub rgvBoards_PreRender(ByVal sender As Object, ByVal e As EventArgs)
        Dim dataItem As GridDataItem = TryCast(e.Item, GridDataItem)
        Dim itemValue As String = dataItem("BoardStatus").Text

        If itemValue.Equals("A") Then
            rgvBoards.MasterTableView.GetColumn("InActivate").Display = False
        ElseIf itemValue.Equals("I") Then
            rgvBoards.MasterTableView.GetColumn("Activate").Display = False
        End If
    End Sub

这会产生 'item' 不是 'System.EventArgs' 的成员的问题。

读取 RadGrid 单元格并将其放入字符串变量的正确方法是什么?

正确的方法是使用 'OnNeedDataSource' 事件。
HTML:

<telerik:RadGrid ID="rgvBoards" runat="server" AllowPaging="True" AllowSorting="True" GroupPanelPosition="Top" 
                             OnNeedDataSource="rgvBoards_NeedDataSource" DataSourceID="odsBoard">
                            <GroupingSettings CaseSensitive="false"></GroupingSettings>
                            <MasterTableView AutoGenerateColumns="False" DataKeyNames="BoardID" DataSourceID="odsBoard" AllowFilteringByColumn="True" ShowFooter="True">
                                <Columns>                                    
                                    <telerik:GridTemplateColumn UniqueName="ImageButtonColumn">
                                        <ItemTemplate>

                                            <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../images/cancelDelete.gif"
                                            CommandName="InActivate" Visible='<%# Eval("BoardStatus").Equals("A")%>'
                                            AlternateText="InActivate" />

                                            <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="../images/greenCheck.png"
                                            CommandName="Activate" Visible='<%# Eval("BoardStatus").Equals("I")%>'
                                            AlternateText="Activate" />

                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridBoundColumn DataField="BoardStatus" AllowFiltering="false" HeaderText="Status" 
                                        UniqueName="BoardStatus" DataType="System.String">
                                    </telerik:GridBoundColumn>
                                </Columns>
                                <PagerStyle AlwaysVisible="true" PageSizes="10;20;30;999" />
                            </MasterTableView>
                        </telerik:RadGrid>                 

连同后端 (VB):

Protected Sub rgvBoards_NeedDataSource(sender As Object, e As GridNeedDataSourceEventArgs)
        rgvBoards.DataSource = GetGridSource()
    End Sub

    Private Function GetGridSource() As DataTable
        Dim dataTable As New DataTable()

        column = New DataColumn()
        column.DataType = Type.[GetType]("System.String")
        column.ColumnName = "BoardStatus"
        dataTable.Columns.Add(column)

        Dim PrimaryKeyColumns As DataColumn() = New DataColumn(0) {}
        PrimaryKeyColumns(0) = dataTable.Columns("BoardID")
        dataTable.PrimaryKey = PrimaryKeyColumns

        Return dataTable
    End Function

允许我使用面板状态作为决定因素让 RadGrid 呈现正确的图像按钮。

来源:http://www.telerik.com/help/aspnet-ajax/grid-advanced-data-binding.html