none
Datagridview not showing colors until a column is clicked RRS feed

  • Question

  • Hi Clever people,

    I have a datagridview on my form which I set the datasource to a datatable & the data appears fine. I then set the different columns to have their own color & I set the columns to specific widths. I've tried all sorts of timing & refresh statements etc to no avail on the colors. I have to click on a column header changing the sort then the colors apply so I have to click a 2nd time to get the sort as it should be. Am I maybe missing an imports or what else could be the cause?

    Here is the code I'm using. Any advice really appreciated

                Using cmd = New SqlCommand("stpHistEnquiryBall", conn)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.AddWithValue("@Competitor", index)
                    dr = cmd.ExecuteReader()
    
                    bt.Load(dr)
    
                End Using  'cmd
    
    
    
                dgvBall.DataSource = bt
                dr.Close()
    
                Dim Bcolwidth0 As DataGridViewColumn = dgvBall.Columns(0)
                Bcolwidth0.Width = 160
                Dim Bcolwidth1 As DataGridViewColumn = dgvBall.Columns(1)
                Bcolwidth1.Width = 250
                Dim Bcolwidth2 As DataGridViewColumn = dgvBall.Columns(2)
                Bcolwidth2.Width = 80
                Dim Bcolwidth3 As DataGridViewColumn = dgvBall.Columns(3)
                Bcolwidth3.Width = 260
                Dim Bcolwidth4 As DataGridViewColumn = dgvBall.Columns(4)
                Bcolwidth4.Width = 70
                dgvBall.Columns(0).CellTemplate.Style.BackColor = Drawing.Color.LightGreen
                dgvBall.Columns(1).CellTemplate.Style.BackColor = Drawing.Color.Bisque
                dgvBall.Columns(2).CellTemplate.Style.BackColor = Drawing.Color.LightCoral
                dgvBall.Columns(3).CellTemplate.Style.BackColor = Drawing.Color.Aqua
                dgvBall.Columns(4).CellTemplate.Style.BackColor = Drawing.Color.SandyBrown

    Wednesday, November 7, 2018 6:34 AM

Answers

  • Hello,

    You can use default cell style in your case

    hope below code helps you

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                'Create a Connection object.
                Dim myConn As SqlConnection = New SqlConnection("Initial Catalog=TestDB;" & _
                        "Data Source=xxxxxxxx\SQLSERVER2012;Integrated Security=True;")
                myConn.Open()
                Dim cmd As SqlCommand = New SqlCommand("Select * from SubjectsInGroup", myConn)
                Dim rd As SqlDataReader = cmd.ExecuteReader()
                Dim dt As New DataTable
                dt.Load(rd)
                DataGridView1.DataSource = dt
                myConn.Close()
                DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.LightGreen
                DataGridView1.Columns(1).DefaultCellStyle.BackColor = Drawing.Color.Bisque
                
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub

    Wednesday, November 7, 2018 8:02 AM

All replies

  • Hi,

    fix the code,put the code here.

     Private Sub dgvBall_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvBall.CellFormatting
            Dim Bcolwidth0 As DataGridViewColumn = dgvBall.Columns(0)
            Bcolwidth0.Width = 160
            Dim Bcolwidth1 As DataGridViewColumn = dgvBall.Columns(1)
            Bcolwidth1.Width = 250
            Dim Bcolwidth2 As DataGridViewColumn = dgvBall.Columns(2)
            Bcolwidth2.Width = 80
            Dim Bcolwidth3 As DataGridViewColumn = dgvBall.Columns(3)
            Bcolwidth3.Width = 260
            Dim Bcolwidth4 As DataGridViewColumn = dgvBall.Columns(4)
            Bcolwidth4.Width = 70
            dgvBall.Columns(0).CellTemplate.Style.BackColor = Drawing.Color.LightGreen
            dgvBall.Columns(1).CellTemplate.Style.BackColor = Drawing.Color.Bisque
            dgvBall.Columns(2).CellTemplate.Style.BackColor = Drawing.Color.LightCoral
            dgvBall.Columns(3).CellTemplate.Style.BackColor = Drawing.Color.Aqua
            dgvBall.Columns(4).CellTemplate.Style.BackColor = Drawing.Color.SandyBrown
        End Sub

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex Li-MSFT Wednesday, November 7, 2018 7:46 AM
    Wednesday, November 7, 2018 7:45 AM
  • Hello,

    You can use default cell style in your case

    hope below code helps you

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Try
                'Create a Connection object.
                Dim myConn As SqlConnection = New SqlConnection("Initial Catalog=TestDB;" & _
                        "Data Source=xxxxxxxx\SQLSERVER2012;Integrated Security=True;")
                myConn.Open()
                Dim cmd As SqlCommand = New SqlCommand("Select * from SubjectsInGroup", myConn)
                Dim rd As SqlDataReader = cmd.ExecuteReader()
                Dim dt As New DataTable
                dt.Load(rd)
                DataGridView1.DataSource = dt
                myConn.Close()
                DataGridView1.Columns(0).DefaultCellStyle.BackColor = Color.LightGreen
                DataGridView1.Columns(1).DefaultCellStyle.BackColor = Drawing.Color.Bisque
                
            Catch ex As Exception
                Dim errormsg As String = String.Empty
                errormsg = ex.ToString()
            End Try
        End Sub

    Wednesday, November 7, 2018 8:02 AM
  • Thank you Thank you Thank you! Works like a charm. Easy when you know how & everything I find out here is something I don't forget since I've read a lot before asking.
    Wednesday, November 7, 2018 1:08 PM