Click to See Complete Forum and Search --> : How to adjust the column width in a datagrid


gibea00
February 11th, 2003, 12:46 PM
Hi !

I'm able to set the differetns column's name of a datadrid but not the width. How can i do that?

Here's my code how i adjust the column name:

Dim adoDRCurrentRow As DataRow
Dim adoDCEffetsIdentifies As New DataColumn

adoDCEffetsIdentifies = CType(_adodtCheque, DataTable).Columns(0)
adoDCEffetsIdentifies.ColumnName = "DATE COMPTABLE"

adoDCEffetsIdentifies = CType(_adodtCheque, DataTable).Columns(1)
adoDCEffetsIdentifies.ColumnName = "NO REF"

Thanks

DdH
February 11th, 2003, 03:52 PM
You got a column out the datatable and not of the datagrid itself.
To change the width of a column in a datagrid, you must add a tablestyle to the grid.

Try the following example:
Add a DataGrid and a Button on a Form and copy the following code to it.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As New DataTable("Test")

Dim co As DataColumn
co = New DataColumn("col1", GetType(String))
dt.Columns.Add(co)
co = New DataColumn("col2", GetType(Integer))
dt.Columns.Add(co)
co = New DataColumn("col3", GetType(Date))
dt.Columns.Add(co)

Me.DataGrid1.DataSource = dt

Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
myGridTableStyle.MappingName = dt.TableName

DataGrid1.TableStyles.Add(myGridTableStyle)

myGridTableStyle = DataGrid1.TableStyles.Item("Test")

Dim myColumnStyle As DataGridColumnStyle = myGridTableStyle.GridColumnStyles.Item("col2")
myColumnStyle.Width = 200
End Sub

I hope this helps,
Danny

nbCathy
February 7th, 2004, 11:53 AM
Danny,

I copied you example and it ran fine. I'm new at this. I already have a datagrid with columns. How might I apply this to my datagrid and columns.

Some parameters I have are
datagrid1
table name=surname1 with columns:Name,Event,Date,Name2,Name3. They are all text.

I don't know what else you might need to know. I would like to be able to give the 5 columns different widths.

I appreciate your help.

nbCathy
February 7th, 2004, 01:01 PM
This is my actual code:

' Creating connection and command sting
Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=" & DBFileName
Dim sqlStr As String = "SELECT NAME1_INDEX,EVENT,SEX,DATE,COUNTY,NAME2,NAME3_COMMENTS
' Create connection object
Dim conn As OleDbConnection = New OleDbConnection(conStr)
' Create data adapter object
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
' Create a dataset object and fill with data using data adapter's Fill method
Dim ds As DataSet = New DataSet
da.Fill(ds, DBTableName)
' Attach dataset's DefaultView to the datagrid control
DataGrid1.DataSource = ds.DefaultViewManager
DataGrid1.DataMember = DBTableName

I can't believe I'm actually so far into this as I am! I can actually do searches and everything. WOW.

If I can get how to set the width's on the columns I'm almost there.

Appreciate your help.

DdH
February 7th, 2004, 05:01 PM
Hi Cathy,

I add some extra code to yours and hope it will help you.

Danny


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Creating connection and command sting
Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=" & DBFileName
Dim sqlStr As String = "SELECT NAME1_INDEX,EVENT,SEX,DATE,COUNTY,NAME2,NAME3_COMMENTS"

' Create connection object
Dim conn As OleDbConnection = New OleDbConnection(conStr)
' Create data adapter object
Dim da As OleDbDataAdapter = New OleDbDataAdapter(sqlStr, conn)
' Create a dataset object and fill with data using data adapter's Fill method
Dim ds As DataSet = New DataSet
da.Fill(ds, DBTableName)
' Attach dataset's DefaultView to the datagrid control
DataGrid1.DataSource = ds.DefaultViewManager
DataGrid1.DataMember = DBTableName


' Set Column width
Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
myGridTableStyle.MappingName = DBTableName

DataGrid1.TableStyles.Add(myGridTableStyle)

myGridTableStyle = DataGrid1.TableStyles.Item(DBTableName)

