Store ID of RowEditing Instead of Index

こ雲淡風輕ζ 提交于 2019-12-13 05:19:05

问题


In my application, I give the user the option to search the database. When they click edit, I want the GridView to only show the row they are updating by storing its ID.

I'm trying the following code, but it is not returning anything other than the Control.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
     string value = GridView1.Rows[e.NewEditIndex].Cells[3].ToString();
}

Any ideas on how to store the value that I'm looking for?


回答1:


Use the cell's Text property

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
     string value = GridView1.Rows[e.NewEditIndex].Cells[3].Text;

     // or 
     string value = GridView1.Rows[e.NewEditIndex].Cells[3].Value;
}

Or If you have declared the control as <TemplateField> for which you are trying to get the value for then try

string value = ""
Label lblID = (Label)GridView1.Rows[e.NewEditIndex].FindControl("lblID");
value = lblID.Text;

Another alternative would be to add DataKeyNames="ID" for the gridview which you can retrieve using

 // assuming that the value of your datakey is numeric value 
 long Id = long.Parse(GridView1.DataKeys[e.NewEditIndex].Values["ID"].ToString());   

Note you can add multiple comma separated values to DataKeyNames property.



来源:https://stackoverflow.com/questions/25128461/store-id-of-rowediting-instead-of-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!