473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 21991
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul | last post by:
Hi, I'm wondering if someone out there can help me overcome this quandary. I need to be able to calculate the height of the current page. I need to calculate the height of the page in pixels after...
7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
1
by: Stephen | last post by:
I have a datagrid and i'm trying to fix the width of one of the columns to be exactly 300px. My problem is that one of the records fields(CompanyName) is really long and has no spaces so it...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
0
by: Stephen | last post by:
I have a datagrid and i'm trying to fix the width of one of the columns to be exactly 300px. My problem is that one of the records fields(CompanyName) is really long and has no spaces so it...
8
by: Alejandro Penate-Diaz | last post by:
I have a datagrid inside a table and I want to fix both (table and databrid) width to 700, but when the content of datagrid is too large the width increases automatically sometimes and sometimes...
5
by: sianan | last post by:
I have an ASP.NET custom control, which contains a DataGrid with checkboxes. I am trying to figure out how to retrieve the selected records, based on the checkbox's checked state. I want to send...
1
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
4
by: skOOb33 | last post by:
I successfully autosized the columns and rows on my Datagrid, and am now facing another issue. Having the sorting ability by clicking the column headers is key, but when I do that, it resizes all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.