Introduction
In the previous article, Hide DataGrid Columns via HeaderText, I described how to hide a DataGridColumn
via the HeaderText
. If you have not read the previous article, it provides a method to hide/show columns based on a user's view. This solved the problem of referencing the index of the column which can lead to problems when you move columns around. This article is built upon that example and demonstrates how to retrieve data in the DataGrid
by the column's HeaderText
. You will now have the ability to use the text of the header for both populating and retrieving data from the DataGrid
.
I ran into the problem of referencing the index of a column a while back. As you know, users frequently want to add, remove, and move columns in a grid. This can become a tedious task if you have many columns in the grid. There is a simple solution to the indexing nightmare. For the simplicity of the example, I opted to include the methods that retrieve a cell or column by HeaderText
in the page's class. In your application, you could choose to create a custom DataGrid
and implement the methods in it, or create a helper class that each grid will call, or just do what I did.
The code
Below is an example of the method that retrieves a TableCell
by the headerText
. The input parameters are DataGridItem item
and string headerText
. To use this method, you would loop through the DataGridItem
s, which I will show you how to do:
public TableCell GetCellByHeaderText(DataGridItem item,
string headerText)
{
if(item == null || headerText == null)
{
return null;
}
DataGrid dg = item.NamingContainer as DataGrid;
int colNum = this.GetIndexByHeaderText(dg.Columns,
headerText);
if(colNum >= 0)
{
return item.Cells[colNum];
}
return null;
}
This method retrieves a DataGridColumn
by the headerText
. The input parameters are DataGrid dg
and string headerText
:
public DataGridColumn GetColumnByHeaderText(DataGrid dg,
string headerText)
{
if(dg == null || dg.Columns == null ||
headerText == null)
{
return null;
}
foreach(DataGridColumn col in dg.Columns)
{
if(col.HeaderText == headerText)
{
return col;
}
}
return null;
}
This method retrieves the index of the column where the column's HeaderText
matches the passed in headerText
. The input parameters are DataGridColumnCollection cols
and string headerText
:
private int GetIndexByHeaderText(DataGridColumnCollection cols,
string headerText)
{
if(cols == null)
{
return -1;
}
for(int i=0; i < cols.Count; i++)
{
if(cols[i].HeaderText == headerText)
{
return i;
}
}
return -1;
}
Page code-behind
This method utilizes the methods shown above to retrieve the data via the HeaderText
. It is placed in the page's code-behind class. The method loops through each DataGridItem
in the DataGrid.
This example outputs the data back on the page for demonstration purposes. In a real application, you would store the data in a database.
private void DisplayDataFromGrid()
{
foreach(DataGridItem item in this.dgExample2.Items)
{
string name = this.GetCellByHeaderText(item,
"Name").Text;
string title = this.GetCellByHeaderText(item,
"Title").Text;
string phone = this.GetCellByHeaderText(item,
"Phone").Text;
string email = this.GetCellByHeaderText(item,
"Email").Text;
string salary = this.GetCellByHeaderText(item,
"Salary").Text;
string ssnum = this.GetCellByHeaderText(item,
"SS#").Text;
Response.Write(name);
Response.Write(" | " + title);
Response.Write(" | " + phone);
Response.Write(" | " + email);
Response.Write(" | " + salary);
Response.Write(" | " + ssnum);
Response.Write("<br>");
}
}
Conclusion
By using this article's and my previous article's examples, you no longer need to reference a column by the index. You can use the HeaderText
for both display and retrieval purposes. This can save you a great amount of time if your DataGrid
is constantly modified.