我有一个GridView设置,我试图绑定到DataTable ..然后我通过网页对GridView的值进行了一些更改..然后我在按钮点击的代码后面,看到DataTable仍然它有旧的价值......
GridView的ASPX标记代码:
<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>
从单击按钮的代码隐藏中初始化GridView数据:
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();
在上面的代码中,您可以看到我使用默认值0和100初始化行。这是我在从网页更改这些值后在调试事件中看到的内容。
如何让GridView自动将对其完成的所有更改保存到其链接的DataTable?
我在VS2005上使用.NET 2.0 Framework ..
答案 0 :(得分:1)
试试这个
你的网格视图
<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>
并在代码中
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();
答案 1 :(得分:0)
有时,Framework无法使用新数据刷新DataSet / Datatable。
尝试清除网格数据源。
ESBAndTSRValuesInputGridView.DataSource = null;
ESBAndTSRValuesInputGridView.DataBind();
ESBAndTSRValuesInputGridView.DataSource = dt;
ESBAndTSRValuesInputGridView.DataBind();
所以你的代码将是
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();