Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does anyone know why I cannot get this code to work on VB.NET? 1

Status
Not open for further replies.

piscis

Programmer
Jun 26, 2003
29
PR

Dim dgStyle as DataGridTableStyle
Dim dgTableStyle As New DataGridTableStyle()
Dim colStyle1 As New DataGridTextBoxColumn()

With colStyle1
.Alignment = HorizontalAlignment.Left
.HeaderText = "Cost"
.MappingName = "ItemCost"
.Width = 20
.format(“c”)
End With

dgTableStyle.GridColumnStyles.Add(colStyle1)
dgTableStyle.MappingName = "tblSales"

Me.DataGrid1.TableStyles.Add(dgTableStyle)
Me.DataGrid1.TableStyles.Clear()
 

How are you setting the DataSource for the DataGrid??? & why are saying

Me.DataGrid1.TableStyles.Clear() ????

 

General rule of thumb for Adding Tables and Columns to the Windows Forms DataGrid control:

Note: when programmatically specifying column styles, always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection object before adding DataGridTableStyle object to the GridTableStylesCollection object.

1. In order to display data in the table, you must first bind the DataGrid control to a DataSet.
2. Declare new table style and set its mapping name.
3. Declare a new column style and set its mapping name and other properties.
4. Call the Add method of GridCoulmnStylesCollection object to add the column to the table style.
5. Call the Add method of the GridTableStylesCollection object to add the table style to the data grid.


 

follow this simple sample:-

Imports System.Data.OleDb

Dim strConn As String
Dim strSQL As String
Dim Conn As OleDbConnection

Dim objDA As OleDbDataAdapter
Dim objDS As New DataSet()

Try
'Build the Connection strings.
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Prospects.mdb"

'Pass the Connection string to OleDbConnection object.
Conn = New OleDbConnection(strConn)

'Build the SQL strings.
strSQL = "SELECT SubTypeID, SubTypeName " & _
"FROM SubType ORDER BY SubTypeName"

'Initialize the OleDbDataAdapter with SQL and Connection string,
'and then use the OleDbAdapter to fill the DataSet with data.
objDA = New OleDbDataAdapter(strSQL, Conn)
objDA.Fill(objDS, "SubType")

'Bind the DataSet to DataGrid.
DataGrid1.SetDataBinding(objDS, "SubType")

'Create DataGridTableStyle and map it to the data table.
Dim subtypeTableStyle As New DataGridTableStyle()
subtypeTableStyle.MappingName = "SubType"

'Create ColumnStyle and set its mapping name & other properties.
Dim colSubTypeID As New DataGridTextBoxColumn()
colSubTypeID.HeaderText = "SubTypeID"
colSubTypeID.MappingName = "SubTypeID"
colSubTypeID.Width = 0

Dim colSubTypeName As New DataGridTextBoxColumn()
colSubTypeName.HeaderText = "Sub Type"
colSubTypeName.MappingName = "SubTypeName"
colSubTypeName.Width = 100

'Call the Add method of GridColumnStylesCollection object to add the column to the table style.
subtypeTableStyle.GridColumnStyles.Add(colSubTypeID)
subtypeTableStyle.GridColumnStyles.Add(colSubTypeName)

'Call the Add method of GridTbaleStylesCollection object to add the table style to the data grid.
grdSubType.TableStyles.Add(subtypeTableStyle)
grdSubType.TableStyles("SubType").ReadOnly = True
grdSubType.TableStyles("SubType").AllowSorting = False

Catch Excep As System.Exception
MessageBox.Show(Excep.Message, "Prospect Management System (searchRec)", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try




 
Guys:

Thanks for all your input I used this and its working just fine. Thanks all.

___________________________________________________________
Sub DataGridTableStyle()

'Create a DataGridTableStyle & set mappingname to table
Me.OleDbDataAdapter1.Fill(Me.DsBrio250Table5)

Dim column As New DataGridTextBoxColumn
Dim tableStyle As New DataGridTableStyle
tableStyle.MappingName = "Brio250Table"
tableStyle.ReadOnly = True
tableStyle.AllowSorting = False

'Create Record # Datagrid Table
column = New DataGridTextBoxColumn
column.MappingName = "ID"
column.HeaderText = "#"
column.Width = 30
column.Alignment = HorizontalAlignment.Center
tableStyle.GridColumnStyles.Add(column)
___________________________________________________________
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top