Click here to Skip to main content
15,914,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to access gridview row elements where user clicks edit button..! All cells of that particular row i should be able to access in code behind. I am using c#.

I have chosen DATA SOURCE for the Gridview in the design view itself by using smart tag and not through code behind.

i tried following code to retrieve values

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        dt1 = g1.Getupdatedtasktime(Int32.Parse(GridView1.SelectedRow.Cells[3].Text), float.Parse(GridView1.SelectedRow.Cells[2].Text));
        dt1 = g1.Getupdatedtasktime(Int32.Parse(GridView1.Rows[GridView1.EditIndex].Cells[1].Text), float.Parse(GridView1.Rows[GridView1.EditIndex].Cells[2].Text));
// trying to access second cell value from the selected row that is when edit is clicked on that row.

String test = GridView1.SelectedRow.Cells[1].ToString();
}


none of them seems to work. Please can somebody help me on this regard.

Thanks.
Posted

HI,

I found out how to do that its really simple.

C#
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];
        Int32 tId = Int32.Parse(row.Cells[1].Text);
        //float tTime = float.Parse(row.Cells[2].Text);
    }


but m not able to get the value which is in edit mode. That is the edited value from the textbox is showing as empty.
 
Share this answer
 
If you are using Item template in your gridview then try this:

C#
//Finding the values for rows at index 0
string strID = ((Label)gvEmpDetails.Rows[0].Cells[0].FindControl("lblID")).Text;
string strName = ((TextBox)gvEmpDetails.Rows[0].Cells[1].FindControl("txtName")).Text;
        string strAddress = ((TextBox)gvEmpDetails.Rows[0].Cells[2].FindControl("txtAddress")).Text;


If you are not using template field then write simply this:

C#
//Finding the values for rows at index 0
string strID = ((Label)gvEmpDetails.Rows[0].Cells[0];
string strName = ((TextBox)gvEmpDetails.Rows[0].Cells[1];
string strAddress = ((TextBox)gvEmpDetails.Rows[0].Cells[2];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900