views:

364

answers:

3

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

  • How to set width of all three columns sender,time,Message?
+1  A: 

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

Steven A. Lowe
@steven how to use autosize?
Pandiya Chendur
@[Pandiya Chendur]: see addendum
Steven A. Lowe
@Steven nothing seems to work for me... Please help me out..
Pandiya Chendur
@[Pandiya Chendur]: edit your question to post what you've tried, and I'll see what I can do. Is DataGridView not an option? It's much easier to work with than DataGrid...
Steven A. Lowe
A: 

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

rip
@rip its is datagrid not a datagridview
Pandiya Chendur
A: 

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.

shahkalpesh
@shahkalpesh there is no property Columns for datagrid.. I ve checked it...
Pandiya Chendur
@shahkalpesh still i find it difficult to get it working
Pandiya Chendur