This is a migrated thread and some comments may be shown as answers.

Get Data Filtered from RadGrid

13 Answers 1171 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 25 Jul 2013, 10:12 AM
HI all is it possible to get data filtered in a radgrid???
What i want to do its getting the data from a radgrid after a filter its apllyed.

Thanks you all.

13 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Jul 2013, 10:19 AM
Hi Carlos,

Please have a look at the below code snippet.We can access the Current filter function and the column being filter using the e.CommandArgument,and the current filter pattern text using e.Item as GridFilteringItem.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource2"
    GridLines="None" AllowPaging="true" AllowFilteringByColumn="true"
    ondatabound="RadGrid1_DataBound1" onitemcommand="RadGrid1_ItemCommand">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" />
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" />
            <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
   {
       if (e.CommandName == RadGrid.FilterCommandName)
       {
           Pair filterPair = (Pair)e.CommandArgument;
           gridMessage1 = "Current Filter function: '" + filterPair.First + "' for column '" + filterPair.Second + "'";
           TextBox filterBox = (e.Item as GridFilteringItem)[filterPair.Second.ToString()].Controls[0] as TextBox;
           gridMessage2 = "<br> Entered pattern for search: " + filterBox.Text;
       }
   }
   private string gridMessage1 = null, gridMessage2 = null;
   
   private void DisplayMessage(string text)
   {
       RadGrid1.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
   }
   protected void RadGrid1_DataBound1(object sender, EventArgs e)
   {
       if (!string.IsNullOrEmpty(gridMessage1))
       {
           DisplayMessage(gridMessage1);
           DisplayMessage(gridMessage2);
       }
   }


Thanks,
Princy
0
Carlos
Top achievements
Rank 1
answered on 25 Jul 2013, 10:31 AM
Thanks,

But what i want not what its beeing asked on the filter, but the Data that was filtered, on one or more filters of the columns.

Best Regards
0
Princy
Top achievements
Rank 2
answered on 25 Jul 2013, 12:50 PM
Hi Carlos,

I misunderstood your requirement.Please try the below code snippet to get the filtered data.

C#:
static bool filter = false;
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (filter == true)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string number= item["ColumnUniqueName1"].Text ;//Access first column text
            string name= item["ColumnUniqueName2"].Text;   //Access second column text
        }
    }      
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
        filter = true;
    }
}

Hope this helps,Please let me know if any concern.

Thanks,
Princy
0
Carlos
Top achievements
Rank 1
answered on 25 Jul 2013, 01:41 PM
hi again and thank you, but what happens if i have autogenerated columns???

Best Regards
0
Princy
Top achievements
Rank 2
answered on 26 Jul 2013, 03:39 AM
Hi Carlos,

To loop through the autogenerated columns,please try the below example code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns) //Loop through the Autogenerated Column
            {
                item[col.UniqueName].ForeColor = Color.Red;               
            }         
        }
    }      
}


Thanks,
Princy
0
Carlos
Top achievements
Rank 1
answered on 26 Jul 2013, 10:26 AM
With This it will run all records and not the filtered ones, i only want to catch the filtered records.

Thanks
0
Princy
Top achievements
Rank 2
answered on 26 Jul 2013, 10:44 AM
Hi Carlos,

Please use it with the code i have given above to check if the filter has occurred.

C#:
static bool filter = false;
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (filter == true)//Checking if filter has occurred
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
            {
                item[col.UniqueName].ForeColor = Color.Red;              
            }         
        }
    }     
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
        filter = true;
    }
}

Thanks,
Princy
0
Carlos
Top achievements
Rank 1
answered on 26 Jul 2013, 01:41 PM
Nothing, still cant get the filtered data only.

Thanks
0
Accepted
Princy
Top achievements
Rank 2
answered on 29 Jul 2013, 03:55 AM
Hi Carlos,

One way to get the filtered data is access each cell of the row.Please try the below code snippet.

C#:
static bool filter = false;
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (filter == true)
    {
        if (e.Item is GridDataItem) //Access data items after the filter
        {
            GridDataItem item = (GridDataItem)e.Item;
            foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
            {                             
                string val1 = item.Cells[2].Text; //Access the first column value
                string val2 = item.Cells[3].Text; //Access the second column value
                string val3 = item.Cells[4].Text; //Access the third column value
                string val4 = item.Cells[5].Text; //Access the fourth column value
            }         
        }
    }      
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
        filter = true;
    }
}

Hope this helps,else please provide the full code you have tried.

Thanks,
Princy
0
Carlos
Top achievements
Rank 1
answered on 30 Jul 2013, 11:03 AM
hi the problem of this code its the GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns
this will only count columns, and even so its detecting the row filtered its multiplicating by the number of columns in existence.

So the problem its how to count rows?

Best regards, and many thanks
0
Accepted
Princy
Top achievements
Rank 2
answered on 30 Jul 2013, 12:38 PM
Hi Carlos,

I apologize for adding the foreach loop in the code which is not required.Then to get the count of the rows you can either get it in the PreRender event,or set a static variable and increment it.Please try the below code snippet.

C#:
static bool filter = false;
static int numberofrows;
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (filter == true) //Access after the filter
    {
        if (e.Item is GridDataItem)
        {
        GridDataItem item = (GridDataItem)e.Item;                                         
        string val1 = item.Cells[2].Text;//Access the first column value
        string val2 = item.Cells[3].Text;
        string val3 = item.Cells[4].Text;
        string val4 = item.Cells[5].Text;
        numberofrows++;  // Get the count of filtered data  
        }       
    }     
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
     filter = true;
    }
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (filter)
    {
     // Another way to get count of filtered data
     int count = RadGrid1.MasterTableView.Items.Count;       
    }
}

Thanks,
Princy
0
Carlos
Top achievements
Rank 1
answered on 31 Jul 2013, 09:33 AM
Hi again,

but without an loop it will pass trough rows???,

Thanks
0
Accepted
Princy
Top achievements
Rank 2
answered on 31 Jul 2013, 09:44 AM
Hi Carlos,

I have tried the code and it works fine at my end.Please put breakpoints and see if you are able to get all the values.Without using for loop,i was able to get all the filtered data,and the correct count of filtered data.Please check the attached screenshot.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (filter == true) //Access after the filter
    {
        if (e.Item is GridDataItem)
        {
                GridDataItem item = (GridDataItem)e.Item;   
                                           
                string val1 = item.Cells[2].Text;//Access the first column value
                string val2 = item.Cells[3].Text;
                string val3 = item.Cells[4].Text;
                string val4 = item.Cells[5].Text;
               
               //Display the filtered rows
                Response.Write("<br>");
                Response.Write(val1);
                Response.Write("<br>");
                Response.Write(val2);
                Response.Write("<br>");
                Response.Write(val3);
                Response.Write("<br>");
                Response.Write(val4);
        }       
    }     
}


Please let me know if any concern.

Thanks,
Princy
Tags
Grid
Asked by
Carlos
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Carlos
Top achievements
Rank 1
Share this question
or