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

binding

3 Answers 91 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ehsan
Top achievements
Rank 1
ehsan asked on 10 Sep 2009, 02:41 PM
hi
when i use sqldatasource to bind every thing is ok.
but when ia am using this code to bind my data grid to data source in(q2 2009 sp1)
 protected void btn_Filter_Click(object sender, EventArgs e) 
    { 
        
        ds_Reports.sp_DataTable tbl = new ds_Reports.sp_DataTable(); 
        ds_ReportsTableAdapters.ta_Select ta = new ds_ReportsTableAdapters.ta_Select(); 
        int c = ta.Fill(tbl); 
        RadGrid1.DataSource = tbl
        RadGrid1.DataBind(); 
         
    } 
after click on btn_filter my datagrid fills by data,
but when i change order of columns or try to group my data table by drag & drop columns to grouping area,
my table claened and need to click  filter button again.
how i can solve my problem?


3 Answers, 1 is accepted

Sort by
0
Accepted
Suresh K
Top achievements
Rank 1
answered on 11 Sep 2009, 06:51 AM

hi.,
can u try this code.,

protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
ds_Reports.sp_DataTable tbl = new ds_Reports.sp_DataTable(); 
        ds_ReportsTableAdapters.ta_Select ta = new ds_ReportsTableAdapters.ta_Select(); 
        int c = ta.Fill(tbl); 
        RadGrid1.DataSource = tbl
       

    }
No Need to write  RadGrid1.DataBind();  in NeedDataSource Event
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Sep 2009, 07:19 AM
Hi Eshan,

When you perform filter operation the Grid is populated in the Filter button's click event. After that when you perform any operation that causes a PostBack the Grid will be empty since its DataSource is not set in the succeeding PostBacks.

   So in order to retain the filtered result while Reordering/Grouping store the filtered result to a ViewState in the button click event and then in the NeedDataSource event populate the Grid again from the ViewState.  NeedDataSource  event will be fired each and every time a Grid operation is performed. Here is a Sample code:

ASPX:
 
   <telerik:RadGrid ID="RadGrid1" runat="server"  AllowPaging="true" PageSize="5"   ShowGroupPanel="true" OnNeedDataSource="RadGrid1_NeedDataSource"
            <MasterTableView CommandItemDisplay="Top"
            </MasterTableView> 
            <ClientSettings AllowDragToGroup="true" AllowColumnsReorder="true" > 
            </ClientSettings> 
        </telerik:RadGrid> 
 
  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Filter" /> 


CS:
 
  protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        if (ViewState["GridData"] != null
        { 
            RadGrid1.DataSource = ViewState["GridData"]; 
        } 
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
         string connectionstring = "Data Source=MyData; Initial Catalog=NorthWind;User ID=***; Password=*****"
         SqlConnection conn = new SqlConnection(connectionstring); 
         SqlCommand cmd = new SqlCommand(); 
             conn.Open(); 
            SqlDataAdapter adp = new SqlDataAdapter("select * from  Products", conn); 
            DataTable dt = new DataTable(); 
            adp.Fill(dt); 
            ViewState["GridData"] = dt; 
 
            if (ViewState["GridData"] != null
            { 
                RadGrid1.DataSource = ViewState["GridData"]; 
            } 
            RadGrid1.DataBind(); 
            conn.Close();  
    } 


Best Regards
Princy

0
ehsan
Top achievements
Rank 1
answered on 12 Sep 2009, 11:51 AM
thank u suresh and thank u princy nice solutions,
my problem solved .
thank u , thank u!
Tags
Grid
Asked by
ehsan
Top achievements
Rank 1
Answers by
Suresh K
Top achievements
Rank 1
Princy
Top achievements
Rank 2
ehsan
Top achievements
Rank 1
Share this question
or