Programmatically size a DataGridView
-
lundi 31 juillet 2006 14:16
Hi,
I posted this in the 'windows forms data controls & databinding forum' but didn't get any response -- so am trying here as well.
i'd like to set up a DataGridView with:
(a) just a horizontal scroll bar at design time
(b) porgrammatically size it vertically to accomodate all rows plus the column header, unless the number rows exceeds N, in which case, programmatically add a vertical scroll bar. The datagridview will not be bound to any data-source. Instead i programmatically add rows to its (initially empty) DataGridViewRowCollection.
I have experimented with trying to programmatically determine the height to set it to with the following code:
dgv.height = dgv.rows.GetRowsHeight(DataGridViewElementStates.None) + dgv.columnHeadersHeight
// where dgv is my DataGridView instance
Anyway, this does not seem to work - it falls short of the height it should be.
As such, any idea how i'd do (b)?
Thanks,
Mark
Toutes les réponses
-
vendredi 25 août 2006 21:15Is is possible that it is off just by some constant value (like the size of the borders, etc)? Then you could just add the constant value to get the correct height.
-
lundi 18 septembre 2006 19:07I wrote this to dynamically resize the width of the datagridView control
Private Sub ResizeDgVarGrid()
Dim iWidth As Integer = 0
Dim maxWidth as Integer=527
For i As Integer = 0 To dgVarData.Columns.Count - 1
iWidth += dgVarData.Columns(i).Width
Next
If (iWidth + 50) >= maxWidth Then
Me.dgVarData.ScrollBars = ScrollBars.Horizontal
Else
Me.dgVarData.ScrollBars = ScrollBars.None
End If
End Sub
I'm sure a similar method would work for height. I have a max width that I have set for form limitation, the '50' is to prevent the horizontal scrollbar appearing
Hope this helps -
mercredi 7 novembre 2007 13:40
I put together this basic control to do this for me. It is nothing fancy and I am sure there are sizing issues based on font selected. The application I wrote this for is a very specific usage. That being said you could take this and use it as a starting point to make a more robust control to take into account resolution and font.
Basic usage of this is set the "Rows to Display" property either programatically or at design-time. If you specifiy a number of rows to display it will size the grid to display at max that number of rows, if the row count exceeds that number it forces scroll. If you set "Rows to Display" to zero the grid will function normally in this regard.
Code Block
public partial class FormattedGridView : DataGridView { private bool __FirstPaint = true; private int __RowsToDisplay; public FormattedGridView() {InitializeComponent();
}
public int RowsToDisplay { get { return __RowsToDisplay; } set { __RowsToDisplay = value; }}
/// <summary> /// Adds a confirmation box on deletes. /// </summary> private void GridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) { if (!e.Row.IsNewRow) { DialogResult response = MessageBox.Show("Are you sure you want to delete this record?", "Delete Record?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (response == DialogResult.No) {e.Cancel =
true;}
}
}
private int GetGridHeight() { int x = 0; foreach (DataGridViewRow row in Rows) {x += row.Height;
}
x += ColumnHeadersHeight;
x += 2;
return x;}
private int GetMaxGridSize() { int singleRowHeight = Rows[0].Height; if (__RowsToDisplay == 0) { return 300;}
else { return singleRowHeight*__RowsToDisplay + ColumnHeadersHeight + 2;}
}
private void GridView_Paint(object sender, PaintEventArgs e) { if (__RowsToDisplay == 0) return; if (__FirstPaint) {__FirstPaint =
false;MaximumSize =
new Size(1024, GetMaxGridSize());}
Height = GetGridHeight();
}
}
-
dimanche 5 août 2012 21:46
Im not a 100% sure but u cant show(if set in properties menu) Scrollbars before the number of rows or columns exceeds that of the DGV(DataGridView) height or width, having said that, you will have to find a way to make the DGV grow maybe 1 row before before the last row reaches the bottom of the grid until a certain height, after that the scroll bar will automatically pop up if u set the scroll bar property to vertical but in your case you will want it set to both and that way the bars pop up automatically because u have allowed the rows to reach the bottom of the grid
Public Class Form1 Dim i As Integer Dim cellwidth As Integer Dim cellheight As Integer Private Sub AddRowsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddRowsButton.Click If DataGridView1.Columns.Count < 1 Then DataGridView1.Columns.Add("", "") cellheight += DataGridView1.Rows(i).Height Label1.Text = cellheight.ToString Else i += 1 DataGridView1.Rows.Add(1) cellheight += DataGridView1.Rows(i).Height Label1.Text = cellheight.ToString If Not DataGridView1.Height = 400 Then 'this is the point where you want the Grid to stop growing If cellheight > DataGridView1.Height - 20 Then ' This is how fine you want the bottom row to reach before grid grow but 'watch how u set it as they catch up with each other DataGridView1.Height += 20 End If End If End If End Sub Private Sub AddColumnsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddColumnsButton.Click DataGridView1.Columns.Add("", "") 'you can do the same for your columns End Sub End Class
Remember to enable scroll bars in properties other wise this theory will not work
as i do not know how to force scroll bars to appear before rows/columns have reached the botton/side of grid
Did not realize this was a 2006 post lol
- Modifié _Damien_ lundi 6 août 2012 17:22

