469,140 Members | 1,205 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

How to set the dataGrid height to fit exactly ?

I have a DataGrid that contains a few rows and column header.
I wish to change the DataGrid height to be the exact height of the total
height of the rows and column header, so that no gray area will be shown
below the last row.

I tried:

int totalRowsHeight = dataTable.Rows.Count *
(dataGridTableStyle.PreferredRowHeight + 1);
Size gridClientSize = dataGrid.ClientSize;
gridClientSize.Height = totalRowsHeight;
dataGrid.ClientSize = gridClientSize;

But it causes the DataGrid to be too small.

Can anybody tell me how can I do that ?
--------
Thanks
Sharon
Jun 7 '06 #1
3 21636
Hi Sharon,

Thank you for your post!

Based on my understanding, you're trying to set the DataGrid's height to
fit with the last row to make sure there's no unused space in the grid. And
I assume you're using Visual Studio 2003's DataGrid. If I've misunderstood
anything, please feel free to post here.

DataGrid has a method 'GetCellBounds' which will return a cell's bounding
Rectangle in the client area of DataGrid. We can get the last row's first
cell's rectangle and use this to adjust the client size of DataGrid.

Rectangle r = dataGrid1.GetCellBounds(rowCount - 1, 0); // rowCount is from
your dataSource
Size s = dataGrid1.ClientSize;
s.Height = r.Y + r.Height;
dataGrid1.ClientSize = s;

Hope this helps. If anything is unclear, please feel free to post here.

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 8 '06 #2
Yes, you did understood me correctly. And yes it's working as follow:

Rectangle bottomRowCell = datagrid.GetCellBounds(sourceDataTable.Rows.Count
- 1, 0);
Size gridClientSize = m_datagrid.ClientSize;
gridClientSize.Height = bottomRowCell.Y + bottomRowCell.Height + 3;
datagrid.ClientSize = gridClientSize;

I also found another solution that is a bit more complicated:

// Retrieving the DataGridRows method of the m_dataGridFilterData by using
reflection.System.Reflection.MethodInfo mi =
datagrid.GetType().GetMethod("get_DataGridRows",
System.Reflection.BindingFlags.FlattenHierarchy |
System.Reflection.BindingFlags.IgnoreCase |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static);
// Invoking the DataGridRows method of the datagrid to get its rows.
System.Array dgra = (System.Array)mi.Invoke(datagrid, null);
// Retrieving the Height property of the row by using reflection.
System.Reflection.PropertyInfo pi =
dgra.GetValue(0).GetType().GetProperty("Height");
// Invoking the Height property of the row to get it height.
int rowHeight = (int)pi.GetValue(dgra.GetValue(0), null);
// Calculating the total height of the rows and column header.
int totalRowsHeight = 3 + (sourceDataTable.Rows.Count + 1) * rowHeight;
// Setting the new calculated height to the m_dataGridFilterData.
Size gridClientSize = datagrid.ClientSize;
int prevDataGridHeight = gridClientSize.Height;
gridClientSize.Height = totalRowsHeight;
datagrid.ClientSize = gridClientSize;

--------
Thanks
Sharon
Jun 8 '06 #3
Hi Sharon,

Thank you for your update.

I'm glad the suggestion worked. Also thank you for contributing another way
to achieve the same objective. I think this will benefit the community as
well.

Have a nice day!

Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Billy Jacobs | last post: by
1 post views Thread by Stephen | last post: by
reply views Thread by Stephen | last post: by
8 posts views Thread by Alejandro Penate-Diaz | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.