2
votes

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

  • How to set width of all three columns sender,time,Message?
3
I have modified the answer to include some code. Post your comment underneath it for me to know, if it was useful. - shahkalpesh

3 Answers

2
votes

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

0
votes

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

0
votes

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.