Answered by:
Auto Sizing DataGridView to Fit XML Table

Question
-
I'm loading a windows form dataGridView with a table saved on an .xml
doc. The table loads and edits fine. I have other buttons and
controls on the form, so I wouldn't like to dock the table to the
entire form. I'd just like to know if there's a way to auto-fit the
dataGridView window inside my form to the table on the .xml doc or
vice-versa.Thanks in advance for any help
Tuesday, August 21, 2007 1:37 PM
Answers
-
Hi,first I will confirm my understanding of the issue with you,if there is any misunderstanding or inconsistency,please let me know.You want to show have an autosize datagridview,you can try following code.
What the code does is first recalculate all columns' width (if you have visible columns). But, at the end, you could have rounding errors and still have a scrollar if the last column is 1 or more pixels too wide. So, at the end you calculate the exact width needed for the last column.
Code Snippetpublic void ResizeGrid(DataGridView dataGrid, ref int prevWidth)
{
if (prevWidth == 0)
prevWidth = dataGrid.Width;
if (prevWidth == dataGrid.Width)
return;int fixedWidth = SystemInformation.VerticalScrollBarWidth +
dataGrid.RowHeadersWidth + 2;
int mul = 100 * (dataGrid.Width - fixedWidth) /
(prevWidth - fixedWidth);
int columnWidth;
int total = 0;
DataGridViewColumn lastVisibleCol = null;for (int i = 0; i < dataGrid.ColumnCount; i++)
if (dataGrid.Columns[i].Visible) {
columnWidth = (dataGrid.Columns[i].Width * mul + 50) / 100;
dataGrid.Columns[i].Width =
Math.Max(columnWidth, dataGrid.Columns[i].MinimumWidth);
total += dataGrid.Columns[i].Width;
lastVisibleCol = dataGrid.Columns[i];
}
if (lastVisibleCol == null)
return;
columnWidth = dataGrid.Width - total +
lastVisibleCol.Width - fixedWidth;
lastVisibleCol.Width =
Math.Max(columnWidth, lastVisibleCol.MinimumWidth);
prevWidth = dataGrid.Width;
}For more information,please check this
Friday, August 24, 2007 6:48 AM
All replies
-
Hi,first I will confirm my understanding of the issue with you,if there is any misunderstanding or inconsistency,please let me know.You want to show have an autosize datagridview,you can try following code.
What the code does is first recalculate all columns' width (if you have visible columns). But, at the end, you could have rounding errors and still have a scrollar if the last column is 1 or more pixels too wide. So, at the end you calculate the exact width needed for the last column.
Code Snippetpublic void ResizeGrid(DataGridView dataGrid, ref int prevWidth)
{
if (prevWidth == 0)
prevWidth = dataGrid.Width;
if (prevWidth == dataGrid.Width)
return;int fixedWidth = SystemInformation.VerticalScrollBarWidth +
dataGrid.RowHeadersWidth + 2;
int mul = 100 * (dataGrid.Width - fixedWidth) /
(prevWidth - fixedWidth);
int columnWidth;
int total = 0;
DataGridViewColumn lastVisibleCol = null;for (int i = 0; i < dataGrid.ColumnCount; i++)
if (dataGrid.Columns[i].Visible) {
columnWidth = (dataGrid.Columns[i].Width * mul + 50) / 100;
dataGrid.Columns[i].Width =
Math.Max(columnWidth, dataGrid.Columns[i].MinimumWidth);
total += dataGrid.Columns[i].Width;
lastVisibleCol = dataGrid.Columns[i];
}
if (lastVisibleCol == null)
return;
columnWidth = dataGrid.Width - total +
lastVisibleCol.Width - fixedWidth;
lastVisibleCol.Width =
Math.Max(columnWidth, lastVisibleCol.MinimumWidth);
prevWidth = dataGrid.Width;
}For more information,please check this
Friday, August 24, 2007 6:48 AM -
Thanks Gavin,
Your help is very much appreciated. I wasn't trying to do exactaly the same thing you were suggesting (I didn't exactaly know how to ask), but I was able to use your algorythem to implement my concept. I simply needed to find the number of rows and columns in a DataTable and increment my DataGridView accordingly (taking in account the scroll bars of curse). Here's what I made out of your method:
Code Snippetpublic static void FitToTable(ref DataGridView grid, DataTable table)
{
int i = 0, j = 0;
//Calculate table height
grid.Height = grid.ColumnHeadersHeight + 2;
grid.Height += grid.Rows[i].Height;
grid.Height +=
//Calculate table width
grid.Width = grid.RowHeadersWidth + 2;
grid.Width += grid.Columns[j].Width;
grid.Width +=
}
I thought there was System method to perform this task, but I guess you can't expect Microsoft to do everything for you.
Thanks again for showing me the light, Gavin.
Tuesday, August 28, 2007 3:23 PM