How to get gridview values [Resolved]

Posted by Oswaldlily under ASP.NET on 3/13/2012 | Points: 10 | Views : 7848 | Status : [Member] | Replies : 7
I designed gridview in followin way
<asp:gridview runat="server" id="gv" autogeneratecolumns="false">
<columns>
<asp:TemplateField HeaderText="column1">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

</columns>
</gridview>


Now at codebehind while clicking "save" button i need to get these values and save into particular table
How to get these values?
I tried in following manner which fails
string a= gv.Rows[i].Cells[0].Text.ToString();

which carries empty string..but truly it contains values




Responses

Posted by: Hmanjarawala on: 3/13/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi,
your code should be:

string a= ((Label)gv.Rows.FindControl("lbl1")).Text.Tostring();



Mark this as answer, if it helps you..........

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Swapnil on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
try following,
string a= ((label) gv.Rows.FindControl("lbl1")).Text;


Thanks and Regards,
Swapnil

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sreekanthv5 on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
try this
Label lbl = (Label)row.FindControl("lbl1");
string a=lbl.text;
and i think u should change eval to bind if any null values are there in db it will not give error while binding
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("name") %>'></asp:Label>
change eval to bind .

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/14/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

Its explained above nicely
but fore further information
Check this Link It will definitely help you for this and also give more information about grid view

http://www.dotnetspider.com/resources/43119-How-get-Grid-view-control-cell-value.aspx

Thanks

Rajendra Patel

R D Patel

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/14/2012 [Member] Silver | Points: 25

Up
0
Down
since you are using templatefield , your code is not working.
You need to find the control i.e lbl1 in your code.
you can go ahead with Himanshu.
or you can also use like this:
Label myLabel=gv.Rows.FindControl("lbl1");
then you can use myLabel.Text for its future use.

Thanks,
Sanjay

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mayankjain0035 on: 7/27/2013 [Member] Starter | Points: 25

Up
0
Down
sir, i do the same as you told but there is this error
"Object reference not set to an instance of an object"
can you please help me...

thankyou

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mayankjain0035 on: 7/27/2013 [Member] Starter | Points: 25

Up
0
Down
sir, this code show red line below the FindControl

please help me

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response