How to hide column of DataGridView when using custom DataSource?

前端 未结 7 1655
时光取名叫无心
时光取名叫无心 2020-12-08 20:07

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

相关标签:
7条回答
  • 2020-12-08 20:10

    I"m not sure if its too late, but the problem is that, you cannot set the columns in design mode if you are binding at runtime. So if you are binding at runtime, go ahead and remove the columns from the design mode and do it pragmatically

    ex..

         if (dt.Rows.Count > 0)
        {
            dataGridViewProjects.DataSource = dt;
            dataGridViewProjects.Columns["Title"].Width = 300;
            dataGridViewProjects.Columns["ID"].Visible = false;
        }
    
    0 讨论(0)
  • 2020-12-08 20:12

    Set that particular column's Visible property = false

    dataGridView[ColumnName or Index].Visible = false;

    Edit sorry missed the Columns Property dataGridView.Columns[ColumnName or Index].Visible = false;

    0 讨论(0)
  • 2020-12-08 20:19

    I had the same problem

    Here is the Solution that might work for you. It worked for me

        GridView1.DataBind();
    if (GridView1.Columns.Count > 0)
        GridView1.Columns[0].Visible = false;
    else
    {
        GridView1.HeaderRow.Cells[0].Visible = false;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            gvr.Cells[0].Visible = false;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 20:20

    Just set DataGridView.AutoGenerateColumns = false;

    You need click on the arrow on top right corner (in datagridview) to add columns, and in DataPropertyName you need to put a name of your property in your class.

    Then, after you defined your columns in datagridview, you can set datagridview.datasource = myClassViewModel.

    0 讨论(0)
  • 2020-12-08 20:27

    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"];
    
    0 讨论(0)
  • 2020-12-08 20:35

    In some cases, it might be a bad idea to first add the column to the DataGridView and then hide it.

    I for example have a class that has an NHibernate proxy for an Image property for company logos. If I accessed that property (e.g. by calling its ToString method to show that in a DataGridView), it would download the image from the SQL server. If I had a list of Company objects and used that as the dataSource of the DataGridView like that, then (I suspect) it would download ALL the logos BEFORE I could hide the column.

    To prevent this, I used the custom attribute

     [System.ComponentModel.Browsable(false)]
    

    on the image property, so that the DataGridView ignores the property (doesn't create the column and doesn't call the ToString methods).

     public class Company
     {
         ...
         [System.ComponentModel.Browsable(false)]
         virtual public MyImageClass Logo { get; set;}
    
    0 讨论(0)
提交回复
热议问题