Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I have a gridview and I want to take a parameter from it to pass it to an insert function.
I tried this code but I get an exception
string accountname = ((TextBox)GridView2.Rows[j].Cells[0].Controls[0]).Text; 
insert_ReadyMade_Mobil(Int64.Parse(cmb_Subgroupmobiles.SelectedValue.ToString()), Id.ToString());

Can any one help me please, and notice that I fill the gridview in the runtime.
Thanks
Posted
Updated 9-Jan-11 0:14am
v2
Comments
thatraja 9-Jan-11 6:00am    
what exception? which line? include all the details in your question.

Looks like,it's not able to find out the textbox inside the GridView & throwing exception at your first line itself.To acess textbox inside a Gridview in ASP.net, you need to use FindControl to find the textbox inside and cast it to TextBox.

C#
foreach (GridViewRow row in GridView2.Rows)
    {


     string accountname = ((TextBox)row.FindControl("TextBox1")).Text;

    }

Check out this article

Accessing the different controls inside a GridView control[^]

Hope this helps!
 
Share this answer
 
v3
Comments
moon2011 10-Jan-11 4:21am    
thanks alot
Anupama Roy 10-Jan-11 4:22am    
You are welcome!
First, you're retrieving something you call accountname, but after retrieving it, I don't see where you're using it in the next statement.

Second, I would do this for the 2nd line:

C#
Int64 value;
if (Int64.TryParse(cmb_Subgroupmobiles.SelectedValue.ToString(), out value)
{
    insert_ReadyMade_Mobil(value, ID.ToString());
}
else
{
    // display an error message?
}


 
Share this answer
 
v3
Heres a working example from my code:
lblMaxCreditJourneys->Text = gridPassenger->Rows[e->RowIndex]->Cells[this->dgMaxCreditJourneys->Name]->Value->ToString();


Its C++/CLI. Change -> to . and you will have C#
 
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