ACP
ACP

Reputation: 35268

Set cell width of a Datagrid in a c# windows application

I have a datagrid in my c# windows application and my code is

private void BindGrid(SmsPdu pdu)
 {
   DataRow dr=dt.NewRow();
   SmsDeliverPdu data = (SmsDeliverPdu)pdu;
   dr[0]=data.OriginatingAddress.ToString();
   dr[1]=data.SCTimestamp.ToString();
   dr[2]=data.UserDataText;
   dt.Rows.Add(dr);
   dataGrid1.DataSource=dt;
  }

And my datagrid looks like this alt text http://www.freeimagehosting.net/uploads/c368f82e0e.jpg

Upvotes: 2

Views: 3440

Answers (3)

shahkalpesh
shahkalpesh

Reputation: 33474

How about datagrid1.Columns[0].Width?

Look at this class. It has a width property that you can set.

EDIT: Look at this page. And look at the code under AddGridStyle, which shows how to create the mapping & set each of the column style, width etc.

Hope that helps.

EDIT2: I am writing the following code without compiler (just using reflector & MSDN to look at the documentation). So, please be kind

DataGridTableStyle tableStyle = dataGrid1.TableStyles[0];
GridColumnStylesCollection colStyles = tableStyle.GridColumnStyles[0];

DataGridColumnStyle styleForCol1 = colStyles[0];
styleForCol1.Width = 165;

DataGridColumnStyle styleForCol2 = colStyles[1];
styleForCol1.Width = 125;

The code is derived from what I understood from this page under Remarks , which is quoted below

The System.Windows.Forms..::.DataGrid control automatically creates a collection of DataGridColumnStyle objects for you when you set the DataSource property to an appropriate data source. The objects created actually are instances of one of the following classes that inherit from DataGridColumnStyle: DataGridBoolColumn or DataGridTextBoxColumn class.

Upvotes: 0

Steven A. Lowe
Steven A. Lowe

Reputation: 61233

the simple way: use autosize

otherwise, use the Columns collection to set the size of each column

ADDENDUM:

sorry, I assumed you were using DataGridView, since it replaced DataGrid in .NET 2.0

for DataGrid, it's a little more complex - but google knows all!

http://www.syncfusion.com/faq/windowsforms/search/1004.aspx

Upvotes: 2

Carl Rippon
Carl Rippon

Reputation: 4673

There is a Width property on the Columns collection:

DataGridView1.Columns(X).Width = Y

Where X is the name or index of the column and Y is the width

Upvotes: 0

Related Questions