468,780 Members | 2,268 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,780 developers. It's quick & easy.

Autosize + datagrid

How I can autosize a DataGrid after then associate a Datasource=Dataset in
Smart Devices applications or Windows forms application?? That same??

--
Cumprimentos,
David de Passos
--------------------------------------------------------------
RCSOFT, Lda.
E-Mail: pa****@rcsoft.pt
Móvel: +351 966931639
Telefone: +351 239708708
Fax: +351 239708701
Tel. Directo: +351 239708705 ext. 401
Jul 21 '05 #1
1 3158
Dear David,

One of the ways to autoresize columns in datagrid is to
simulate the behavior of the mouse double click on the
column header border. This solution works perfect unless
you want to write the entire code to resize the columns.

Solution:
Create a control inherited from the DataGrid control and
create a public method with following signature and
implementation:

public void AutoSizeColumn(int colNumber)
{
// The rect1 will contain the bounds of the zeroth cell
at the specified column.

System.Drawing.Rectangle rect1 = this.GetCellBounds(0,
colNumber);

// Basically we need to extract the x and y information
so that the same can be passed

// to the base.OnMouseDown call to simulate the behavior
of double click over a column.

int X = rect1.X + rect1.Width;

int Y = rect1.Y - 5;

// 5 can be taken as a constant as above the cell at
zeroth row of this column will be the column header.

// The height of the column header must be greater than 5.

// For more precision, you can retrieve the header font /
header text and then use Graphics class's

// measurestring method to calculate the height of the
header and then pass y as somewhere

// in the middle of the header's Y + Height.

System.Windows.Forms.MouseEventArgs me = new
System.Windows.Forms.MouseEventArgs
(System.Windows.Forms.MouseButtons.Left, 2, X, Y, 0);

base.OnMouseDown(me);
}
Finally call this method from your client application and
pass the column id of the column you want to autoresize.
Regards,
Puneet Taneja
-----Original Message-----
How I can autosize a DataGrid after then associate a Datasource=Dataset inSmart Devices applications or Windows forms application?? That same??
--
Cumprimentos,
David de Passos
--------------------------------------------------------- -----RCSOFT, Lda.
E-Mail: pa****@rcsoft.pt
Móvel: +351 966931639
Telefone: +351 239708708
Fax: +351 239708701
Tel. Directo: +351 239708705 ext. 401
.

Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jinx | last post: by
2 posts views Thread by Tim Wallace | last post: by
1 post views Thread by Silvia | last post: by
1 post views Thread by | last post: by
1 post views Thread by Gunjan Shah | last post: by
1 post views Thread by matthewtech | last post: by
8 posts views Thread by Jordi Rico | last post: by
1 post views Thread by SAL | last post: by
2 posts views Thread by =?Utf-8?B?Qm9i?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.