|
-
Dec 13th, 2012, 02:20 PM
#1
Thread Starter
Fanatic Member
DataGridView - Highlight Cell
I have a datagridview in my project which is bound to a dataset.
The grid populates fine, but I'm looking to do something a bit more.
One of the columns is called "Status" and right now it returns the ID from the database in this column (e.g. "13", "14" etc). I'd like to substitute this value with it's associated color so if it's "13" set the cell background to green, if it's 14 set it to yellow and so on.
Just not sure a) where to put this code and b) how to do it.
Thanks
-
Dec 13th, 2012, 03:03 PM
#2
Re: DataGridView - Highlight Cell
Simply loop through your datagridview cells and if the cell value matches a certain number, then change the background color. I don't have VS installed on this computer, but it'd look like this:
Code:
For i As Integer = 0 to DataGridView1.Rows.Count - 1
Dim row As DataGridViewRow = DataGridView1.Rows(i)
Dim cell As DataGridViewCell = row.Cells(0)
If cell.Value = "1" Then
'Do something if the ID is 1
End If
Next
-
Dec 13th, 2012, 03:35 PM
#3
Re: DataGridView - Highlight Cell
vb.net Code:
Private Sub DataGridView1_CellPainting(sender As System.Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
Dim c As Drawing.Color
For i = 0 To DataGridView1.Rows.Count - 1
Select Case CInt(DataGridView1.Rows(i).Cells(0).Value)
Case 1
c = Color.AliceBlue
Case 2
c = Color.Bisque
Case 3
c = Color.Coral
Case Else
c = Color.White
End Select
DataGridView1.Rows(i).Cells(0).Style.BackColor = c
Next
End Sub
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Dec 13th, 2012, 04:44 PM
#4
Re: DataGridView - Highlight Cell
Here is something to consider
Code:
Public Class Form1
Private dictColors As New Dictionary(Of Int32, Color) From _
{
{1, Color.LightPink},
{13, Color.LightGreen},
{14, Color.Yellow}
}
Private Sub Form1_Load(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable With {.TableName = "People"}
dt.Columns.AddRange(New DataColumn() _
{
New DataColumn("ID", GetType(Int32)),
New DataColumn("Name", GetType(String))
}
)
dt.Rows.Add(New Object() {1, "AAA"})
dt.Rows.Add(New Object() {13, "BBB"})
dt.Rows.Add(New Object() {3, "CCC"})
dt.Rows.Add(New Object() {14, "DDD"})
dt.Rows.Add(New Object() {11, "EEE"})
DataGridView1.DataSource = dt
End Sub
Private Sub DataGridView1_CellFormatting(
ByVal sender As Object,
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
Handles DataGridView1.CellFormatting
If Not DataGridView1.CurrentRow.IsNewRow Then
If e.ColumnIndex = DataGridView1.Columns("ID").Index Then
If Not IsDBNull(e.Value) Then
If dictColors.ContainsKey(CInt(e.Value)) Then
e.CellStyle.BackColor = dictColors(CInt(e.Value))
End If
End If
End If
End If
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|