Telerik:正在尝试搜索字符串并筛选包含模板列的radgrid

本文关键字:包含模 筛选 串并 radgrid 字符串 字符 搜索 Telerik | 更新日期: 2023-09-27 18:24:43

我在尝试实现字符串搜索功能以指出用户搜索的网格行时遇到了令人困惑的异常。以下是我使用的代码片段,以前这段代码与网格绑定列完美地结合在一起,现在它正在抛出在我的类型"DataRowView"中不存在属性或字段"ObjSN"。

ASPX-

<telerik:RadComboBox ID="rcbFieldName" runat="server" Label="Pencarian: " LabelCssClass="filterLabel">
    <Items>
        <telerik:RadComboBoxItem Text="Container No." Value="ObjSN" />
        <telerik:RadComboBoxItem Text="Seal No." Value="TrxSeal" />                                      
    </Items>
</telerik:RadComboBox>
<telerik:RadTextBox ID="txtSearch" runat="server" OnTextChanged="txtSearch_TextChanged" />
<telerik:RadButton ID="btnSearchGrid" runat="server" Text="Search" ToolTip="Search Record" />
<telerik:RadButton ID="btnShowAllItem" runat="server" Text="Reset" ToolTip="Reset View" OnClick="btnShowAll_Click" />
<telerik:RadGrid ID="RadGrid1" 
    GridLines="None" 
    AutoGenerateColumns="false" 
    PageSize="10"
    AllowPaging="true" 
    AllowSorting="true" 
    runat="server" 
    DataSourceID="MasterViewDataSource" 
    OnItemDataBound="OnItemDataBoundHandler" 
    OnItemCommand="RadGrid1_ItemCommand"
    AllowAutomaticInserts="True" 
    AllowAutomaticDeletes="True"
    ShowStatusBar="true">
    <PagerStyle Mode="NextPrevAndNumeric" />
        <MasterTableView ShowFooter="false" DataKeyNames="TrxId" EditMode="InPlace" CommandItemDisplay="TopAndBottom">
            <Columns>
                <telerik:GridBoundColumn DataField="TrxId" HeaderText="Trx No." ReadOnly="true" DataFormatString="{0:0000}" UniqueName="TrxId"/>
                    <telerik:GridTemplateColumn UniqueName="ObjId" HeaderText="Container No."
                        SortExpression="ObjSN">
                        <FooterTemplate>Template footer</FooterTemplate>
                        <FooterStyle VerticalAlign="Middle" HorizontalAlign="Center" />
                            <ItemTemplate>
                                <%#DataBinder.Eval(Container.DataItem, "ObjSN")%>
                            </ItemTemplate>
                        <EditItemTemplate>
                            <telerik:RadComboBox runat="server" ID="RadComboBox1" EnableLoadOnDemand="True" DataTextField="ObjSN" 
                                OnItemsRequested="RadComboBox1_ItemsRequested" OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged" 
                                DataValueField="ObjId" AutoPostBack="true" HighlightTemplatedItems="true" Height="140px" Width="220px" 
                                DropDownWidth="300px">
                                    <HeaderTemplate>
                                        <ul>
                                            <li class="col1">Container No.</li>
                                            <li class="col2">Container Type</li>
                                       </ul>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <ul>
                                            <li class="col1">
                                                <%# DataBinder.Eval(Container, "Text")%>
                                            </li>
                                            <li class="col2">
                                                <%# DataBinder.Eval(Container, "Attributes['ObjType']")%>
                                            </li>
                                       </ul>
                                  </ItemTemplate>
                             </telerik:RadComboBox>
                        </EditItemTemplate>
                   </telerik:GridTemplateColumn>
             </Columns>
         </MasterTableView>
    </telerik:RadGrid>
<asp:SqlDataSource ID="MasterViewDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ZEUS %>"
     SelectCommand="SELECT TransactsData.TrxId,TransactsData.ObjId, 
                           ObjectsData.ObjSN, ObjectsData.ObjType  
                    FROM   TransactsData INNER JOIN              
                           ObjectsData ON TransactsData.ObjId = ObjectsData.ObjId" />

C#

protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        RadTextBox txt = sender as RadTextBox;
        RadComboBox list = (RadComboBox)((txt.NamingContainer).FindControl("rcbFieldName"));
        string option;
        if (list.SelectedValue == "TrxId")
            {
                option = " = ";
            }
            else
            {
                option = " LIKE ";
            }
        string filterExpression;
        if (option == " = ")
            {
                filterExpression = "(" + list.SelectedValue + option + txt.Text + ")";
            }
            else
            {
                filterExpression = "(" + list.SelectedValue + option + "'%" + txt.Text + "%'" + ")";
            }
        RadGrid1.MasterTableView.FilterExpression = filterExpression;
        RadGrid1.MasterTableView.Rebind();
        RadAjaxManager1.FocusControl(txtSearch.ID);
    }
    protected void btnShowAll_Click(object sender, EventArgs e)
    {
        RadGrid1.MasterTableView.FilterExpression = "";
        RadGrid1.MasterTableView.Rebind();
        txtSearch.Text = string.Empty;
        RadAjaxManager1.FocusControl(txtSearch.ID);
    }

提前谢谢。

Telerik:正在尝试搜索字符串并筛选包含模板列的radgrid

我似乎忘记将EnableLinqExpressions设置为False,从而导致SQL查询变形。无论如何,此方法仅适用于网格边界列,不适用于模板列。