Search code examples
c#winformsdatagridviewdatagridviewcolumn

How to change datadridview columns width that are placed on different TabControl pages


I have a TabControl on my Form with two pages. Each of pages contains a datagridview. On the Form.Load event I am filling my datagridviews from SQL the database and changing some of the column widths for those grids.

 DataTable GLOBAL_TABLE = new DataTable();
    object[] GLOBAL_PARAMETERS = new object[50];

    private void frmMAIN_Load(object sender, EventArgs e)
    { 
        LOAD_TAB_1();
        LOAD_TAB_2();
    }

    void LOAD_TAB_1()
    {
        //SQLRELATION class contain method that execute stored procedure on server 
        //and return DataTAble
        Array.Clear(GLOBAL_PARAMETERS, 0, GLOBAL_PARAMETERS.Length);
        GLOBAL_PARAMETERS[0] = userID;
        GLOBAL_PARAMETERS[1] = date_1.Date;
        GLOBAL_PARAMETERS[2] = date_2.Date;
        GLOBAL_TABLE = SQLRELATION.GET_PROCEDURE("PROC023", GLOBAL_PARAMETERS, true);
        dgv_MAIN_substitution.DataSource = GLOBAL_TABLE;

        //Here I'm changing my columns width
        foreach (DataGridViewColumn col in dgv_MAIN_substitution.Columns)
        {
            switch (col.Name)
            {
                case "Dummy": col.Width = 30; break;
                case "MOQ": col.Width = 30; break;
                case "Inactive": col.Width = 30; break;
                default: break; 
             }
         }
    }

    void LOAD_TAB_2()
    {
        //The same I'm doing for the second datagridview 
        Array.Clear(GLOBAL_PARAMETERS, 0, GLOBAL_PARAMETERS.Length);
        GLOBAL_PARAMETERS[0] = userID;
        GLOBAL_PARAMETERS[1] = date_1.Date;
        GLOBAL_PARAMETERS[2] = date_2.Date;
        GLOBAL_TABLE = SQLRELATION.GET_PROCEDURE("PROC023", GLOBAL_PARAMETERS, true);
        dgv_MAIN_orders.DataSource = GLOBAL_TABLE;

        //Changing my columns width for second datagrigview
        foreach (DataGridViewColumn col in dgv_MAIN_orders.Columns)
            {
                switch (col.Name)
                {
                    //Error if HERE!
                    case "Status": col.Width = 30; break;
                    case "PO Number": col.Width = 150; break;                        
                    default: col.Width = 40; break;
                }        
             }
      }

Both of my datagridviews are getting data from database. For my first datagridview everything is OK. But when I try to change column width for the second datagridview on second TabPage, I get a message Object reference not set to an instance of an object. But why? On screenshot below, you can see that the column name exists for the second datagridview, but I can't change the column widths for it.

Error screenshot

What I'm doing wrong? My datagrid's are not empty and contains data. Should I first active my TabPage or something else?


Solution

  • I couldn't reproduce your problem when I tried it out my self. However, If you know your column names before hand, I would just use this approach:

    dgv_MAIN_substitution.Columns["Dummy"].Width = 30;
    dgv_MAIN_substitution.Columns["MOQ"].Width = 30;
    dgv_MAIN_substitution.Columns["Inactive"].Width = 30;
    

    and

    dgv_MAIN_orders.Columns["Status"].Width = 30;
    dgv_MAIN_orders.Columns["PO Number"].Width = 150;
    

    which is cleaner than the foreach loop and switch statement. Another thing you could do is set the Column.AutoSizeMode:

    dgv_MAIN_orders.Columns["PO Number"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
    

    so that it automatically adjusts your widths without your intervention.