我有一个GridView的设置与我试图绑定到数据表..然后我通过网页进行一些修改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控件自动坚持做它与其相关联的所有数据表的变化?
我使用.NET 2.0框架与VS2005 ..
尝试这个
您的网格视图
<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();
有时框架未能刷新了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();