C# Telerik 网格数据绑定不重新加载

C# Telerik Grid data binding not reloading

我想在 telerik 网格中动态绑定数据。 最初我使用下面的代码。

protected void radGridItemList_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            var gridItemList = sender as RadGrid;
            var repoItemMaster = CreateIItemMasterRepository();
            var ItemList = repoItemMaster.GetAll();
            gridItemList.DataSource = ItemList;
}

现在我想用过滤后的 ItemNumbers 重新加载 gridItemList(例如 select ItemNumber from ItemMaster where ItemNumber LIKE 'a%')并再次将新数据源绑定到网格。

我做了类似的事情,但它不起作用。

private void LoadItemListByItemNumber(string ItemNumber)
        {
            var gridItemList = radcmbItemNumber.Items[0].FindControl("radGridItemList") as RadGrid;
            string whereCondition = "a.ItemNumber LIKE '"+ ItemNumber + "%'";

            var repoItemMaster = CreateIItemMasterRepository();        
            List<ItemMaster> itemList = repoItemMaster.FindbyItemNumber(ItemNumber, whereCondition);
            gridItemList.DataSource = itemList;
        }       

有什么建议吗?

在使用高级数据绑定时,您可以使用内置的 RadGrid 功能来过滤由自定义对象组成的数据源(RadGrid1_OnNeedDataSource())。

要在 RadGrid 中执行此操作,只需通过设置 AllowFilteringByColumn="True" 启用过滤,RadGrid 将自动过滤您的数据源。

您也可以通过设置当前过滤器表达式并在 RadGrid 上调用 rebind() 来在外部执行此操作。

ASPX:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Button ID="Button1" runat="server" Text="Filter by 1" OnClick="Button1_Click" />
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AllowFilteringByColumn="True" CellSpacing="-1" GridLines="Both" EnableLinqExpressions="false" AllowPaging="True"></telerik:RadGrid>

C#:

 // <summary>
// Demo cusotm object
// </summary>
public class MyObject
{
    public int ItemNumber { get; set; }
    public string ItemName { get; set; }
}

protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    //  Bind RadGrid         
    RadGrid1.DataSource = this.GetData();
}

// <summary>
// Returns a Demo custom object DataSource
// </summary>
// <returns></returns>
private List<MyObject> GetData()
{
    List<MyObject> data = new List<MyObject>();
    for (int i = 0; (i <= 100); i++)
    {
        var item = new MyObject();
        item.ItemNumber = i;
        item.ItemName = ("Item # " + i);
        data.Add(item);
    }
    return data;
}

protected void Button1_Click(object sender, EventArgs e)
{
    RadGrid1.MasterTableView.FilterExpression = "([ItemName] LIKE \'%1%\')";
    RadGrid1.Rebind();
}

一些要查看的链接:

Advanced DataBinding

Advanced Data Binding Tips

Manual Filtering

.aspx

<div class="col-lg-8 qb-col">
                                                    <telerik:RadComboBox ID="radcmbItemNumber" runat="server"
                                                        MarkFirstMatch="True" 
                                                        AllowCustomText="true"
                                                        OnClientDropDownOpening="radcmbItemNumberOnClientDropDownOpening"                                                      
                                                        OnClientKeyPressing="radcmbItemNumberOnKeyPress"                                                                                                                
                                                        OnTextChanged="radcmbItemNumber_TextChanged"                                                      
                                                        Width="95%" 
                                                        CssClass="qb-input-combo"
                                                        ExpandAnimation-Type="None">
                                                        <ItemTemplate>
                                                            <telerik:RadGrid ID="radGridItemList" runat="server" CssClass="GridStyle1"
                                                                AutoGenerateColumns="false" AllowPaging="true"
                                                                AllowSorting="true" AllowFilteringByColumn="true"
                                                                OnNeedDataSource="radGridItemList_NeedDataSource"
                                                                EnableLinqExpressions="false">
                                                                <GroupingSettings CaseSensitive="false" />
                                                                <PagerStyle Mode="NextPrevAndNumeric" />
                                                                <MasterTableView DataKeyNames="ItemNumber" ClientDataKeyNames="ItemNumber">
                                                                    <Columns>
                                                                        <telerik:GridBoundColumn UniqueName="ItemNumber" DataField="ItemNumber"
                                                                            HeaderText="Item Number" AutoPostBackOnFilter="true"
                                                                            FilterControlWidth="100px">
                                                                            <HeaderStyle Width="150px" />
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn UniqueName="RevisionNumber" DataField="RevisionNumber"
                                                                            HeaderText="Revision Number" AutoPostBackOnFilter="true"
                                                                            FilterControlWidth="100px">
                                                                            <HeaderStyle Width="150px" />
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn UniqueName="Description" DataField="Description"
                                                                            HeaderText="Description" AutoPostBackOnFilter="true"
                                                                            FilterControlWidth="100px">
                                                                            <HeaderStyle Width="150px" />
                                                                        </telerik:GridBoundColumn>

                                                                    </Columns>
                                                                </MasterTableView>
                                                                <ClientSettings>
                                                                    <ClientEvents OnRowClick="radGridItemListOnRowClicked" />
                                                                    <Scrolling AllowScroll="true" UseStaticHeaders="true" />
                                                                </ClientSettings>
                                                            </telerik:RadGrid>
                                                        </ItemTemplate>
                                                        <Items>
                                                            <telerik:RadComboBoxItem runat="server" Text=" "></telerik:RadComboBoxItem>
                                                        </Items>
                                                    </telerik:RadComboBox>
                                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
                                                                                        ControlToValidate="radcmbItemNumber" ErrorMessage="!"
                                                                                        ForeColor="Red" 
                                                                                        SetFocusOnError="true" />
                                                </div>

我使用了一个组合,当它点击时,我需要在网格中加载组合框中键入内容的搜索结果。 这是我的 .cs

 protected void radcmbItemNumber_TextChanged(object sender, EventArgs e)
        {
            string textToSearch = radcmbItemNumber.Text.ToString();
            var gridItemList = radcmbItemNumber.Items[0].FindControl("radGridItemList") as RadGrid;
            if (!string.IsNullOrEmpty(textToSearch))
                gridItemList.MasterTableView.FilterExpression = "([ItemNumber] LIKE \'%" + textToSearch + "%\')";

            gridItemList.MasterTableView.Rebind();
}

所以这个文本更改非常有效,但在我的网格中,新的搜索结果在我转到第二页之前不会重新加载。希望这能让 sense.Let 我知道您是否需要更多信息。谢谢