【问题标题】:Get a gridview Column Heading after editing编辑后获取gridview列标题
【发布时间】:2013-04-15 14:58:29
【问题描述】:

我正在处理一个 ASP.net 项目,并且有一个 gridview,其中包含来自 sql 数据库的数据集作为数据源。

当我更改 gridview 中的值时,我想更新数据集,以便我可以使用该数据集再次更新数据库。

到目前为止我的声明。

这是我遇到问题的部分。我可以获取选定的行,但不能获取用于更新数据集的选定列名。

 myDataSet.Tables[0].Rows[e.RowIndex][?] = "";

RowUpdating 事件的完整代码。

protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
               //Just get the dataset populated from my database (sql)
                DataSet dsOriginal = dbConn.returnSqlDataset(query);

                //update the dataset here so that you can update the database again.
                foreach (TableCell cell in grdViewDetails.Rows[e.RowIndex].Cells)
                {
                    //set the employeeid so you can update the dataset
                    if (cell.Controls[0] is TextBox)
                    {
                        TextBox textbox = (TextBox)cell.Controls[0];
                        string value = textbox.Text; //This is just a tester to see if the value is correct
                     //   dsOriginal.Tables[0].Rows[e.RowIndex][?] = value; //Here is the problem, how to get the selected column name

                    }
                    else
                    {
                        if (cell.Controls[0] is CheckBox)
                        {
                            CheckBox chkBoxWeek = (CheckBox)cell.Controls[0];
                            Boolean checkStatus = chkBoxWeek.Checked;  //This is just a tester to see if the value is correct
                          //dsOriginal.Tables[0].Rows[e.RowIndex][?] = checkStatus; //Here is the problem, how to get the selected column name
                        }
                    }
                }

                //Use the updated dataset to update the database with.
                dbConn.udpatCourse(dsOriginal );


            }

【问题讨论】:

    标签: c# asp.net sql gridview


    【解决方案1】:

    您可以遍历GridViewUpdateEventArgs.NewValues 集合以找出已更新的内容。

    这略微改编自链接的 MSDN 文档:

    foreach (DictionaryEntry entry in e.NewValues)
    {
        string columName = entry.Key;
        string userInput = e.NewValues[entry.Key];
        // Now you can do stuff with this info that meets your logic needs
    }
    

    如果我没看错你的问题,你应该可以从这里得到你需要的东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 2016-07-29
      相关资源
      最近更新 更多