Pedro Cunha
Pedro Cunha

Reputation: 431

How to search data from a telerik RadGrid?

I'am new in C# and Telerik.

I have a radgrid with a MasterTableView with some columns. I am using stored procedures to insert, update and delete data.

What I want is to search RadGrid data through my table. How can I do this?

<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>

Upvotes: 0

Views: 1531

Answers (2)

woodykiddy
woodykiddy

Reputation: 6455

In addition to the approach suggested by Seano666, you can also try binding your RadGrid to a data source control, e.g. SQLDataSource control, if your site is connecting to a SQL Server database. The control allows you to specify the parameters so that you can pass them into your stored procedure for data processing. In your case, filter based on the name, department or teacher group.

<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>

But ideally, by implementing OnNeedDataSource event of RadGrid gives you more control and flexibility in the long run.

References:

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

Upvotes: 0

Seano666
Seano666

Reputation: 2238

How are you populating the grid, through a view/table model? If so, then you just need to tap into the data binding event and filter your results.

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

If you are populating the grid with a stored procedure, then it will be more difficult.

Upvotes: 1

Related Questions