【问题标题】:Updating data for a single field in a GridView with an ObjectDataSource?使用 ObjectDataSource 更新 GridView 中单个字段的数据?
【发布时间】:2013-07-09 13:50:04
【问题描述】:

我正在使用由 ASP.net Web 表单应用程序上的 ObjectDataSource 支持的 GridView。 GridView 从数据库中获取大量字段并显示它们。我只想启用其中一个字段的编辑,但我已经遇到了一些问题。我的 ObjectDataSource 更新命令如下所示:

public void UpdateRML(string itemCode, int newRML)
        {

            SqlCommand cmd = new SqlCommand("UpdateRML_byItemCode", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter ItemCodeParameter = new SqlParameter("@itemCode", SqlDbType.NVarChar);
            ItemCodeParameter.Value = (itemCode);
            cmd.Parameters.Add(ItemCodeParameter);

            SqlParameter NewRMLParamter = new SqlParameter("@RML", SqlDbType.Int);
            NewRMLParamter.Value = (newRML);
            cmd.Parameters.Add(NewRMLParamter);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

        }

如您所见,它通过获取两个参数并将它们传递给数据库中的存储过程来进行更新。不幸的是,当我按下 GridView 的“更新”按钮时(单击编辑后),我收到了这个错误。

 ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 
'UpdateRML' that has parameters: p, itemcode, newRML, itemname, RML, Buyer, Vendor,
 AvgCost, FWAC, MSRP, SalePrice, Profit_Ave, Profit_MSRP, onHand, isCommited, onOrder, ATS,
 AOH, Vintage, Producer, Country, Region, Appellation, CRU, Size, RtrakDept, str. 

几乎是说我需要包含 GridView 的所有字段,我想这并不奇怪,因为我确实读过这个article on msdn,它说了这样的话,但令人沮丧的是该方法需要采取所有当我只使用 itemcode 和新数据时,这些参数。

那么最好的方法是什么?我是否让它变得比它应该的更复杂?我应该只使用带有荒谬签名的这种方法吗?如果您需要更多信息,请与我们联系!谢谢。

【问题讨论】:

    标签: c# asp.net sql gridview objectdatasource


    【解决方案1】:

    如果有人发现这个并且对答案感兴趣:我所要做的就是将 GridView 的 AutoGenerateColumns 设置为 false,并改用 asp:BoundField。我将每个 BoundField 的 ReadOnly 属性设置为“False”,除了我希望能够编辑的那个,然后砰!现在UpdateRML() 方法只需要一个参数:RML。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多