问题
I have a GridView setup with 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 ...
ASPX Markup code for the GridView:
<asp:gridview ID="ESBAndTSRValuesInputGridView" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Award ID" HeaderText="Award ID" Visible="false" />
<asp:BoundField DataField="Award Name" HeaderText="Award Name" />
<asp:TemplateField HeaderText="ESB Value">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TSR Value">
<ItemTemplate>
<asp:TextBox ID="TextBox2" 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("Award ID", typeof(string)));
dt.Columns.Add(new DataColumn("Award Name", typeof(string)));
dt.Columns.Add(new DataColumn("ESB Value", typeof(string)));
dt.Columns.Add(new DataColumn("TSR Value", 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();
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'm using .NET 2.0 Framework with VS2005 ..
回答1:
try this
your grid view
<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="TSR Value">
<ItemTemplate>
<asp:TextBox ID="TextBox2" Text='<%# Eval("TSRValue") %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
and in code behind
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();
回答2:
Sometimes Framework fails the flush the DataSet/Datatable with the new data.
Try clearing the grid datasource.
ESBAndTSRValuesInputGridView.DataSource = null;
ESBAndTSRValuesInputGridView.DataBind();
ESBAndTSRValuesInputGridView.DataSource = dt;
ESBAndTSRValuesInputGridView.DataBind();
So your code will be
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Award ID", typeof(string)));
dt.Columns.Add(new DataColumn("Award Name", typeof(string)));
dt.Columns.Add(new DataColumn("ESB Value", typeof(string)));
dt.Columns.Add(new DataColumn("TSR Value", 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 = null;
ESBAndTSRValuesInputGridView.DataBind();
ESBAndTSRValuesInputGridView.DataSource = dt;
ESBAndTSRValuesInputGridView.DataBind();
来源:https://stackoverflow.com/questions/17364934/gridview-changes-not-showing-in-binded-datatable