CurtisHx
CurtisHx

Reputation: 758

Cannot assign datagridview.columns[i].Width property

I'm writing a method that will resize the columns in a datagridview so that all the data is visible, and the columns completely fill the datagridview. What I'm doing is:

1) Set the DataGridViewAutoSizeColumnsMode to AllCells. This will show all the data in the cells.

2) Calculate the pixel distance between the right edge of the last column and the right edge of the data grid view.

3) Divide that distance by the number of columns. This gives me the width that should be added to each column to completely fill the datagridview.

4) Add the width from (3) to each column.

Theoretically, this will display all the data in the columns, and completely fill the datagridview.

With the DataGridViewAutoSizeColumnsMode set to fill, the column widths are all equal. However, not all of my data has equal width. So with the ColumnsMode set to fill, I have data running off the edge in some cells, and a bunch of white space in other cells. But set to AllCells, and I wind up with a big gap between the right most column and the right edge of the datagridview.

On to the problem: I can't seem to set the column width property. I do realize that I haven't considered the case where the total column width is greater than the datagridviewwidth.

private void autoSizeDGV(DataGridView dgv)
    {
        try
        {
            //Step 1
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            //Step 2
            int totalcellwidth = 0;
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                totalcellwidth += dgv.Columns[i].Width;
            }
            //Step 3
            int widthtoadd = (dgv.Width - totalcellwidth)/dgv.ColumnCount;
            //Step 4
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                Debug.WriteLine("previous dgv.Columns[" +i+ "]: " + dgv.Columns[i].Width);
                dgv.Columns[i].Width += widthtoadd;
                Debug.WriteLine("after dgv.Columns[" + i + "]: " + dgv.Columns[i].Width);
            }
        }

Upvotes: 3

Views: 3670

Answers (1)

CurtisHx
CurtisHx

Reputation: 758

Well, I answered my own question. The dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; statement will override any non-user changes to the column widths. So trying to programically change the widths is a no-go. But by using dgv.AutoResizeColumns(); in its place, everything works.

So, the correct method is:

private void autoSizeDGV(DataGridView dgv)
    {
        try
        {
            //Step 1
            dgv.AutoResizeColumns();

            //Step 2
            int totalcellwidth = 0;
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                totalcellwidth += dgv.Columns[i].Width;
            }
            //Step 3
            int widthtoadd = (dgv.Width - totalcellwidth) / dgv.ColumnCount;
            //Step 4
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                Debug.WriteLine("previous dgv.Columns[" + i + "]: " + dgv.Columns[i].Width);
                dgv.Columns[i].Width += widthtoadd;
                Debug.WriteLine("after dgv.Columns[" + i + "]: " + dgv.Columns[i].Width);
            }
        }

Upvotes: 2

Related Questions