Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retreiving cell text of bound datagrid column? 1

Status
Not open for further replies.

NBartomeli

Programmer
Jul 1, 2003
111
US
I have a datagrid with 4 templated columns,

3 of the columns have controls in them, and the other one is bound to a field in the datatable I bind the grid to

after I bind the grid, it displays the data just fine, but If I loop through the item collection to try to extract the text from the cell's with the text in them , it comes back blank.

If I set the text manually in the program using Cell(0).text = "something" and then retreive it, this works fine.

Any ideas to point me in the right direction?

Thanks in advance
 
For the columns that contain controls you'll usually need to access the cell, the control and then the control property that you're after.

If you're just iterating through a DataGrid (which is what I think you're doing then here's an C# example that will give you the idea.
Code:
foreach(DataGridItem dgItem in yourDataGrid.Items)
{
string myID=dgItem.Cells[0].Text;
string someDDLID = ((DropDownList)dgItem.FindControl("ddlmyID")).SelectedItem.Value;
string someData = ((TextBox)dgItem.FindControl("txtYourTextBox")).Text;
}
It does depend on the event you are trying to access the value of the control under. If this were an update command, item_databound, item_command etc. etc... it would be more like:
Code:
string myID=e.Item.Cells[0].Text;
DropDownList ddl1 = (DropDownList)e.Item.Cells[1].FindControl("ddlmyID");
string someDDLID = ddl1.Text;
TextBox tb = (TextBox)e.Item.Cells[2].FindControl("txtYourTextBox")
string someData = tb.Text;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top