如何只更新C#中的Gridview

时间:2013-04-15 17:20:43

标签: c# asp.net gridview

我在C#ASP.net应用程序中有gridview。

当用户编辑一行时,我想要做的就是更新gridview的数据源(还不是数据库,因为我只是在最后获取datagrid的数据源并将其作为数据集推送到我的数据库中)。

但我得到的值是旧值,而不是我在gridview上编辑的更新值。

为什么会这样?我正在遵循这些链接的所有建议。

    protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = (GridViewRow)grdViewDetails.Rows[e.RowIndex];
        int adsf = row.Controls.Count;

        foreach (Control item in row.Controls)
        {
            if (item.Controls[0] is TextBox)
           {
               TextBox textbox = (TextBox)item.Controls[0];
               string x = textbox.Text;

//on the second for loop the string variable x = Employee1
           }
           if (item.Controls[0] is Label)
           {
               Label mylabel = (Label)item;
               //do stuff
           }

enter image description here

enter image description here

现在文本框变量x应该是xxxxxxx,但它仍然是旧值....为什么会发生这种情况?

你不能更新gridview而不更新任何其他数据集/来源。因为我最后使用gridview的整个数据源。

1 个答案:

答案 0 :(得分:3)

您可以使用GridViewUpdateEventArgs访问新值。以下是the documentation的示例:

  void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {

    // Iterate through the NewValues collection and HTML encode all 
    // user-provided values before updating the data source.
    foreach (DictionaryEntry entry in e.NewValues)
    {

      e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());

    }
  }

您可以使用调试器更仔细地检查e变量,并找出您需要的值。