Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > I can't set the column width.....
 

I can't set the column width.....

I am going to show 2 columns only (phone, type), and I want to set the column width of phone, but I can't set it althought I've tried the post.

Here is my code.

Dim strSelect As String = "select autoid, id, phone, type from phone where id = '" & CboFiltered.SelectedValue & "'"

Dim DA As New OleDb.OleDbDataAdapter(strSelect, conn)
Dim DS As New DataSet

conn.Open()

DG.DataSource = DS.Tables("Phone")

Thank You.

Newbie Kam  Monday, January 08, 2007 2:08 AM

Chris,

If you're using VS2005, then use the DataGridView rather than the DataGrid. You can set the column width easily with:

this.MyDataGridView.Columns[0].Width = 300;

If you're using VS2003, then you're stuck with the DataGrid and you have to jump through a few more hoops to set up column widths by using DataGridTableStyles ... something like this (I just copy/pasted this from existing test code I had, sorry if it's a bit messy):

CurrencyManager cm = (CurrencyManager)this.BindingContext[this.oDataFromXML, this.oDataFromXML.Tables[1].TableName];

DataGridTableStyle style = new DataGridTableStyle(cm);

style.GridColumnStyles.Clear();
PropertyDescriptor pd = cm.GetItemProperties()["description"];
DataGridTextBoxColumn col = new DataGridTextBoxColumn(pd);

col.MappingName = "description";
col.HeaderText = "Description";
col.Width = 100;
col.Alignment = HorizontalAlignment.Right;

style.GridColumnStyles.Add(col);
style.AllowSorting = false;

pd = cm.GetItemProperties()["code"];
col = new DataGridTextBoxColumn(pd);

col.MappingName = "code";
col.HeaderText = "Code";
col.Width = 20;
col.Alignment = HorizontalAlignment.Right;

style.GridColumnStyles.Add(col);

style.MappingName = this.oDataFromXML.Tables[1].TableName;

this.oGrid2.TableStyles.Add(style);
this.oGrid2.DataSource = this.oDataFromXML.Tables[1];

BonnieB  Monday, January 08, 2007 5:38 AM

Chris,

If you're using VS2005, then use the DataGridView rather than the DataGrid. You can set the column width easily with:

this.MyDataGridView.Columns[0].Width = 300;

If you're using VS2003, then you're stuck with the DataGrid and you have to jump through a few more hoops to set up column widths by using DataGridTableStyles ... something like this (I just copy/pasted this from existing test code I had, sorry if it's a bit messy):

CurrencyManager cm = (CurrencyManager)this.BindingContext[this.oDataFromXML, this.oDataFromXML.Tables[1].TableName];

DataGridTableStyle style = new DataGridTableStyle(cm);

style.GridColumnStyles.Clear();
PropertyDescriptor pd = cm.GetItemProperties()["description"];
DataGridTextBoxColumn col = new DataGridTextBoxColumn(pd);

col.MappingName = "description";
col.HeaderText = "Description";
col.Width = 100;
col.Alignment = HorizontalAlignment.Right;

style.GridColumnStyles.Add(col);
style.AllowSorting = false;

pd = cm.GetItemProperties()["code"];
col = new DataGridTextBoxColumn(pd);

col.MappingName = "code";
col.HeaderText = "Code";
col.Width = 20;
col.Alignment = HorizontalAlignment.Right;

style.GridColumnStyles.Add(col);

style.MappingName = this.oDataFromXML.Tables[1].TableName;

this.oGrid2.TableStyles.Add(style);
this.oGrid2.DataSource = this.oDataFromXML.Tables[1];

BonnieB  Monday, January 08, 2007 5:38 AM

Thank you Bonnie

I did it with DataGridView

Newbie Kam  Monday, January 08, 2007 7:02 AM
You're welcome Chris. A little bit easier, huh?
BonnieB  Monday, January 08, 2007 2:55 PM
yup
Newbie Kam  Tuesday, January 09, 2007 1:32 AM

You can use google to search for other answers

Custom Search

More Threads

• Linq AnonymousType Problem
• What does this MSFlexgrid code do and how can I do it with DataGridView?
• How to delete rows using DataGridView bound to DataSet using DataBindingSource?
• Add newData Source: Error getting type information
• i display the image files in certain directory in DataGridView and then i want to modify the name of each image.how can i manage to do this?
• Order of events when updating BindingSource.
• Change the background color on individual cells within the .Net 2003 datagrid
• Cell is locked for update
• Find DataRow with matching data in a DataTable
• BindingList & Innherited Classes