1

What is the best way for me to access extended properties of a DataColumn on rowDataBound and apply a certain class and tool-tip if the error key exists?

protected void gridView_rowDataBound(object sender, GridViewRowEventArgs e)
{
    switch (e.Row.RowType)
    {
        case DataControlRowType.Header:
            ((DataRow)e.Row.DataItem)...
            break;
        case DataControlRowType.DataRow:

            break;
    }
}

This is what I got before I got stuck. I noticed that my DataRow cast did not have a reference to the DataColumn.

kzh
  • 19,810
  • 13
  • 73
  • 97

2 Answers2

0

The following is what I came up with, but sadly, it is tightly coupled to only one DataTable. Is there a way to do this for use in multiple DataTables? I really don't want to accept my own crappy answer.

protected void gridView_rowDataBound(object sender, GridViewRowEventArgs e)
{
    switch (e.Row.RowType)
    {
        case DataControlRowType.Header:
            foreach (DataColumn col in myDataTable.Columns)
            {
                if (col.ExtendedProperties["error"] != null)
                {
                    e.Row.Cells[col.Ordinal].CssClass = "error-cell";
                    e.Row.Cells[col.Ordinal].ToolTip = col.ExtendedProperties["error"].ToString();
                }
            }                 
            break;
        case DataControlRowType.DataRow:

            break;
    }
}
kzh
  • 19,810
  • 13
  • 73
  • 97
0

Well, you could extract out a method to do this for you and call it from all of your grid RowDataBound events. You could put this in a grid utilities class.

public void ShowExtendedProperties(GridViewRow row, DataTable table)
{
switch (row.RowType)
    {
        case DataControlRowType.Header:
            foreach (DataColumn col in table.Columns)
            {
                if (col.ExtendedProperties["error"] != null)
                {
                    row.Cells[col.Ordinal].CssClass = "error-cell";
                    row.Cells[col.Ordinal].ToolTip = col.ExtendedProperties["error"].ToString();
                }
            }                 
            break;
        case DataControlRowType.DataRow:
            //I assume you have logic here, or other case statements?
            break;
    }
}
Marcie
  • 1,169
  • 1
  • 10
  • 17