【问题标题】:Sorting radgrid gridtemplatecolumns with changing or dynamic column value使用变化或动态列值对 radgrid gridtemplatecolumns 进行排序
【发布时间】:2014-01-09 21:28:33
【问题描述】:

通常对RadGrid 中的列进行排序,我在GridTemplateColumn 中使用sortexpressiontext。但现在不同了;这里我有一个RadGrid 有几列,它们都是GridTemplateColumns。我有一个绑定RadGrid 的集合。

<telerik:GridTemplateColumn ItemStyle-BorderWidth="0" ItemStyle-HorizontalAlign="Left" 
    HeaderStyle-Font-Bold="true" UniqueName="CustomerSupplierName" ShowSortIcon="true">
    <ItemTemplate>
        <asp:Label ID="lblSupplierName" runat="server" />
        <%-- Text='<%# Eval("SupplierNameText")%>'/>--%>
    </ItemTemplate>
</telerik:GridTemplateColumn>

这是其中一列。现在在数据绑定函数中,我填充了网格。我的页面上有一个单选按钮列表,完全远离这个网格。根据其选择的索引,我更改要填充到此特定列 (UniqueName="CustomerSupplierName") 中的值。像这样

在 ItemDataBound 中:

If rblCustomersSuppliers.SelectedIndex = 0 Then
    Dim lblSupplierName As Label = e.Item.FindControl("lblSupplierName")
    lblSupplierName.Text = cont.SupplierNameText
Else
    Dim lblSupplierName As Label = e.Item.FindControl("lblSupplierName")
    lblSupplierName.Text = cont.CustomerOrganization
End If

所以我要么根据选择绑定供应商名称或客户组织。现在我需要这个列是可排序的。我怎么做?如果您需要更多信息,请询问。谢谢

编辑: 项目数据绑定

 If TypeOf e.Item Is GridHeaderItem Then
 If rblCustomersSuppliers.SelectedIndex = 0 Then
       rgContractHistory.MasterTableView.GetColumn("CustomerName").Visible = False
            rgContractHistory.MasterTableView.GetColumn("SupplierName").Visible = True
 Else
   item("CustomerName").Text = "Customer"
            rgContractHistory.MasterTableView.GetColumn("CustomerName").Visible = True
            rgContractHistory.MasterTableView.GetColumn("SupplierName").Visible = False
  End If
End If

【问题讨论】:

    标签: sorting telerik expression radgrid


    【解决方案1】:

    首先,UniqueName 属性不应用于排序,因为它旨在成为列的唯一标识符;相反,DataField 应该定义要排序的列(来自数据库)。

    因此,您要考虑查看的事件处理程序是DataBinding,或者,如果使用高级数据绑定,则为NeedDataSource。从这里可以更改分配给列的DataField

    获取列的示例代码:

    Dim columnToChange As GridTemplateColumn = CType(grdUsers.MasterTableView.Columns.FindByUniqueNameSafe("CustomerSupplierName"), GridTemplateColumn)
    If rblCustomersSuppliers.SelectedIndex = 0 Then
        columnToChange.DataField = "SupplierNameText"
    Else
        columnToChange.DataField = "CustomerOrganization"
    End If
    

    另一种方法是使用两个单独的列,根据当前选择的RadioButton 隐藏和显示它们,这将需要更改合理数量的代码,因为您需要清除被隐藏列的过滤器文本,并可能将其移动到显示的列中。 (对不起,我相信这会涉及很多,我真的没有时间去举个例子)

    以上代码均未经过测试/语法检查,但希望能给您合理的指示,让您知道下一步该往哪里看。

    【讨论】:

    • 谢谢。我接受了你建议的替代想法。因此,根据单选按钮的选择,显示一列而隐藏其他列。但它们仍然不可排序。我对两列都有不同的唯一名称。但是对于排序表达式,我给出了数据库中的数据字段。出了什么问题?查看我编辑的问题
    • 每当我进行过滤时,我都会使用以下内容:&lt;telerik:GridBoundColumn DataField="SupplierNameText" UniqueName="SupplierName" HeaderText="Supplier Name" AllowFiltering="true" AllowSorting="true" FilterControlWidth="100%" /&gt; 我会使用Display 属性而不是Visible,因为Visible 设置为false该字段将不会被填充。 DataField 属性应该包含列名,您不需要填充 SortExpression,因为这应该会自动匹配分配的 DataField
    猜你喜欢
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多