GridVeiw DataKey提供索引超出范围异常. [英] GridVeiw DataKey is giving index out of range exception.

查看:87
本文介绍了GridVeiw DataKey提供索引超出范围异常.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须检查的是我在gridview的datakey上获得了异常,即它获得了nul值..

gridview正确显示数据..为什么我的代码没有获取数据键?

这是代码

What i have to check i am getting exception on datakey of a gridview that its getting nul value..

gridview is properly displaying data.. why my code is not getting data key??

here is the code

if (DropDownList1.Text == "Completed")
     {
         for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
         {
             GridViewRow row = GridView1.Rows[i];
             if (row.RowType == DataControlRowType.DataRow)
             {
                 ShoppingCartItem item = new ShoppingCartItem();

                 item.ProductID = (int)GridView1.DataKeys[i].Value; //I am getting index out of range exception here..
                 item.Quantity = int.Parse(((Label)(row.FindControl("txtmainqty"))).Text);





                 dbcon db1 = new dbcon();
                  int orderedQuantity = System.Convert.ToInt32(item.Quantity);

                  string query1 = "update Products set productquantity=productquantity-'" + orderedQuantity + "' where ProductID='" + item.ProductID + "'";
                  //bool b = db.UDI(query);
                  bool c = db1.UDI(query1);
                  if (c == true)
                  {

                     // lblshow.Text = "Product Delivered and Added to DataBase Sucessfully";

                  }

                  else
                  {
                      //lblshow.Text = " Operation Not successful";

                  }


我在gridview中没有问题,我已经在另一页中完成了此操作.但是在当前页面上却无济于事.y数据键给出了索引超出范围的异常..


i have no proble with the gridview i have done this in another page.. buts its no working on my current page.. y datakey is giving index out of range exception..

推荐答案

请检查是否已将 GridView DataKeyNames 属性设置为数据的必填字段.因为该程序在设置 DataKeyNames 属性后创建了 DataKey 对象,并且 DataKey 对象包含在 DataKeyNames 属性.如果尚未设置 DataKeyNames 属性,则在访问 DataKeys 集合的元素时可能会出现索引超出范围的异常.
Please check whether you have set the DataKeyNames property of GridView to the required field of the data. Because the program creates DataKey objects after setting the DataKeyNames property and the DataKey object contains the values of the fields specified in the DataKeyNames property. If you have not set DataKeyNames property, you may get index out of range exception when you access elements of DataKeys collection.


这篇关于GridVeiw DataKey提供索引超出范围异常.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