473,588 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

programmaticall y 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 6738
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
DataGridViewCol umn'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
CellValueChange d 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
2439
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: DataGridView1.AutoSizeRows(DataGridViewAutoSizeRowsMode.HeaderAndColumnsAllRows) (source MSDN artice: Introducing a New Data Grid, MacDonald)
1
2550
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 display statistics on a large LCD display for viewing by people within the room (30 feet away). One problem is that the screen can obviously only display so much legible data at a time. I may have upwards of 60 or 70 rows to display on a rotating...
3
22140
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. I wish to populate, by default, all of the ComboBoxes with the same list of three choices. How do I add the DataGridViewComboBoxColumn, and do I first populate one as a template for the others? I will appreciate any help.
4
15336
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 another page, the widths are set as they should be. Going back to page one, the widths are still correct. Not a clue what's going on here!
5
3503
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 needed. For simplicity the DGV has 7 columns and 15 rows. The 15 rows are populated and the DGV is made visible. Everything is just fine. But now I want rows 6,7 and 11,12 to have different column spacing to the other rows.
3
1962
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 and what not, but I was hoping there would be a much easier solution. It seems like such an obvious requirement, but I can find any articles on how to do it. Many thanks to anyone who can advise
0
2168
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 which I use to programmatically set *other* r/o cell values for the currently selected grid row. So, after that click on another control, and stuffing the related cell values, I want to seamlessly allow the user to resume editing/adding that row...
4
36378
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 width wide enough so that you don't have to scroll to the right. I want to know if there is a property to resize the width programmatically in case I add or remove columns. thanks
0
2450
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 takes about 3 seconds during which time I can see the bottom scroll bar shinking more and more till its done. I've tried putting "DataGrideView.SuspendLayout();" "DataGridView.ResumeLayout();" around the "AutoSeizcolumnsMode = Fill;" but I still...
0
7862
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
8223
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...
1
5729
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
5398
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();...
0
3847
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2372
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
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1196
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.