views:

706

answers:

3

I want to get the cell value from a grid view.

I am using the following code but it produces an error.

Code:

cmd.Parameters.Add("@ProjectCode", SqlDbType.VarChar).Value = ((GridView)TeamMemberGrid.Rows[e.RowIndex].Cells[1].Controls[0]).ToString();

Note:

@ProjectCode is one of the fields in the grid view.

A: 

TableCell has a Text property.

leppie
how to get grid view cell value
thiru
A: 

I think its:

cmd.Parameters.Add("@ProjectCode", SqlDbType.VarChar).Value = ((GridView)TeamMemberGrid.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
BenB
this only produce error question and ur answer are same
thiru
this is also produce error
thiru
I guess the next question would therefore be... What is the error produced?!
BenB
+1  A: 

As Leppie has already stated, the TableCell object exposes a Text property which will give you access to the text contents of a TableCell.

What you need to understand is that the TeamMemberGrid.Rows[e.RowIndex].Cells[1] statement returns a TableCell object referencing the specified TableCell in your GridView.

So your statement becomes :

cmd.Parameters.Add("@ProjectCode", SqlDbType.VarChar).Value = TeamMemberGrid.Rows[e.RowIndex].Cells[1].Text;

Finally, the reason for the cast seems unclear in your statement so I removed that.

Cerebrus