karthik
karthik

Reputation:

Getting Data from a DataGrid

I'm populating a C# DataGrid with some values, and I'd like to retrieve a value from a cell when I click on that. How do I go about doing that with the .NET 1.1 Framework?

datagridview1 is not available in .net1.1

only for windows applications

Upvotes: 2

Views: 6730

Answers (3)

Tyler S. Loeper
Tyler S. Loeper

Reputation: 836

Here is an excerpt from http://www.jigar.net/articles/viewhtmlcontent4.aspx, also found as a link in Eric Lathrop's answer. This covers most of the common scenarios.

Bear in mind that in the following example, dataGridItem is an alias for MyDataGrid.Items[x], where x is the (row) index. This is because the following example is using a foreach loop, so if skimming keep that in mind.

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){ 

}

Getting the Value from a Bound Column in DataGrid

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;

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;

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;

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(',');

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;

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;

Upvotes: 0

Eric Lathrop
Eric Lathrop

Reputation: 1358

If you are talking about a web/ASP.Net 1.1 DataGrid:

  1. Use myDataGrid.Items to get a row
  2. Use row.Cells[x] to get a column in that row
  3. Use (TextBox)cell.FindControl("TextBox1") to get a control inside the cell

For more info, see Accessing a Control’s Value from DataGrid in ASP.NET

Upvotes: 3

Belliez
Belliez

Reputation: 5376

Does the following work:

string strCell = (string)dataGridView1.CurrentCell.Value;

Upvotes: 0

Related Questions