Autosize + datagrid

G

Guest

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: (e-mail address removed)
Móvel: +351 966931639
Telefone: +351 239708708
Fax: +351 239708701
Tel. Directo: +351 239708705 ext. 401
 
P

Puneet Taneja

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Autosize + Datagrid 1
Autosize + Datagrid 1
DataGrid Collumn/Cell Color 1
IP 1
Running Program 8
DataGrid 2 1
Query Timeout 3
TextBox 2

Top