473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

programmatically setting DataGridView column widths

I am wondering what is considered a best practice for setting column widths
to fit the data for a DataGridView (.Net 2.0) that was created on-the-fly
from an arbitrary query. What I would probably do is scan the width of each
column for the first couple hundred or so rows and find the largest. (But how
does one convert character counts to field widths in pixels?)

More generally, however, is there a better approach to accomplish this?
Jan 24 '08 #1
3 6721
Linda's suggestion solved my issue, in that upon loading a result set the
displayed columns adjusted to the data automatically.

I am curious about Andrus' point also, however. When I ran the sample code
it took about 30 seconds to resize the column after double-clicking the
column header separator, as he indicated. Note that this is a somewhat
different case--it allows the user to resize columns to the data on demand
rather than automatically.

Jan 28 '08 #2
Michael,
I am curious about Andrus' point also, however. When I ran the sample code
it took about 30 seconds to resize the column after double-clicking the
column header separator, as he indicated. Note that this is a somewhat
different case--it allows the user to resize columns to the data on demand
rather than automatically.
Nicholas Paldino wrote in other thread, that immediate resize should occur
and requested code to reproduce.
I created this code to reproduce the issue.

This sample demonstates that for Virtual DataGridView containing large
number of rows, automatic column resize based on visible rows is not
possible.

Is this DataGridView bug ?

Andrus.
Jan 28 '08 #3
Hi Michael,

Thank you for your reply!

(1) In the workaround I gave you in my previous reply, once a
DataGridViewColumn's width reaches a given maximum, this column's
AutoSizeMode property is set to None, which means this column will not
autosize with its cell content unless its AutoSizeMode property is set to
NotSet again.
I think in my situation this could be simplified because I only need to
size the
columns when I load the DataGridView with data, though this could happen
many times during a given run.
If the data source bound to the DataGridView remains the same and you only
load data into the data source many times, you do need to handle the
CellValueChanged event of the DataGridView in case that the actually
required column widths become shorter than the given maximum value in the
later data loading.

(2) The workaround I provided in my previous reply has already met this
requirement. Once a column autosizes up to a given maximum, its
AutoSizeMode property is set to None so that the user can grab the column
separator to resize the column manually as he/she likes.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Feb 1 '08 #4

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

Similar topics

0
by: Jyrki | last post by:
Hello, I have a small problem in regards to DataGridView component. I would like to know how to set row heights in a DataGridView. Sofar, I have used command: ...
1
by: Jay | last post by:
I'm wondering if anyone has even encountered a control or other scripting that will automatically rotate the rows of a datagrid... I am in the process of building an asp.net application that will...
3
by: Rain County | last post by:
I am programmatically building and populating a table and then making it the data source for a dataGridView. I want to add a column to the dataGridView which will be a DataGridViewComboBoxColumn. ...
4
by: Chuck | last post by:
I'm setting the column with for a gridview (25+- columns) and have paging turned on. When the gridview is first displayed, the column widths are all set to the default. But after paging to...
5
by: Roger | last post by:
I am trying to do a 'simple' task in a DGV control. The problem is this. I have an unbound DGV with all 'AllowUser' options disabled. RowHeader.Visible=False ColumnHeader.Text is labelled as...
3
by: Simon Harvey | last post by:
Hi all, Can anyone tell me if there is an easy way to retain per-user column widths for the datagridview in a windows forms application. I know I could do it with a lot of database programming...
0
by: RKT | last post by:
I have a DataGridView bound to an MS Access table. This is a single- user application. When the User is adding or editing a row, the User may click on a Control elsewhere. That Control has context...
4
by: vbDavidC | last post by:
I have an unbound grid that is on my form (not created programmatically). I stretched the size to what I think is wide enough to accomodate the 6 columns I will be creating. I plan on making the...
0
by: NvrBst | last post by:
By default I have it set to "DataGridView.AutoResizeColumns(DisplayedCells);" I let the user change to "DataGridView.AutoSizecolumnsMode = Fill;" at runtime with a button. When I do that it...
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:
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...
0
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,...
1
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.