I have a GridView setup which I've tried to bind to DataTable .. I then make some changes to the GridView's values through the webpage .. Then I go behind the code of a button click, and see that the DataTable still has the old values in it, even though visually the new values are shown on the webpage (because the user typed them in) ...
ASPX Markup code for the GridView:
<asp:gridview ID="ESBAndTSRValuesInputGridView" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="AwardID" HeaderText="Award ID" Visible="false" />
<asp:BoundField DataField="AwardName" HeaderText="Award Name" />
<asp:TemplateField HeaderText="ESBValue">
<ItemTemplate>
<asp:TextBox ID="TextBox1" Text='<%# Eval("ESBValue") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TSRValue">
<ItemTemplate>
<asp:TextBox ID="TextBox2" Text='<%# Eval("TSRValue") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
Initializing GridView data from code-behind of a button click:
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("AwardID", typeof(string)));
dt.Columns.Add(new DataColumn("AwardName", typeof(string)));
dt.Columns.Add(new DataColumn("ESBValue", typeof(string)));
dt.Columns.Add(new DataColumn("TSRValue", typeof(string)));
DataRow[] PSPAwards = dtAwards.Select("AWARDTYPE = 'PSP'");
foreach (DataRow dr in PSPAwards)
{
dt.Rows.Add(dr["AWARDID"].ToString(), dr["AWARDNAME"].ToString(), "0", "100");
}
ViewState["ESBAndTSRValuesDataTable"] = dt;
ESBAndTSRValuesInputGridView.DataSource = dt;
ESBAndTSRValuesInputGridView.DataBind();
Trying to get new data on a button click:
DataTable dt = (DataTable)ViewState["ESBAndTSRValuesDataTable"];
dt.AcceptChanges();
dt = ESBAndTSRValuesInputGridView.DataSource as DataTable;
DataSet ds = ESBAndTSRValuesInputGridView.DataSource as DataSet;
In the above code, you can see that I initialize the rows with default values 0 and 100 .. This is what I see during a debug event after making changes to these values from the webpage ..
How can I make the GridView automatically persist all changes done to it to its linked DataTable ?
I have also tried using Session instead of ViewState, but it didn't help ..
I'm using .NET 2.0 Framework with VS2005 ..