更新gridview行时没有行位置 [英] There is no row position while updating gridview row

查看:86
本文介绍了更新gridview行时没有行位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试更新gridview行,但我收到了错误。

i我正在使用以下代码



Hi All,
I am trying to update gridview row,but i am getting the error.
i am using the below code

DataTable dt = (DataTable)Session["table"];


               GridViewRow row = Admin_Grid.Rows[e.RowIndex];
               dt.Rows[row.DataItemIndex]["APCUserID"] = ((Label)(row.Cells[1].Controls[0]));
              dt.Rows[row.DataItemIndex]["FirstName"] = ((TextBox)(row.Cells[2].Controls[0]));
               dt.Rows[row.DataItemIndex]["LastName"] = ((TextBox)(row.Cells[3].Controls[0]));
               dt.Rows[row.DataItemIndex]["Organization"] = ((TextBox)(row.Cells[4].Controls[0]));
               dt.Rows[row.DataItemIndex]["PhoneNumber"] = ((TextBox)(row.Cells[5].Controls[0]));
               dt.Rows[row.DataItemIndex]["EmailAddress"] = ((TextBox)(row.Cells[6].Controls[0]));
               //dt.Rows[row.DataItemIndex]["AIPServed"] = ((TextBox)(row.Cells[0].Controls[1]));
               dt.Rows[row.DataItemIndex]["DropDownUserStatus"] = ((DropDownList)(row.Cells[7].Controls[0]));
               // dt.Rows[row.DataItemIndex]["UserID"] = ((TextBox)(row.Cells[0].Controls[1]));
               // dt.Rows[row.DataItemIndex]["Password"] = (TextBox)Admin_Grid.Rows[e.RowIndex].FindControl("TextBoxEmailAddress");
             //  dt.Rows[row.DataItemIndex]["DropDownUserStatus"] = (DropDownList)Admin_Grid.Rows[e.RowIndex].FindControl("DropDownUserStatus");
               //Reset the edit index.
               Admin_Grid.EditIndex = -1;
              // ((TextBox)(row.Cells[1].Controls[0])).Text;
               //Bind data to the GridView control.
               LoadAdminData();





i我得到错误的粗体



任何人都可以请你分享解决方案。



我尝试过:



我尝试了所有可能的方法,我得到了行值,但它下降到下一行它的抛出错误是没有行位置。



i am getting the error the bolded one

Any one can u please share solution to me.

What I have tried:

I tried all the possible ways,i am getting row value but it's coming down to next line it's throwing error is There is no row position.

推荐答案

一些注意事项:



(1)引用Session对象时始终检查空值。

A few notes for you:

(1) Always check for nulls when referencing a Session object.
if(Session["table"] != null){
     //your code here
}



(2)在访问网格的行集合之前,请务必检查行计数。


(2)Always check for the Rows count before accessing the grid’s row collection.

if(Admin_Grid.Rows.Count > 0){
    //your code here
}



(3)如果如果你想获得Label的文本值,那么你应该在将对象转换为Label之后使用.Text。


(3) If you want to get the text vale of a Label, then you should use the .Text after casting the object to a Label.

string someVariableName = ((Label)(row.Cells[1].Controls[0])).Text;



(4)确认您的DataTable包含您尝试访问的字段的名称。在您的情况下,请确保DataTable中的 APCUserID 字段名存在,否则会引发错误。如果您不确定字段名称,则可以使用indeces引用它们。例如:


(4)Verify that you your DataTable contains the name of the field that you are trying to access. In your case, make sure that the APCUserID field name exist in your DataTable otherwise it will throws an error. If you are unsure about the field names, then you can alternatively reference them using indeces. For example:

dt.Rows[row.DataItemIndex][0] = ((Label)(row.Cells[1].Controls[0])).Text;





在引用列时使用索引的缺点是,如果列的序列发生变化,那么您的数据/逻辑将会混乱。



The downside of using index in referencing a column is that if the sequence of column changes, then your data/logic will be messed up.


这篇关于更新gridview行时没有行位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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