Highlight a column in a datagrid

P

PuckInLA

I have a datagrid with a column that I want to change the background
color so it stands out. Basically there is VB a procedure that imports
a text file directly into the datagrid. The last column in the
datagrid, results, I want highlighted and I just can't figure it
out.... Here is the code I have so far:

Sub OLE_SQL()
Dim strSelect As String = "SELECT * FROM sampleResult.txt"
Dim strConString As String
Dim strDataSource As String

Dim strConString1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="
Dim strConString2 As String = ";Extended
Properties='text;FMT=Delimited'"
strConString = strConString1 & strDataSource & strConString2

Dim objConnect As New OleDb.OleDbConnection(strConString)
Dim objCommand As New OleDb.OleDbCommand(strSelect, objConnect)

Dim myDataSet As New DataSet

Dim objAdapter As New OleDb.OleDbDataAdapter(objCommand)

objConnect.Open()
objAdapter.Fill(myDataSet)
objConnect.Close()

myDataSet.Tables(0).Columns(0).ColumnName = "Sample ID"
myDataSet.Tables(0).Columns(1).ColumnName = "Date Collected"
myDataSet.Tables(0).Columns(2).ColumnName = "Time Collected"
myDataSet.Tables(0).Columns(3).ColumnName = "T1"
myDataSet.Tables(0).Columns(4).ColumnName = "T2"
myDataSet.Tables(0).Columns(5).ColumnName = "Person Sampling"
myDataSet.Tables(0).Columns(6).ColumnName = "Sample ID"
myDataSet.Tables(0).Columns(7).ColumnName = "Result"


Dim dbTable1 As New DataTable
dbTable1 = myDataSet.Tables(0)


DataGrid1.DataSource = dbTable1
DataGrid1.DataBind()

'Highlights the Result Column (7)
DataGrid1.Columns(7).ItemStyle.BackColor = System.Drawing.Color.Yellow

End Sub

But when it runs an error is thrown on
DATAGRID1.COLUMNS(7).ITEMSTYLE....

Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index

Any help on this would be very welcome.
 
K

Ken Tucker [MVP]

Hi,

Datagrid.Select(rownum)
http://msdn.microsoft.com/library/d...ystemwindowsformsdatagridclassselecttopic.asp


Ken
--------------------
I have a datagrid with a column that I want to change the background
color so it stands out. Basically there is VB a procedure that imports
a text file directly into the datagrid. The last column in the
datagrid, results, I want highlighted and I just can't figure it
out.... Here is the code I have so far:

Sub OLE_SQL()
Dim strSelect As String = "SELECT * FROM sampleResult.txt"
Dim strConString As String
Dim strDataSource As String

Dim strConString1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source="
Dim strConString2 As String = ";Extended
Properties='text;FMT=Delimited'"
strConString = strConString1 & strDataSource & strConString2

Dim objConnect As New OleDb.OleDbConnection(strConString)
Dim objCommand As New OleDb.OleDbCommand(strSelect, objConnect)

Dim myDataSet As New DataSet

Dim objAdapter As New OleDb.OleDbDataAdapter(objCommand)

objConnect.Open()
objAdapter.Fill(myDataSet)
objConnect.Close()

myDataSet.Tables(0).Columns(0).ColumnName = "Sample ID"
myDataSet.Tables(0).Columns(1).ColumnName = "Date Collected"
myDataSet.Tables(0).Columns(2).ColumnName = "Time Collected"
myDataSet.Tables(0).Columns(3).ColumnName = "T1"
myDataSet.Tables(0).Columns(4).ColumnName = "T2"
myDataSet.Tables(0).Columns(5).ColumnName = "Person Sampling"
myDataSet.Tables(0).Columns(6).ColumnName = "Sample ID"
myDataSet.Tables(0).Columns(7).ColumnName = "Result"


Dim dbTable1 As New DataTable
dbTable1 = myDataSet.Tables(0)


DataGrid1.DataSource = dbTable1
DataGrid1.DataBind()

'Highlights the Result Column (7)
DataGrid1.Columns(7).ItemStyle.BackColor = System.Drawing.Color.Yellow

End Sub

But when it runs an error is thrown on
DATAGRID1.COLUMNS(7).ITEMSTYLE....

Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index

Any help on this would be very welcome.
 

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