在c#中更改GridView列值

本文关键字:GridView 列值 | 更新日期: 2023-09-27 17:49:45

我有一个GridView,它有一个列状态,它有默认值False(在一个标签)在每一行。有一个列命名为学生,这是链接按钮。所以当用户点击Student name
打开gridview以显示该用户的数据。我改变第二个GridView的数据,然后保存它。当这个数据被成功保存。我想修改第一个GridView的状态从False到True为特定的行。所以请建议我怎么做?是否有必要再次绑定GridView。有什么简单的方法吗,请告诉我。

所以主要的问题是如何修改GridView中一个特定的单元格值,基于一些动作。

我正在写这段代码,我正在尝试。我保存父GridView行号。视图状态。

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();  

当我尝试这个。每行"状态"列为"空"。

在c#中更改GridView列值

从上面的场景中,我可以推断的是,当你在第二个网格视图(显示学生信息)中进行更改并成功保存它时,那么父网格视图中标签列'Status'的值应该修改为'True'。

可能的修复:

  1. 因为列'状态'不是一个绑定字段,它是根据条件设置,您应该分别设置此值每次gridview加载时。你也可以这么做在page_load函数或在Grid View加载函数作为网格视图不会知道这个值之前被修改过,除非你
  2. 相反,您也可以将字段"Status"更改为绑定,以便您是否可以检索或修改数据集中的值(就像您在这里使用的那样)并且总是可以有没有在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();
}

你的方法有'Code to bind first 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,因此没有其他选择