Reputation:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
// .... here ....
cell.Text = cell.Text.Replace(entry.Value, "<span class='highlightSearchTerm'>" + entry.Value + "</span>");
}
}
}
The part where it says .... here .... I need to somehow get the column name of the DataGrid. So based on which cell I am CURRENTLY in I need to get a Column name. Later in the code depending which Column I am in I need to do different formatting for the cell. Also Column names are not know at run time they are generated on the fly so I need a generic method to get a Column name.
Upvotes: 2
Views: 10344
Reputation: 594
I had to make a method for myself for JUST this purpose. Things like this should be built into the objects, so that we can easily get the information that we need. When it's not built in, we have to get it ourselves, unfortunately:
/// <summary>
/// Makes the assumption that the TableCell is a bound DataControlField cell, and returns the name of the column of the table
/// </summary>
/// <param name="tc"></param>
/// <returns></returns>
private string GetColumnNameOfTableCell (TableCell tc)
{
return ((BoundField)((DataControlFieldCell)tc).ContainingField).DataField;
}
Upvotes: 0
Reputation: 66641
You can change the way you have the loop, and make something like:
for (int i = 0; i < gvMyLista.Rows.Count; i++)
{
// get your data as...
// gvMyLista.Rows[i].Cells[2].Text
}
that you can direct see the cell
and the row
. The gvMyLista
is the id of your GridView.
Now, if you make the formating here it maybe change after the post back, because the data may not bind again, so I suggest to make the formating on PreRender, here is a similar q/a: How to Keep colors on gridview after Postback? asp.net c#
Upvotes: 0
Reputation: 4273
Try this:
string currentColName = GridView1.Columns[GridView1.CurrentCell.ColumnIndex].Name;
Upvotes: 2
Reputation: 223257
By column Name if you mean HeaderText
then you can use a for
loop instead of foreach
, Use the index to get HeaderText
like:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
TableCell cell = e.Row.Cells[i];
string HeaderText = yourGridView.Columns[i].HeaderText;
cell.Text = cell.Text.Replace(entry.Value,
"<span class='highlightSearchTerm'>" + entry.Value + "</span>");
}
}
}
}
Upvotes: 0