Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I'm hoping to find an easy way to change the backcolor for an entire column in a datagridview. My program is a windows forms vb.net program. This is what I have so far but it doesn't seem to be working.

datagridview1.columns(0).defaultcellstyle.backcolor = color.blue

I greatly appreciate any help or suggestions you may offer. Thanks!
Posted
Updated 23-Mar-22 15:25pm
v2

It works fine for me:
VB
Using con As New SqlConnection(strConnect)
    con.Open()
    Dim dt As New DataTable()
    Dim da As New SqlDataAdapter("SELECT * FROM MyTable", con)
    da.Fill(dt)
    myDataGridView.DataSource = dt
    myDataGridView.Columns(0).DefaultCellStyle.BackColor = Color.Blue
End Using

What am I doing that is different from you?
 
Share this answer
 
VB
Private Sub dgvfamily_CellLeave(sender As Object, e As DataGridViewCellEventArgs)
    For i As Integer = 0 To dgvfamily.Rows.Count - 1
        'dgvfamily.Rows[i].Cells[0].Value = (i + 1).ToString();
        For j As Integer = 1 To dgvfamily.Columns.Count - 1
            If dgvfamily.Rows(i).Cells(j).Value Is Nothing Then
                'dgvfamily.ClearSelection();
                dgvfamily.Rows(i).Cells(j).Style.BackColor = Color.RoyalBlue
                dgvfamily.CurrentCell = dgvfamily.Rows(i).Cells(j)
                Return
            End If
        Next
    Next




I ma using this when datagrid cell leave blank on that time it will change the color of blank cell


Thanks
Kishan
 
Share this answer
 
DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Blue

or refer this side
http://www.vb-tips.com/DGVRowColor.aspx[^]
 
Share this answer
 
DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.Red
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900