DataGrid Style

D

Daniel

Hi all. Help! What am I doing wrong? My datagrid works fine, the
columns are created and the data is filled. However, I just cannot
resize the column widths to what I want them to be. I created a
TableStyle but it is ignored. :-(

I am using this code:


Dim DS As New DataSet
Dim MyTable As New DataTable
Dim Col0 As New DataColumn
Dim Col1 As New DataColumn
Dim Col2 As New DataColumn

DS.Tables.Add(0)
DS.Tables(0).Columns.Add(Col0)
DS.Tables(0).Columns(0).ColumnName = "Checked"
DS.Tables(0).Columns.Add(Col1)
DS.Tables(0).Columns(1).ColumnName = "Path"
DS.Tables(0).Columns.Add(Col2)
DS.Tables(0).Columns(2).ColumnName = "Node"


Dim newRow As DataRow

newRow = DS.Tables(0).NewRow
newRow.Item("Checked") = False
newRow.Item("Path") = datSplit(0)
newRow.Item("Node") = NodeName

DS.Tables(0).Rows.Add(ErrRow)

myDataGrid.DataSource = DS
myDataGrid.SetDataBinding(DS, DS.Tables(0).ToString)
 
K

Ken Tucker [MVP]

Hi,

I dont see any code where you added a tablestyle to your grid.

Here is an example.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim conn As OleDbConnection

Dim strConn As String

Dim strSQL As String

Dim ds As New DataSet

Dim daCustomers As OleDbDataAdapter

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn += "Data Source = C:\Northwind.mdb;"

conn = New OleDbConnection(strConn)

daCustomers = New OleDbDataAdapter("Select * from Customers", conn)

daCustomers.Fill(ds, "Customers")

SetupGrid()



DataGrid1.DataSource = ds.Tables("Customers")

End Sub

Private Sub SetupGrid()

Dim ts As New DataGridTableStyle

ts.MappingName = "Customers"

Dim colName As New DataGridTextBoxColumn

With colName

..MappingName = "ContactName"

..HeaderText = "Name"

..Width = 150

End With


Dim colID As New DataGridTextBoxColumn

With colID

..MappingName = "CustomerID"

..HeaderText = "ID"

..Width = 80

End With

Dim colRegion As New DataGridTextBoxColumn

With colRegion

..MappingName = "Region"

..HeaderText = "Region"

..Width = 80

..NullText = ""

End With

ts.GridColumnStyles.Add(colID)

ts.GridColumnStyles.Add(colName)

ts.GridColumnStyles.Add(colRegion)

DataGrid1.TableStyles.Add(ts)

ts = Nothing

colRegion = Nothing

colName = Nothing

colID = Nothing

End Sub

Ken
--------------------------
Hi all. Help! What am I doing wrong? My datagrid works fine, the
columns are created and the data is filled. However, I just cannot
resize the column widths to what I want them to be. I created a
TableStyle but it is ignored. :-(

I am using this code:


Dim DS As New DataSet
Dim MyTable As New DataTable
Dim Col0 As New DataColumn
Dim Col1 As New DataColumn
Dim Col2 As New DataColumn

DS.Tables.Add(0)
DS.Tables(0).Columns.Add(Col0)
DS.Tables(0).Columns(0).ColumnName = "Checked"
DS.Tables(0).Columns.Add(Col1)
DS.Tables(0).Columns(1).ColumnName = "Path"
DS.Tables(0).Columns.Add(Col2)
DS.Tables(0).Columns(2).ColumnName = "Node"


Dim newRow As DataRow

newRow = DS.Tables(0).NewRow
newRow.Item("Checked") = False
newRow.Item("Path") = datSplit(0)
newRow.Item("Node") = NodeName

DS.Tables(0).Rows.Add(ErrRow)

myDataGrid.DataSource = DS
myDataGrid.SetDataBinding(DS, DS.Tables(0).ToString)
 
D

Daniel

Ken said:
Hi,

I dont see any code where you added a tablestyle to your grid.

Here is an example.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim conn As OleDbConnection

Dim strConn As String

Dim strSQL As String

Dim ds As New DataSet

Dim daCustomers As OleDbDataAdapter

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn += "Data Source = C:\Northwind.mdb;"

conn = New OleDbConnection(strConn)

daCustomers = New OleDbDataAdapter("Select * from Customers", conn)

daCustomers.Fill(ds, "Customers")

SetupGrid()



DataGrid1.DataSource = ds.Tables("Customers")

End Sub

Private Sub SetupGrid()

Dim ts As New DataGridTableStyle

ts.MappingName = "Customers"

Dim colName As New DataGridTextBoxColumn

With colName

..MappingName = "ContactName"

..HeaderText = "Name"

..Width = 150

End With


Dim colID As New DataGridTextBoxColumn

With colID

..MappingName = "CustomerID"

..HeaderText = "ID"

..Width = 80

End With

Dim colRegion As New DataGridTextBoxColumn

With colRegion

..MappingName = "Region"

..HeaderText = "Region"

..Width = 80

..NullText = ""

End With

ts.GridColumnStyles.Add(colID)

ts.GridColumnStyles.Add(colName)

ts.GridColumnStyles.Add(colRegion)

DataGrid1.TableStyles.Add(ts)

ts = Nothing

colRegion = Nothing

colName = Nothing

colID = Nothing

End Sub

Ken
--------------------------
Hi all. Help! What am I doing wrong? My datagrid works fine, the
columns are created and the data is filled. However, I just cannot
resize the column widths to what I want them to be. I created a
TableStyle but it is ignored. :-(

I am using this code:


Dim DS As New DataSet
Dim MyTable As New DataTable
Dim Col0 As New DataColumn
Dim Col1 As New DataColumn
Dim Col2 As New DataColumn

DS.Tables.Add(0)
DS.Tables(0).Columns.Add(Col0)
DS.Tables(0).Columns(0).ColumnName = "Checked"
DS.Tables(0).Columns.Add(Col1)
DS.Tables(0).Columns(1).ColumnName = "Path"
DS.Tables(0).Columns.Add(Col2)
DS.Tables(0).Columns(2).ColumnName = "Node"


Dim newRow As DataRow

newRow = DS.Tables(0).NewRow
newRow.Item("Checked") = False
newRow.Item("Path") = datSplit(0)
newRow.Item("Node") = NodeName

DS.Tables(0).Rows.Add(ErrRow)

myDataGrid.DataSource = DS
myDataGrid.SetDataBinding(DS, DS.Tables(0).ToString)
Thanks for this however i get the error "The data grid table styles
collection already contains a table style with the same mapping name"

This is because I have already created the tablestyle via the properties
of the datagrid in the IDE. My problem is that the style I created is
ignored! Any ideas?
 
D

Daniel

Daniel said:
Thanks for this however i get the error "The data grid table styles
collection already contains a table style with the same mapping name"

This is because I have already created the tablestyle via the properties
of the datagrid in the IDE. My problem is that the style I created is
ignored! Any ideas?
It is OK, I worked it out. I binned the tablestyle that i made in the
IDE and just created it via code and it works. 1 line i missed though
was "MyTS.MappingName = mYTable.GetType().Name" where MyTS is the New
TableStyle.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top