【问题标题】:Change GridView Column Value in C#在 C# 中更改 GridView 列值
【发布时间】:2014-03-13 05:23:56
【问题描述】:

我有一个 GridView,它有一个列 Status,它在每一行中都有默认值 False(在标签中)。 有一列名为Student,这是链接按钮。所以当用户点击学生姓名时
打开一个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();  

当我尝试这个时。每一行的状态列都是空的。将此设置为关联后。

【问题讨论】:

  • 如果父gridview可以通过第二个gridview的save功能访问,那么通过学生id或者父行号信息,你就可以改变status的值。跨度>
  • 无法访问父 GridView。但我有行号。信息。
  • 有很多方法可以做到这一点。如果父级绑定到数据源并且可以在 save func 中访问,则可以影响更改。或者,如果父级绑定到数据库,您可以更改并重新加载。没有给出完整的场景很难评论。
  • 父绑定到数据集。
  • 上述函数有错误吗?在实际更改它的值之前检查 lblStatus 是否不为空会很好。

标签: c# asp.net gridview


【解决方案1】:

从上面的场景我可以推断的是,当您在第二个网格视图(显示学生信息)中进行更改并成功保存时,应该修改父网格视图中标签列“状态”的值为“真”。

可能的修复:

  1. 由于列“状态”不是绑定字段,而是 根据条件设置,您应该分别设置此值 每次加载gridview时。所以你也可以这样做 在 page_load 函数或网格视图中 加载函数作为网格视图 不会知道这个值之前已经被修改过,除非你 手动设置此值。
  2. 相反,您也可以将“状态”字段更改为已绑定,以便您 可以检索或修改数据集中的值(如您在此处使用的那样),并且始终可以 在 gridview 'Status' 列中显示更新的值 手动完成。

注意:如果您可以发布更多关于正在发生的事情的代码,我可以为您提供相同的代码。

解决方案第 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();
        }

您应该在每次您认为 Status 值已被修改时调用此绑定方法,因为它是动态数据。

也在Page_Load方法中调用函数bindTheGriView()

【讨论】:

  • 我添加了gridview绑定功能。关联意味着真。待处理意味着 False。
  • 查看我回答的解决方案部分,如果您有问题,请告诉我。
  • 我已经按照你说的去做了。仍然没有修改状态列值。它仍然是待处理而不是关联。
  • 对不起,我犯了错误。我完全忘记了 Page_Load 函数。给我一些时间我会回复你的
  • @SearchForKnowledge:你能描述得更具体一些吗?网址坏了
【解决方案2】:

假设您对链接按钮列使用 BoundFields,并且您想要处理 LinkBut​​ton-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,那么尝试使用此代码它将起作用。
  • 我不想在单击 LinkBut​​ton 时更改状态。当我单击第二个 GridView 的保存按钮时。
【解决方案3】:

当你保存第一个gridview的数据时,在这种情况下更改数据集以绑定第二个gridview,你必须在第一个gridview上保存数据时加载第二个gridview,所以没有其他选择。

【讨论】:

    猜你喜欢
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多