473,322 Members | 1,610 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,322 software developers and data experts.

changing datagrid column width

Can I add a column to the datagrid and also resize it? I add them like this:

DataTableAddress.Columns.Add("Col1", typeof(string));
DataTableAddress.Columns.Add("Col2", typeof(string));
DataTableAddress.Columns.Add("Col3", typeof(string));

I'd like to add to resize them without using a datacolumn because there's
over 30 columns and because the application is working well already (I don't
know how the datacolumn additions will affect the application).

Thanks,
Amos
Nov 16 '05 #1
1 3210
You'll need to create a DataGridTableStyle object and add it to the
TableStyles collection of the data grid. Here's an example that changes
the width of all of the columns.

DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "table name"; // replace table name with the name of the
table who's style you want to modify

foreach (DataColumn dc in table.Columns) // where table is an existing
DataTable
{
DataGridColumnStyle dgc = new DataGridTextBoxColumn();

dgc.MappingName = dc.ColumnName;
dgc.HeaderText = dc.ColumnName;
dgc.Width = 25;

dgts.GridColumnStyles.Add(dgc);
}

this.dataGrid1.TableStyles.Add(dgts);

Check out
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemwindowsformsdatagridtablestyleclasstopi c.asp for more details on
the DataGridTableStyle class and its properties.

hth

-Joel
--------------------
Reply-To: "Amos" <am***@earthlink.net>
From: "Amos" <am***@earthlink.net>
Subject: changing datagrid column width
Date: Mon, 5 Apr 2004 15:14:57 -0400
Lines: 14
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Message-ID: <uz**************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 66.50.252.112
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftn gp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:235329
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Can I add a column to the datagrid and also resize it? I add them like this:
DataTableAddress.Columns.Add("Col1", typeof(string));
DataTableAddress.Columns.Add("Col2", typeof(string));
DataTableAddress.Columns.Add("Col3", typeof(string));

I'd like to add to resize them without using a datacolumn because there's
over 30 columns and because the application is working well already (I don'tknow how the datacolumn additions will affect the application).

Thanks,
Amos

--------------------------------------------------------------------
This reply is provided AS IS, without warranty (express or implied).
Nov 16 '05 #2

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

Similar topics

1
by: Amos | last post by:
Can I add a column to the datagrid and also resize it? I add them like this: DataTableAddress.Columns.Add("Col1", typeof(string)); DataTableAddress.Columns.Add("Col2", typeof(string));...
9
by: web1110 | last post by:
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...
5
by: VB Programmer | last post by:
I have a simple datagrid on a webform. (I'm using VB.NET.) In the page load I get some data, set the .DataSource property of the dg, then do a .DataBind. The columns are automatically created. ...
1
by: martin | last post by:
Hi, I have a datagrid that contains 3 colums. This is rendered to the page fine, except that I would like to be able to control the width of each table cell of the datagrid I have the following...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
3
by: Aziz | last post by:
1. I have a shopping basket DataGrid with a list of products. What I want to do is when the user clicks on a row, a button will become visible/be created that allows user to edit the quantity. The...
1
by: Veeves | last post by:
I would like to change the width of a datagrid column at run time. I have noticed when creating a datagrid, if the header text is shorter than the data in the column, then the column width set by...
2
by: cj | last post by:
I was looking over some of my 2003 code today (see below) that loads a foxpro table via oledb connection. I used a sub "autosizecolumns" I found on the web but I never quite understood why they...
2
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.