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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagridview conditional row formatting in .Net 2005 1

Status
Not open for further replies.

jbehrne

Programmer
Dec 18, 2002
484
US
Hello all,

I am having problems finding an answer to row conditional formatting in VB.Net 2005 using the datagridview control on a windows form.

I am pulling data from a stored procedure and dumping it into a datagridview (NOT A .NET 2003 DATAGRID). Once the data is loaded how do I change the row backcolor if the value of a cell (a check box) is marked as being checked?

Basically if the cell's check box is checked on any given row (and there are many rows) the row backcolor needs to be changed to red, otherwise the row backcolor should be displayed as white.

Here is the code that loads the stored procedure to my datagrid:
Code:
 Private Function GetValues() As Boolean

        Dim returnVals As New SqlCommand                        'Variable to hold sql command
        Dim oSQLConn As SqlConnection = New SqlConnection()     'Variable to hold connection to the server
        Dim recreturned As Integer                              'Variable to hold the number of records returned from sql server

        GetValues = False

        'Clear any existing data from datagrid
        Call ClearDataGrid()

        Windows.Forms.Cursor.Current = Cursors.WaitCursor

        oSQLConn.ConnectionString = "Data Source=" & ServerName & ";Integrated Security=SSPI;Initial Catalog=InHouseGLS"

        With returnVals
            .Connection = oSQLConn
            .CommandText = "sp_frmInvoices"
            .CommandType = CommandType.StoredProcedure
        End With

        Try
            oSQLConn.Open()

            Dim rdrVen As SqlDataAdapter = New SqlDataAdapter(returnVals)
            Dim DataSet As DataSet = New DataSet(returnVals.CommandText)

            rdrVen.Fill(DataSet)

            recreturned = DataSet.Tables(0).Rows.Count

            DataGridView1.DataSource = DataSet.Tables(0)

            Windows.Forms.Cursor.Current = Cursors.Default

            Me.ToolStripStatusLabel1.Text = "Returned " & DataSet.Tables(0).Rows.Count & " records."

            GetValues = True
            oSQLConn.Dispose()

            'Set the datagird null values to empty strings
            Call SetIsNullStringsToEmpty(DataSet)

        Catch ex As Exception
            Windows.Forms.Cursor.Current = Cursors.Default
            Select Case Err.Number
                Case 5
                    MsgBox("Could not establish a connection to the database." & vbCrLf & "Record this informaiton and contact your computer analyst immediately!" & vbCrLf & vbCrLf & "Error: " & Err.Number, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
                Case Else
                    MsgBox("An unknown error has occurred." & vbCrLf & "Please record this information and contact your computer analyst immediately!" & vbCrLf & vbCrLf & "Error: " & Err.Number & vbCrLf & "Description: " & Err.Description, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error")
            End Select
            oSQLConn.Dispose()
            Me.Close()
            Me.Dispose()
            Exit Function
        End Try

    End Function

Thanks for any help,

jbehrne

If at first you don't succeed, call in an airstrike. - Murphy's Laws of Combat Operations
 
Use the DataGridView.CellFormatting event, check that the value is true and adjust the row default cell style to show a red back colour:
Code:
Private Sub dgv_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs)

    'Change the column index to match your check box column

    If e.ColumnIndex = 0 AndAlso Not e.Value Is Nothing AndAlso Convert.ToBoolean(e.Value) Then

        dgv.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red

    End If

End Sub
(this was translated to VB from C# by memory, so please excuse any syntax errors....)
 
SHelton,

Worked like a charm! Thanks!

jbehrne

If at first you don't succeed, call in an airstrike. - Murphy's Laws of Combat Operations
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top