【问题标题】:How to Update ONLY the Gridview in C#如何在 C# 中仅更新 Gridview
【发布时间】:2013-04-15 17:20:43
【问题描述】:

我的 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
           }

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

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

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您可以使用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 变量并找出您需要哪些值。

    【讨论】:

    • 问题是,e.NewValues 计数为 0.... 可能是因为它的数据源是 Dataset?
    • 啊,我明白我的问题是什么了,谢谢!我没有将 if(!isPostback) 放入我的 load() 中,因此将旧值重新加载到编辑框中。谢谢
    猜你喜欢
    • 2012-04-04
    • 1970-01-01
    • 2018-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多