views:

330

answers:

2

I've created a control that extends the BoundField control to do some special processing on the data that's passed into it.

I now have a grid that has AutoGenerateColumns="true", by which I'd like to intercept the HeaderText, see if it's a particular value and then swap in the "SpecialBoundField" instead. I've tried using the OnDataBinding event to loop through the columns, but at this point there are no columns in the grid. I think that RowDataBound and DataBound are too late in the game so not sure what to do.

My next thought was to override the grid control itself to add in a "AutoGeneratingColumn" event in

protected virtual AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)

Can anyone help or point me in a better direction? Thanks!

A: 

If you have both fields coming back in the dataset, I would suggest setting the column visibilities instead of trying to dynamically add or change the datafields. Invisible columns don't render any HTML, so it would just be a matter of looking at the header row when it gets bound, checking the field you're interested in, and setting the column visibility.

void myGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if (e.Row.RowType == DataControlRowType.Header)
    {
      if (e.Row.Cells[1].Text = "BadText")
      {
         myGridView.Columns[1].Visible = false;
         myGridView.Columns[5].Visible = true;
      }
    }
  }
womp
This may work, but I do need to change the actual column type from the simple "BoundField" to "SpecialBoundField" as well. I'll give this a go and report back.
rball
Got me to where I needed to go, my solution is posted as well...
rball
A: 

What I ended up with:

public class SpecialGridView : GridView
{
 protected override void OnRowDataBound(GridViewRowEventArgs e)
 {
  ModifyData(e);
  base.OnRowDataBound(e);
 }

 IList<string> _columnNames = new List<string>();
 protected void ModifyData(GridViewRowEventArgs e)
 {
  LoadColumnNames(e);

  if (e.Row.RowType == DataControlRowType.DataRow)
  {
   for (int i = 0; i < e.Row.Cells.Count; i++)
   {
    string currentColumnName = _columnNames[i];
    if (IsSpecialColumn(currentColumnName))
    {
     string text = e.Row.Cells[0].Text;
     bool isSpecialData = text.ToUpper() == "Y";

     if (isSpecialData)
     {
      e.Row.Cells[i].CssClass += " specialData";
     }
    }
   }
  }
 }

 private void LoadColumnNames(GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.Header)
  {
   foreach (TableCell cell in e.Row.Cells)
   {
    _columnNames.Add(cell.Text);
   }
  }
 }

 private bool IsSpecialColumn(string currentColumnName)
 {
  foreach (string columnName in SpecialColumnNames)
  {
   if (currentColumnName.ToUpper() == columnName.ToUpper())
   {
    return true;
   }
  }
  return false;
 }

 private IList<string> _specialColumnNames = new List<string>();
 public IList<string> SpecialColumnNames
 {
  get { return _specialColumnNames; }
  set { _specialColumnNames = value; }
 }
}
rball