如何使用多行维护Gridview [英] How to maintain Gridview with multiple rows
问题描述
晚安,
这是单击按钮时的方法
protected void btnAdd_Click(object sender,EventArgs e)
{
if(cboSources.SelectedIndex!= -1)
{
grdViewDisease.DataSource = GetDataTable(cboSources.SelectedValue);
grdViewDisease.DataBind();
}
}
这是绑定数据并使用上述方法调用
private DataTable GetDataTable(string itemID)
{
DataTable dt = new DataTable();
dt.Columns.Add( ID,typeof(string));
dt.Columns.Add(Disease,typeof(string));
dt.Columns.Add(来源 ,typeof(string));
dt.Rows.Add(itemID,hidSource.Attri butes [value],cboSources.SelectedItem);
返回dt;
}
所以之后添加一个新行并将其绑定到网格我可以添加一个新行,但不显示旧行值
那么请解释如何保留所有值?
谢谢你
Hi,
Good Evening,
This is the method when clicking a button
protected void btnAdd_Click(object sender, EventArgs e)
{
if (cboSources.SelectedIndex != -1)
{
grdViewDisease.DataSource = GetDataTable(cboSources.SelectedValue);
grdViewDisease.DataBind();
}
}
This is for binding data and calling in the above method
private DataTable GetDataTable(string itemID)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Disease", typeof(string));
dt.Columns.Add("Source", typeof(string));
dt.Rows.Add(itemID, hidSource.Attributes["value"], cboSources.SelectedItem);
return dt;
}
So after adding a new row and binding it to the grid I can add a new row but old row values are not shown
So please explain how to retain all the values?
Thank You
推荐答案
你只添加了一行。在你的GetDataTable方法中,你正在创建每次都有新的数据表,并返回新的数据表。因为它会返回新值并消失旧值。
为了解决这个问题,你只需要全局声明Datatable变量试试。
Well you are adding only one row.In your GetDataTable method,you are creating the new datatable everytime and returing that new datatable.Because of this it will return new values and vanish old value.
For solving this issue,you just globally declare Datatable variable and check.
试试这个!!
Try this!!
DataTable dt = null;
protected void btnAdd_Click(object sender, EventArgs e)
{
if (cboSources.SelectedIndex != -1)
{
grdViewDisease.DataSource = GetDataTable(cboSources.SelectedValue);
grdViewDisease.DataBind();
}
}
private DataTable GetDataTable(string itemID)
{
if(viewstate["dt"]==null)
{
dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Disease", typeof(string));
dt.Columns.Add("Source", typeof(string));
}
else
{
dt=(DataTable)viewstate["dt"];
}
dt.Rows.Add(itemID, hidSource.Attributes["value"], cboSources.SelectedItem);
viewstate["dt"]=dt;
return dt;
}
这篇关于如何使用多行维护Gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!