如何从telerik RadGrid中搜索数据?

时间:2016-06-28 15:30:07

标签: c# search telerik telerik-grid radgrid

我是C#和Telerik的新手。

我有一个带有MasterTableView的radgrid和一些列。我正在使用存储过程来插入,更新和删除数据。

我想要的是通过我的表搜索RadGrid数据。我怎么能这样做?

<h6>Filtros</h6>
<table class="table table-condensed" style="margin-top: 25px; padding-top: 10px;">
    <tr>
        <td style="width: 80px">Filtro
        </td>
        <td style="width: 30%;">
            <telerik:RadTextBox ID="txtFiltro" runat="server" EmptyMessage="insert name, department or teacher group" Width="90%"></telerik:RadTextBox>
        </td>
        <td>
            <telerik:RadButton ID="lbSearch"
                Visible="true"
                runat="server"
                Text="pesquisar"
                CausesValidation="false"
                Style="background-color: #FFFFFF">
                <Icon PrimaryIconCssClass="icon-search" PrimaryIconLeft="5px" PrimaryIconTop="4px" />
            </telerik:RadButton>
        </td>
    </tr>
</table>   

<telerik:RadGrid ID="lstProfessores" runat="server"  AutoGenerateColumns="False" CellSpacing="0"GridLines="None"OnInsertCommand="lstProfessores_InsertCommand"OnUpdateCommand="lstDocentes_UpdateCommand" >

    <MasterTableView  CommandItemDisplay="Top">
        <Columns>
            <telerik:GridButtonColumn ButtonCssClass="icon-pencil" UniqueName="Edit" CommandName="Edit"> 
                <HeaderStyle Width="30px" />
            </telerik:GridButtonColumn>       

            <telerik:GridButtonColumn ButtonCssClass="icon-trash" UniqueName="Delete" CommandName="Delete" ConfirmDialogType="RadWindow" ConfirmText="Continuar com a remoção?"> 
                <HeaderStyle Width="30px" />
            </telerik:GridButtonColumn>       

            <telerik:GridBoundColumn HeaderText="IdTeacher" DataField="IdTeacher" UniqueName="IdTeacher" DataType="System.Int32" SortExpression="IdTeacher">
                    <ColumnValidationSettings>
                    <ModelErrorMessage Text=""></ModelErrorMessage>
                    </ColumnValidationSettings>
            </telerik:GridBoundColumn>

            <telerik:GridBoundColumn HeaderText="Name" DataField="Name" UniqueName="Name" SortExpression="Name">
                    <ColumnValidationSettings>
                    <ModelErrorMessage Text=""></ModelErrorMessage>
                    </ColumnValidationSettings>
            </telerik:GridBoundColumn>

            <telerik:GridBoundColumn DataField="Department" HeaderText="Departamento" SortExpression="Departamento" UniqueName="Departamento">
                    <ColumnValidationSettings>
                        <ModelErrorMessage Text="" />
                    </ColumnValidationSettings>
            </telerik:GridBoundColumn>

            <telerik:GridBoundColumn DataField="TeacherGroup" HeaderText="TeacherGroup" SortExpression="TeacherGroup" UniqueName="TeacherGroup">
                    <ColumnValidationSettings>
                        <ModelErrorMessage Text="" />
                    </ColumnValidationSettings>
            </telerik:GridBoundColumn>
            <telerik:GridEditCommandColumn />
        </Columns>

    </MasterTableView>
</telerik:RadGrid>

2 个答案:

答案 0 :(得分:1)

如何通过视图/表格模型填充网格?如果是这样,那么您只需要点击数据绑定事件并过滤结果。

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    var dataSource = myDataSource.Where(s => s.ColumnToSearch == mySearchTextBoxValue);
    RadGrid1.DataSource = dataSource;
}

如果使用存储过程填充网格,则会更加困难。

答案 1 :(得分:0)

除了Seano666建议的方法之外,您还可以尝试将RadGrid绑定到数据源控件,例如:如果您的站点连接到SQL Server数据库,则SQLDataSource控制。该控件允许您指定参数,以便您可以将它们传递到存储过程以进行数据处理。在您的情况下,根据姓名,部门或教师组进行过滤。

<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:Your Connection String %>" ProviderName="System.Data.SqlClient" SelectCommand="Your Stored Procedure" SelectCommandType="StoredProcedure" runat="server">

        

<telerik:RadGrid ID="lstProfessores" runat="server" AutoGenerateColumns="False" CellSpacing="0"GridLines="None" OnInsertCommand="lstProfessores_InsertCommand"OnUpdateCommand="lstDocentes_UpdateCommand" DataSourceId="SqlDataSource1">
//...
</telerk:RadGrid>

但理想情况下,通过实施RadGrid的OnNeedDataSource事件,从长远来看,可以为您提供更多控制和灵活性。

参考文献:

http://www.telerik.com/forums/telerik-radgrid-datasource-with-stored-procedure
http://forums.asp.net/t/1019588.aspx?Passing+parameters+to+Sqldatasource+stored+procedure https://msdn.microsoft.com/en-us/library/z72eefad.aspx