473,883 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGrid Width

Hi y'all,

I have resized the columns in a DataGrid and I want to set the width of the
DataGrid to fit the columns. Just summing the column widths is too short
due to the grid and gray row selection column on the left.

I have the widths of the columns. What other values do I need to include in
the DataGrid width?

Thanx,
Bill
Nov 17 '05 #1
9 3952
JV
Are you talking about an ASP.NET datagrid or a Windows Datagrid?

"web1110" <we***@comcast. net> wrote in message
news:N_******** ************@co mcast.com...
Hi y'all,

I have resized the columns in a DataGrid and I want to set the width of
the
DataGrid to fit the columns. Just summing the column widths is too short
due to the grid and gray row selection column on the left.

I have the widths of the columns. What other values do I need to include
in
the DataGrid width?

Thanx,
Bill

Nov 17 '05 #2
A Windows DataGrid.
Nov 17 '05 #3
Since the DataGrid draws its own borders and the vertical scroll bar is a
child control that sits on top of the client area, you'll need to consider
the BorderStyle, RowHeaderWidth, column widths, and vertical scroll bar
width (if present). To retrieve the column widths, the code below assumes
that the DataGrid is using a custom DataGridTableSt yle. If this is not the
case then you may need to make some tweaks to the source.

using System.Reflecti on;

private void AutoSizeDataGri dWidth(DataGrid grid)
{
int width = 0;

// Calculate occupied border space.
switch (grid.BorderSty le)
{
case BorderStyle.Non e:
{
// Do nothing.
break;
}
case BorderStyle.Fix edSingle:
{
width += 4;
break;
}
case BorderStyle.Fix ed3D:
{
width += (SystemInformat ion.Border3DSiz e.Width * 2);
break;
}
}

// Calculate occupied row header space.
if (grid.TableStyl es.Count > 0)
{
if (grid.TableStyl es[0].RowHeadersVisi ble)
{
width += grid.RowHeaderW idth;
}
}
else if (grid.RowHeader sVisible)
{
width += grid.RowHeaderW idth;
}

// Calculate occupied column space.
if (grid.TableStyl es.Count > 0)
{
foreach (DataGridColumn Style column in
grid.TableStyle s[0].GridColumnStyl es)
{
width += column.Width;
}
}

// Calculate occupied vertical scrollbar space.
PropertyInfo propInfo = grid.GetType(). GetProperty("Ve rtScrollBar",
BindingFlags.In stance | BindingFlags.No nPublic);
if (propInfo != null)
{
ScrollBar propValue = propInfo.GetVal ue(grid, null) as ScrollBar;
if (propValue != null)
{
if (propValue.Visi ble)
{
width += propValue.Width ;
}
}
}

grid.Width = width;
}

--
Tim Wilson
..Net Compact Framework MVP

"web1110" <we***@comcast. net> wrote in message
news:N_******** ************@co mcast.com...
Hi y'all,

I have resized the columns in a DataGrid and I want to set the width of the DataGrid to fit the columns. Just summing the column widths is too short
due to the grid and gray row selection column on the left.

I have the widths of the columns. What other values do I need to include in the DataGrid width?

Thanx,
Bill

Nov 17 '05 #4
Thank you much.

Without a horizontal scroll bar present, I still had to add 3 to the width
to zap te horizonatl scroll bar, but all in all, it's pretty close.

Thanx,
Bill
Nov 17 '05 #5
er, I should add:

I had to add 3 to the width per column.
Nov 17 '05 #6
Huh. It all seemed to work fine for me.

--
Tim Wilson
..Net Compact Framework MVP

"web1110" <we***@comcast. net> wrote in message
news:u5******** ************@co mcast.com...
er, I should add:

I had to add 3 to the width per column.

Nov 17 '05 #7
Some mysteries will never be solved. Thanks.
Nov 17 '05 #8
Although I do have readonly set. Not worth investigating.
Nov 17 '05 #9
Even with ReadOnly set to true it still works as expected at my end. Odd
problem. Must be something set slightly different with the DataGrid,
DataGridTableSt yle, DataGridColumnS tyle's, or the calculation of the column
widths.

--
Tim Wilson
..Net Compact Framework MVP

"web1110" <we***@comcast. net> wrote in message
news:UZ******** ************@co mcast.com...
Although I do have readonly set. Not worth investigating.

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
7708
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 Impact Summary</td> </tr> <tr> <td colspan=2></td>
5
3384
by: Prateek | last post by:
Hi, How can I change the width of a column in datagrid control? TIA Prateek
2
2510
by: Josef Meile | last post by:
Hi, I'm using a ComboBox, some Textboxes, and a DataGrid to represent a many-to-many relationship between Person and Course. Each time that I change the value in the ComboBox (which for now is the OID of Person), the information of the person matching the selected OID is shown in the Textboxes (Name, Address, id, etc) and the courses this person is taken are shown in a DataGrid (course name, price, etc.). This is working well so far, I...
5
2091
by: John M | last post by:
Hello, In Visual Studio .NET 2003, How can I change the columns' width (at design or runtime) of datagrid ? Thanks :)
2
6421
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
4
1835
by: Steve | last post by:
I am fairly new to VB.NET, and I am rewriting an application I wrote a while back, also in VB.NET. I aplied some new things I learned. Anyway, here is my problem....... I have a custom DataGrid with a buttonRow that does a delete function for me. Microsoft support helped me back then to get this done. Here is part of the code that creates the dataGrid: Dim ButtonColStyle As DataGridButtonColumn
6
2754
by: Agnes | last post by:
I understand it is impossible, but still curious to know "Can I freeze several column in the datagrid, the user can only scroll the first 3 columns (not verical), for the rest of the coulumn, it is freeze.
10
2979
by: amiga500 | last post by:
Hello, I have one basic simple question. When I have multiple records in the datagrid as follows: Code Product 1 Product 2 Product 3 11111 A B C 22222 D E F 33333 G H I 44444 J K L
2
6600
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click in the child datagrid and then go to the combobox and choose another vendor. When the new vendor is loaded nothing shows in the datagrid but the itemsource shows the info is there. Know if I click on the child cell and then click back on the...
0
9793
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10419
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9577
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7974
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3237
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.