0

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?

Holger Brandt
  • 4,324
  • 1
  • 20
  • 35
mbigun
  • 1,304
  • 4
  • 19
  • 46

4 Answers4

7

I had the same problem. Apparently the crash occurs because control isn't displayed. I set widths in the form's Shown() event and then it worked for me.

Hintz
  • 71
  • 1
  • 1
4

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.

Moop
  • 3,414
  • 2
  • 23
  • 37
  • Thanks a lot! It works in this case: col.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; But anyway, it's very strange why I can't to change column widths like col.Width = 30. – mbigun Aug 08 '12 at 15:26
0

I am using this code to set column widths, and then later storing the widths by user Public Sub SetGridColWidthsAuto(ByRef DG As DataGridView) ' Set all columns in supplied Datagrid to auto size. ' Set the column width property ' Set them back to not auto sized, so the user can resize them

    Try

        Dim MyWidth As Integer

        DG.EndEdit()

        ' Auto Fit
        For i = 0 To DG.Columns.Count - 2
            DG.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        Next i

        ' Set last column to AutoFill
        DG.Columns(DG.Columns.Count - 1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

        DG.Refresh()

        ' Set width to current width, and set to not auto fill
        For i = 0 To DG.Columns.Count - 1
            MyWidth = DG.Columns(i).Width
            DG.Columns(i).AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            DG.Columns(i).Width = MyWidth
        Next

    Catch ex As Exception
        MsgBox("Error in function <SetGridColWidthsAuto>:" + vbCrLf + ex.Message)
    End Try
End Sub

However i have discovered that it doesn't work unless the tab control is selected via selected index.

From load or Shown. OF course i have tried refresh of the DG sand tab control, and entire form, and update. There is some kind of flakeyness in there

  • I wanted to add the comment that the code works perfectly when initiated from a button on the toolbar. It just wont run from load shown or activated event correctly unless the TAB is selected. – ScopeCreep Apr 02 '23 at 17:12
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 09 '23 at 11:33
-1

I had the same exception when I tried to resize the columns by this code:

this.DGVproOrder.Columns[0].Width = 86;
this.DGVproOrder.Columns[1].Width = 166;
...

The problem was because I set the data grid view property auto size columns mode to fill to fill the container but when I set it back to null every thing worked great.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Desha
  • 1