Datagridview Column Width

enter image description here I have a problem with datagridview in Winform.

I have a list of table names in the left pane. When I click on a table, I show the contents of the table in the right pane. I display the data in a datagridview, getting the data and assigning a datasource to dgv.

I set the following property in dgv.

dgTemp.Dock = DockStyle.Fill; dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dgTemp.AutoSize = true; dgTemp.DefaultCellStyle.WrapMode = DataGridViewTriState.True; dgTemp.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dgTemp.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; dgTemp.ReadOnly = true; dgTemp.AutoGenerateColumns = true; dgTemp.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dgTemp.AllowUserToAddRows = false; 

My problem: there can be any number of columns in the Datasource that I assign to dgv. So if the number of columns is very small (say, 1 or 2), the dgv size is very small, and the empty space on the right side gives a very ugly look. I cannot use auto autosizecolumnmode to fill , because when there are more columns, all columns become squeezed and expanding columns prevent me from scrolling at the bottom

therefore my requirement

  • All space in the datagridview must be filled. (should cover the entire area)
  • When a larger column appears, a scroll will appear to make it look better.

Are there any events or properties that I can use?

Thanks pending.

+4
source share
1 answer

Try the following:

 dgTemp.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; 

Update:

  dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5 //you can have a horizontal scroll bar with this code : dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column 

Update 2:

 int rows = dataGridView1.Rows.Count; int columns = dataGridView1.Columns.Count; if (rows < 5 && columns < 10) { dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; } else { dataGridView1.FirstDisplayedScrollingRowIndex = 5; //use 5 because you want to start from 5 //you can have a horizontal scroll bar with this code : dataGridView1.FirstDisplayedScrollingColumnIndex = 10; //you can choose every column you wanna start with that column } 
+5
source

Source: https://habr.com/ru/post/1433558/


All Articles