How can I change the type of a field in a GridView at runtime using AutoGenerate = "True"?

I created a control that extends the BoundField control to do special handling on the data passed to it.

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

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

protected virtual AutoGeneratedField CreateAutoGeneratedColumn(AutoGeneratedFieldProperties fieldProperties)

      

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

+2


source to share


2 answers


If you have both fields returning to a dataset, I would suggest setting the visibility of the columns instead of trying to dynamically add or modify data fields. Invisible columns don't render HTML, so just have to look at the header row when it's anchored, will check the field you're interested in and set the visibility of the column.



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;
      }
    }
  }

      

+3


source


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; }
    }
}

      

+1


source







All Articles