Dim myColumnStyle As DataGridColumnStyle
myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME1_INDEX")
myColumnStyle.Width = 50

myColumnStyle = myGridTableStyle.GridColumnStyles.Item("EVENT")
myColumnStyle.Width = 150

myColumnStyle = myGridTableStyle.GridColumnStyles.Item("SEX")
myColumnStyle.Width = 200

myColumnStyle = myGridTableStyle.GridColumnStyles.Item("COUNTY")
myColumnStyle.Width = 100

myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME2")
myColumnStyle.Width = 100

myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME3_COMMENTS")
myColumnStyle.Width = 300

End Sub

nbCathy
February 7th, 2004, 07:33 PM
It worked fine. But now I have my rowheadervisible (which I don't want). It's also reset my rowheight to something. How do I adjust the rowheight also.

Thanks for responding

nbCathy
February 7th, 2004, 08:05 PM
I got the RowHeader fixed

myGridTableStyle.RowHeadersVisible = False


Used this for rowheight but it didn't work

myGridTableStyle.PreferredRowHeight = 80

Cathy

DdH
February 8th, 2004, 04:09 AM
Setting the PreferredRowHeight to 80 does work for me, when I put it in my previous example.

Danny


' Set Column width
Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
myGridTableStyle.MappingName = DBTableName
myGridTableStyle.RowHeadersVisible = False
myGridTableStyle.PreferredRowHeight = 80

DataGrid1.TableStyles.Add(myGridTableStyle)

myGridTableStyle = DataGrid1.TableStyles.Item(DBTableName)

Dim myColumnStyle As DataGridColumnStyle
myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME1_INDEX")
myColumnStyle.Width = 50

...ect

nbCathy
February 8th, 2004, 07:39 AM
It now works. I had inserted that line in the wrong place.

My form has about 32 rows of information. The information in the cells seem to be at the left botton. Question is how do I set the font and the position of the data within the cells.

Also, (null) values are displayed for empty cells. How do I rid the cell of the (null) and just have the cell blank.

I really appreciate your help. I'm not real good at programming but if you ever need to look for oil on your property, I might be able to handle that.

DdH
February 8th, 2004, 09:09 AM
Getting rid of the null is easy, just add the following line to all your columns.
- myColumnStyle.NullText = ""

You only can adjust the horizontal alignment, by adding the following.
- myColumnStyle.Alignment = HorizontalAlignment.Center

To set the Font, you must set it for the entire grid.
- DataGrid1.Font = New Font("Courier", 15)
or for the headers.
- myGridTableStyle.HeaderFont = New Font("Ariel", 10)

Hope this helps also
Danny
(I'll appreciate your offer, maybe I'll keep you to it some day :D)



' Set Column width
Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle
myGridTableStyle.MappingName = DBTableName
myGridTableStyle.RowHeadersVisible = False
myGridTableStyle.PreferredRowHeight = 80
myGridTableStyle.HeaderFont = New Font("Ariel", 10)

DataGrid1.Font = New Font("Courier", 15)
DataGrid1.TableStyles.Add(myGridTableStyle)

myGridTableStyle = DataGrid1.TableStyles.Item(DBTableName)

Dim myColumnStyle As DataGridColumnStyle
myColumnStyle = myGridTableStyle.GridColumnStyles.Item("NAME1_INDEX")
myColumnStyle.Width = 50
myColumnStyle.Alignment = HorizontalAlignment.Center
myColumnStyle.NullText = ""

nbCathy
February 8th, 2004, 10:43 AM
I meant something else rather than
myColumnStyle.Alignment = HorizontalAlignment.Center

Actually I guess I'm looking for VerticalAlignment.Center within the cell. I didn't see it.

My grid is pretty tight. Ariel 7 with height of 7 or as tight as I can get it. The data in the cell itself seems to be assigned bottom.left.

Is there some way to vertical center data in the cell?

The big question. Does one have to have a memory of all the available options. I have a Sam's do it all in 21 days. Is there a better reference book for VB.net?

I also find it hard to navigate in the MSDN library.

Thanks