Accessing values of different controls inside the Datagrid control

I have been asked infinite number of times that how do I access a value of a checkbox that is present inside the datagrid control. When I finish answering the question a new question pops up "How do I access dropdownlist value present inside datagrid control?". Instead of answering these questions separately I wrote an article which deals with accessing values of different controls inside datagrid. Following is a code snippet from my article:
foreach(DataGridItem dgi in myDataGrid.Items)

{

TextBox myTextBox = (TextBox) (dgi.Cells[0].Controls[1]);

ListBox myListBox = (ListBox) (dgi.Cells[1].Controls[1]);

DropDownList myList = (DropDownList) (dgi.Cells[2].Controls[1]);

CheckBox myCheckBox = (CheckBox) (dgi.Cells[3].Controls[1]);

str.Append(myTextBox.Text);

string a = myListBox.SelectedValue;

if(a != null && a != "")

{

str.Append(myListBox.SelectedItem.Text);

}

str.Append(myList.SelectedItem.Text);

str.Append(myCheckBox.Checked);

}


You can check out the complete article at: 

 http://www.codersource.net/asp_net_accessing_different_values_datagrid.aspx 

Print | posted @ Wednesday, June 22, 2005 4:44 PM

Twitter