[英]Change GridView Column Value in C#
我有一个GridView,它具有一列Status,在每一行中的默认值为False(在标签中)。 有一个名为“学生”的列,这是链接按钮。 因此,当用户点击学生姓名时
将打开一个gridview以显示该用户的数据。 我更改第二个GridView的数据,然后将其保存。 因此,当此数据成功保存时。 我想将该特定行的第一个GridView的状态从False修改为True。 所以,请建议我该怎么做? 是否有必要再次绑定GridView。 有没有简单的方法,请告诉我。
因此,主要问题是如何基于一些操作来修改GridView中的特定单元格值。
我正在写一段我正在尝试的代码。 我要保存父级GridView行号。 是ViewState。
protected void btnSave_Click(object sender, EventArgs e)
{
if (ViewState["RowIndexPOS"] != null)
{
int iRowIndex = Convert.ToInt32(ViewState["RowIndexPOS"]);
Label lblStatus = (Label)GridViewMultiplePOSAssociationId.Rows[iRowIndex].FindControl("LabelStatusPendingPOSId");
//Means all rows in GridView are successfully associated
if (table.Rows.Count == iResultCount)
{
lblStatus.Text = "Associated";
}
else
{
lblStatus.Text = "Pending";
}
}
}
绑定第一个gridview的代码
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Row Number", typeof(string)));
dt.Columns.Add(new DataColumn("POS Id", typeof(string)));
dt.Columns.Add(new DataColumn("Action", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
for (int index = 0; index < m_listStrPendingListOfPOS.Count; index++)
{
dr = dt.NewRow();
int iRowNo = index + 1;
dr["Row Number"] = iRowNo;
string strGridViewPOSId = m_listStrPendingListOfPOS[index];
dr["POS Id"] = strGridViewPOSId;
dr["Action"] = string.Empty;
dr["Status"] = "Pending";
dt.Rows.Add(dr);
}
ViewState["POSTable"] = dt;
GridViewMultiplePOSAssociationId.DataSource = dt;
GridViewMultiplePOSAssociationId.DataBind();
当我尝试这个。 每行中的状态列为空。 将其设置为关联后。
从上面的场景中,我可以推断出,当您在第二个网格视图(显示学生信息)中进行更改并成功保存后,父网格视图中的Label列“ Status”的值应修改为“ True”。 ”。
可能的修复:
注意:如果您可以将更多代码发布到正在发生的事情上,那么我可以为您提供相同的代码。
解决方案第2部分:
请尝试以下方法:
让我们假设您已将“代码绑定到第一个gridview”放入bindTheGriView()
方法中。 在这种假设下,以下代码可以正常工作。 请根据您的代码对其进行修改。
btnSave_Click方法:
protected void btnSave_Click(object sender, EventArgs e)
{
bool statusFlag=false;
if (ViewState["RowIndexPOS"] != null)
{
int iRowIndex = Convert.ToInt32(ViewState["RowIndexPOS"]);
Label lblStatus = (Label)GridViewMultiplePOSAssociationId.Rows[iRowIndex].FindControl("LabelStatusPendingPOSId");
//Means all rows in GridView are successfully associated
if (table.Rows.Count == iResultCount)
{
lblStatus.Text = "Associated";
}
else
{
lblStatus.Text = "Pending";
}
}
//now call the binding method with the bool flag value
bindTheGriView();
}
您的方法具有“绑定第一个gridview的代码”(此示例中的bindTheGriView(bool statusFlag))将如下所示:
private void bindTheGriView()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Row Number", typeof(string)));
dt.Columns.Add(new DataColumn("POS Id", typeof(string)));
dt.Columns.Add(new DataColumn("Action", typeof(string)));
dt.Columns.Add(new DataColumn("Status", typeof(string)));
for (int index = 0; index < m_listStrPendingListOfPOS.Count; index++)
{
dr = dt.NewRow();
int iRowNo = index + 1;
dr["Row Number"] = iRowNo;
string strGridViewPOSId = m_listStrPendingListOfPOS[index];
dr["POS Id"] = strGridViewPOSId;
dr["Action"] = string.Empty;
//check for the flag. if the flag is true set status to Pending else to Associated
dr["Status"]=((Label)GridViewMultiplePOSAssociationId.Rows[index].FindControl("LabelStatusPendingPOSId")).Text;
dt.Rows.Add(dr);
}
ViewState["POSTable"] = dt;
GridViewMultiplePOSAssociationId.DataSource = dt;
GridViewMultiplePOSAssociationId.DataBind();
}
由于状态值是动态数据,因此每次您认为状态值已被修改时,都应该调用此绑定方法。
还要在Page_Load方法中调用函数bindTheGriView()
假设您正在对“链接按钮”列使用BoundFields,并且想要处理LinkButton-click事件(而不是GridView的例如RowCommand):
protected void LinkClicked(Object sender, EventArgs e)
{
//After Populating your Second Gridview
var closeLink = (Control) sender;
GridViewRow row = (GridViewRow) closeLink.NamingContainer;
dataGridView1.Rows[rowNum].Cells[colNum].Text = "True";
}
保存第一个gridview的数据时,在这种情况下,更改要绑定第二个gridview的数据集,则必须在将数据保存到第一个gridview时加载第二个gridview,因此没有其他选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.