Accessing a Control’s Value from DataGrid in ASP.NET

Introduction

There are many real-world situations in which you might like to access values from a DataGrid that has different server controls in each column, i.e. if you want to create a grid where you can get edit multiple records on one click or do some other work on that data.

Download source code     Run Sample

DataGrid

Let us consider a case where we have a DataGrid as shown in Figure 1.

 DataGrid with values

We have the following columns in the DataGrid:

  • Column 1: a bound column that contains names from a datasource.
  • Column 2: contains a TextBox control, which is bound to an age field of the datasource.
  • Column 3: contains a CheckBox control, which is bound to an Isgraduate field of the datasource.
  • Column 4: contains a CheckBoxList control.
  • Column 5: contains a RadioButtonList control.
  • Column 6: contains DropDownList control.

Iterating through Rows of a DataGrid

We have to iterate through rows of our DataGrid to get values of controls within that row, so let’s do that first. The DataGrid control has a property called Items, which is a collection of object DataGridItem that represents the individual item in a DataGrid control, and we can use this property to iterate through DataGrid rows by following these six steps.

foreach(DataGridItem dataGridItem in MyDataGrid.Items){ 
				
}
  1. Getting the Value from a Bound Column in DataGrid

  2. Our first column is a bound column, and we need to get a value written in that column. DataGridItem has a property named Cells of which is the collection of the TableCell object that represents the cells of the row. The Text property of TableCell gives us the value written in that particular cell.

    //Get name from cell[0] 
    String Name = dataGridItem.Cells[0].Text;
    

  3. Getting the Value of a TextBox Control in DataGrid
    Now our second column contains a TextBox control, and we need to get a Text property of that object. We use the FindControl method of the DataGridItem to get a reference for the TextBox.

    //Get text from textbox in cell[1] 
    String Age = 
    ((TextBox)dataGridItem.FindControl("AgeField")).Text;
    

  4. Getting the Value from CheckBox Control in DataGrid
    Our third column in DataGrid contains a CheckBox Web control that we need to check if the Checked property of that control is true or false.

    //Get Checked property of Checkbox control 
    bool IsGraduate = 
    ((CheckBox)dataGridItem.FindControl
    ("IsGraduateField")).Checked;
    

  5. Getting the Value from CheckBoxList Web Control in DataGrid
    This case differs from the previous one because the CheckBoxList may return more then one value selected. We have to iterate through the CheckBoxList items to check if the user has selected a particular item.

    // Get Values from CheckBoxList 
    String Skills = ""; 
    foreach(ListItem item in 
    
    ((CheckBoxList)dataGridItem.FindControl("CheckBoxList1")).Items) 
    { 
    	if (item.Selected){ 
    		Skills += item.Value + ","; 
    	} 
    } 
    Skills = Skills.TrimEnd(',');
    

  6. Getting the Value from a RadioButtonList Web Control in a DataGrid
    We use the FindControl method of the DataGridItem to get a reference for RadioButtonList and then the SelectedItem property of the RadioButtonList to get the selected item from RadioButtonList.

    //Get RadioButtonList Selected text 
    String Experience = 
    ((RadioButtonList)dataGridItem.FindControl("RadioButtonList1"))
        .SelectedItem.Text;
    

  7. Getting the Value from a DropDownList Web Control in a DataGrid
    This is similar to a RadioButtonList. I have used this control just to show that it works the same way as any other ListControls. Similarly, you can work with a ListBox Web control as we did with the CheckBoxList control.

    //Get DropDownList Selected text 
    String Degree = 
    ((DropDownList)dataGridItem.
    FindControl("DropDownList1")).SelectedItem.Text;
    

Combining Everything Together

Now that we know how to get the values from each Control, let’s put together the code to display the result of values in each row of the DataGrid.

String Result = ""; 
foreach(DataGridItem dataGridItem in MyDataGrid.Items){ 
	//Get name from cell[0] 
	String Name = dataGridItem.Cells[0].Text; 
	//Get text from textbox in cell[1] 
	String Age = 
((TextBox)dataGridItem.FindControl("AgeField")).Text; 
	//Get Checked property of Checkbox control 
	bool IsGraduate = 
((CheckBox)dataGridItem.FindControl("IsGraduateField")).Checked; 
        // get Values from Checkboxlist 
	String Skills = ""; 
	foreach(ListItem item in ((CheckBoxList)dataGridItem.
FindControl("CheckBoxList1")).Items){ 
		if (item.Selected){ 
			Skills += item.Value + ","; 
		} 
	} 
	Skills = Skills.TrimEnd(','); 
         
	//Get RadioButtonList Selected text 
	String Experience = 
	((RadioButtonList)dataGridItem.
FindControl("RadioButtonList1")).SelectedItem.Text; 
            
	//Get DropDownList Selected text 
	String Degree = 
	((DropDownList)dataGridItem.
FindControl("DropDownList1")).SelectedItem.Text; 
             
	// Build String to show result. 
	Result += Name; 
	Result += " [Age -" + Age + "] "; 
	
	if (IsGraduate){ 
		Result += "Is Graduate , "; 
	}else{ 
		Result += "Is not Graduate , "; 
	} 
    
	Result += "Has Skills[" + Skills + "] , ";    
	Result += "Has " + Experience + " Experience , And " ; 
    	Result += "Has " + Degree + " Degree." ; 
    	Result += ""; 
}

Result

Figure 2 shows the result that we obtained by iterating through a DataGrid.

Figure2

Bookmark with:
Technorati   Digg   Delicious   StumbleUpon   Facebook
My name is Jigar Desai I share my ideas on this site and you can contact me by filling contact form.