How to hide column of DataGridView when using custom DataSource?


Answers

You have to hide the column at the grid view control rather than at the data source. Hiding it at the data source it will not render to the grid view at all, therefore you won't be able to access the value in the grid view. Doing it the way you're suggesting, you would have to access the column value through the data source as opposed to the grid view.

To hide the column on the grid view control, you can use code like this:

dataGridView1.Columns[0].Visible = false;

To access the column from the data source, you could try something like this:

object colValue = ((DataTable)dataGridView.DataSource).Rows[dataSetIndex]["ColumnName"];
Question

I have a small app in c#, it has a DataGridView that gets filled using:

grid.DataSource = MyDatasource array;

MyClass hold the structure for the columns, it looks something like this:

class MyDatasource
{
    private string column1;        
    private string column2;

    public MyDatasource(string arg1, string arg2)
    {
        this.column1 = arg1;
        this.column2 = arg2;
    }

    public string column1
    {
        get
        {
            return this.column1;
        }
        set
        {
            this.column1 = value;
        }
    }

    public string column2
    {
        get
        {
            return this.column2;
        }
        set
        {
            this.column1 = value;
        }
    }
}

Everything works fine and the DataGridView gets populated with the correct data, but now I want to hide the column2. I tried adding [Browsable(false)] above the column declaration, that will hide it, but I also need to access the column value from code, and when I use [Browsable(false)] and try to read the content it acts like if the column doesn't exist. If I don't use it I can read the column without problem but it's visible in the DataGridView.

How could I hide the column but still be able to read its content from code?




You will need to set AutoGenerateColumns to false on the datagrid and then bind the two columns that you created in the datagrid to the corresponding columns in the datasource.




Hidden certain column header in C# datagridview

You will need to handle the DataGridView.CellPainting Event.

In the event handler you will be given an instance of DataGridViewCellPaintingEventArgs. You can use the ColumnIndex and RowIndex properties of this object to determine if a header cell you want to hide is being painted. RowIndex will be -1 if the cell being painted is a column header.

It may just be a simple matter of doing nothing except e.Handled = true; when the header cell in question is being painted.




From your question, I would imagine you don't want to show certain "columns" within the datagridview? If so, use the Columns property to add and remove any automatically created columns that are found on the datasource which you use to attach to the grid.

The DataGridView by default will create columns for all public properties on the underlying data source object. So,

public class MyClass
{
   private string _name;

   public string Name
   {
      get{ return _name; }
      set { _name = value; }
   }

   public string TestProperty
   {
      { get { return "Sample"; }
   }
}

...
[inside some form that contains your DataGridView class]

MyClass c = new MyClass();

// setting the data source will generate a column for "Name" and "TestProperty"
dataGridView1.DataSource = c;

// to remove specific columns from the DataGridView
// dataGridView1.Columns.Remove("TestProperty")



c# Hide a property in datagridview with datasource

If you are adding the columns yourself... don't add the columns you don't want.

If you have AutoCreateColumns enabled, then:

  • if it is a class-based model, add [Browsable(false)] to properties you don't want
  • or set the column's .Visible to false
  • or simply remove the columns you don't want afterwards



Links



Tags