ASP NET. Rad Grid Lose modified values ​​in postback

I am using RadGrid with several columns that can be edited (BatchEdit as excel).

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" AutoGenerateColumns="False"GridLines="Both"OnNeedDataSource="RadGrid1_NeedDataSource" AllowAutomaticInserts="True" AllowAutomaticUpdates="True"AllowAutomaticDeletes="True"> <ItemStyle Wrap="false" /> <MasterTableView TableLayout="Fixed" NoMasterRecordsText="" ShowFooter="true" EditMode="Batch"> ... </MasterTableView> </telerik:RadGrid> 

RadGrid Data Source - ObjectDataSource

 <asp:ObjectDataSource ID="TestSource" runat="server" TypeName="TestClass" SelectMethod="GetAllItems"> <UpdateParameters> <asp:Parameter Name="Name" Type="String" /> <asp:Parameter Name="LastName" Type="String" /> <asp:Parameter Name="BirthData" Type="DateTime" /> </UpdateParameters> </asp:ObjectDataSource> 

In the OnNeedDataSource event, I set the Id of the ObjectDataSource to RadGrid.DataSourceId.

 protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) { RadGrid1.DataSourceID = "TestSource"; } 

On the current page, I have a Send Email button to send rad gird values.

 <asp:Button ID="SendEmail" OnClick="SendEmail_Click" Text="Send" runat="server" /> 

The problem is that in SendEmail_Click the DataSource is NULL. But I want to get a recently modified DataSource.

 protected void SendEmail_Click(object sender, EventArgs e) { RadGrid1.Rebind(); // RadGrid1.Datasource is null } 

How can I solve this problem? Many thanks.

+7
c # telerik
source share
1 answer

DataSource in the GridView not persistent through Postback, so you need to save it somewhere, or you must request it again from your database. Because of this, your data source is null.

This code is suggested in Forum for how to get the source of your grid.

  DataTable dtRecords = new DataTable(); foreach (GridColumn col in grdShippedOrders.Columns) { DataColumn colString = new DataColumn(col.UniqueName); dtRecords.Columns.Add(colString); } foreach (GridDataItem row in grdShippedOrders.Items) // loops through each rows in RadGrid { DataRow dr = dtRecords.NewRow(); foreach (GridColumn col in grdShippedOrders.Columns) //loops through each column in RadGrid dr[col.UniqueName] = row[col.UniqueName].Text; dtRecords.Rows.Add(dr); } return dtRecords; 
+1
source share

All Articles