Change Button Color in Datagridview Vb.net

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 10
  • Joined: 19-May 16

Changing background color of certain rows in datagridview

Posted 19 May 2016 - 04:53 AM

I am trying to get my program to change the background color of certain rows that have certain criteria in one column of the row...I have some code I am trying, but to no avail...I can't get it to work correctly...any suggestions? As of right now, the datagridview does populate the query results, but the background colors for those selected rows isn't...is my code in the right place, is something changing it back to defaults???

Here is my code thus far...when the user clicks the Enter button.

Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click      Try          Dim sqlSelect As String = "SELECT MACHSUMM9.LOAD_MACH, MACHSUMM9.LOAD_ORDER, MACHSUMM9.LOAD_SEQUENCE, MACHSUMM9.LOAD_START,      MACHSUMM9.Date_Due, MACHSUMM9.Sales_Order_Number, SALES_ORDER.OR_ADDR1, MACHSUMM9.Part_Number, MACHSUMM9.I_GAUGE, MACHSUMM9.I_DIM1, MACHSUMM9.Quantity, MACHSUMM9.Quantity_Completed     FROM MACHSUMM9 LEFT JOIN SALES_ORDER ON MACHSUMM9.Sales_Order_Number = SALES_ORDER.Sales_Order_Number     ORDER BY MACHSUMM9.LOAD_MACH, MACHSUMM9.Date_Due"          Dim myDataAdapter = New OleDbDataAdapter(sqlSelect, myWorkforceConnection)          ' Execute the query         myCommandBuilder = New OleDbCommandBuilder(myDataAdapter)          ' Fill the Data Table with the records         myDataAdapter.Fill(myDataTable)       Catch ex As Exception         MsgBox(ex.Message)     End Try      ' Check to see if any records were found, if zero sen a "No Records Found" message     If myDataTable.Rows.Count = 0 Then         MessageBox.Show("No records found.")         Exit Sub     End If      ' Move the records to the Data Grid View     dgvMachSumm.DataSource = myDataTable      ' Update enter button to refresh      btnEnter.Text = "Refresh Report"      '''''''''''''''''''''''''''''''''''''''''''''     '     ' The following statements change the column headings and width appropriately     '     '''''''''''''''''''''''''''''''''''''''''''''      ' Scan all the rows     For index As Integer = 0 To dgvMachSumm.RowCount - 1          ' Check the row looking for "G-SL02" in column LOAD_MACH          If dgvMachSumm.Rows(index).Cells("LOAD_MACH").Value = "G-SL02" Then              ' Set the row color to red             dgvMachSumm.Rows(index).DefaultCellStyle.BackColor = Color.Red          End If          ' Check the row looking for "G-SL06" in column LOAD_MACH          If dgvMachSumm.Rows(index).Cells("LOAD_MACH").Value = "G-SL06" Then              ' Set the row color to red             dgvMachSumm.Rows(index).DefaultCellStyle.BackColor = Color.Yellow          End If          If dgvMachSumm.Rows(index).Cells("LOAD_MACH").Value = "G-SL19" Then              ' Set the row color to red             dgvMachSumm.Rows(index).DefaultCellStyle.BackColor = Color.LightBlue          End If     Next       dgvMachSumm.Columns("LOAD_MACH").Width = 65     dgvMachSumm.Columns("LOAD_MACH").HeaderText = "MACH ID"       dgvMachSumm.Columns("LOAD_ORDER").Width = 80     dgvMachSumm.Columns("LOAD_ORDER").HeaderText = "WIP #"      dgvMachSumm.Columns("LOAD_SEQUENCE").Width = 65     dgvMachSumm.Columns("LOAD_SEQUENCE").HeaderText = "SEQ"      dgvMachSumm.Columns("LOAD_START").Width = 70     dgvMachSumm.Columns("LOAD_START").HeaderText = "STARTED"      dgvMachSumm.Columns("Date_Due").Width = 70     dgvMachSumm.Columns("Date_Due").HeaderText = "DATE DUE"      dgvMachSumm.Columns("Sales_Order_Number").Width = 65     dgvMachSumm.Columns("Sales_Order_Number").HeaderText = "SO #"      dgvMachSumm.Columns("OR_ADDR1").Width = 100     dgvMachSumm.Columns("OR_ADDR1").HeaderText = "CUSTOMER"      dgvMachSumm.Columns("Part_Number").Width = 85     dgvMachSumm.Columns("Part_Number").HeaderText = "PART #"      dgvMachSumm.Columns("I_GAUGE").Width = 40     dgvMachSumm.Columns("I_GAUGE").HeaderText = "GA"      dgvMachSumm.Columns("I_DIM1").Width = 60     dgvMachSumm.Columns("I_DIM1").HeaderText = "WIDTH"      dgvMachSumm.Columns("Quantity").Width = 75     dgvMachSumm.Columns("Quantity").HeaderText = "QTY DUE"      dgvMachSumm.Columns("Quantity_Completed").Width = 75     dgvMachSumm.Columns("Quantity_Completed").HeaderText = "COMPLETE"   End Sub  Any help would be appreciated!!!  [Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click  Try  Dim sqlSelect As String = "SELECT MACHSUMM9.LOAD_MACH, MACHSUMM9.LOAD_ORDER, MACHSUMM9.LOAD_SEQUENCE, MACHSUMM9.LOAD_START, MACHSUMM9.Date_Due, MACHSUMM9.Sales_Order_Number, SALES_ORDER.OR_ADDR1, MACHSUMM9.Part_Number, MACHSUMM9.I_GAUGE, MACHSUMM9.I_DIM1, MACHSUMM9.Quantity, MACHSUMM9.Quantity_Completed FROM MACHSUMM9 LEFT JOIN SALES_ORDER ON MACHSUMM9.Sales_Order_Number = SALES_ORDER.Sales_Order_Number ORDER BY MACHSUMM9.LOAD_MACH, MACHSUMM9.Date_Due"  Dim myDataAdapter = New OleDbDataAdapter(sqlSelect, myWorkforceConnection)  ' Execute the query myCommandBuilder = New OleDbCommandBuilder(myDataAdapter)  ' Fill the Data Table with the records myDataAdapter.Fill(myDataTable)   Catch ex As Exception MsgBox(ex.Message) End Try  ' Check to see if any records were found, if zero sen a "No Records Found" message If myDataTable.Rows.Count = 0 Then MessageBox.Show("No records found.") Exit Sub End If  ' Move the records to the Data Grid View dgvMachSumm.DataSource = myDataTable  ' Update enter button to refresh btnEnter.Text = "Refresh Report"  ''''''''''''''''''''''''''''''''''''''''''''' ' ' The following statements change the column headings and width appropriately ' '''''''''''''''''''''''''''''''''''''''''''''  ' Scan all the rows For index As Integer = 0 To dgvMachSumm.RowCount - 1  ' Check the row looking for "G-SL02" in column LOAD_MACH If dgvMachSumm.Rows(index).Cells("LOAD_MACH").Value = "G-SL02" Then  ' Set the row color to red dgvMachSumm.Rows(index).DefaultCellStyle.BackColor = Color.Red  End If  ' Check the row looking for "G-SL06" in column LOAD_MACH If dgvMachSumm.Rows(index).Cells("LOAD_MACH").Value = "G-SL06" Then  ' Set the row color to red dgvMachSumm.Rows(index).DefaultCellStyle.BackColor = Color.Yellow  End If  If dgvMachSumm.Rows(index).Cells("LOAD_MACH").Value = "G-SL19" Then  ' Set the row color to red dgvMachSumm.Rows(index).DefaultCellStyle.BackColor = Color.LightBlue  End If Next   dgvMachSumm.Columns("LOAD_MACH").Width = 65 dgvMachSumm.Columns("LOAD_MACH").HeaderText = "MACH ID"   dgvMachSumm.Columns("LOAD_ORDER").Width = 80 dgvMachSumm.Columns("LOAD_ORDER").HeaderText = "WIP #"  dgvMachSumm.Columns("LOAD_SEQUENCE").Width = 65 dgvMachSumm.Columns("LOAD_SEQUENCE").HeaderText = "SEQ"  dgvMachSumm.Columns("LOAD_START").Width = 70 dgvMachSumm.Columns("LOAD_START").HeaderText = "STARTED"  dgvMachSumm.Columns("Date_Due").Width = 70 dgvMachSumm.Columns("Date_Due").HeaderText = "DATE DUE"  dgvMachSumm.Columns("Sales_Order_Number").Width = 65 dgvMachSumm.Columns("Sales_Order_Number").HeaderText = "SO #"  dgvMachSumm.Columns("OR_ADDR1").Width = 100 dgvMachSumm.Columns("OR_ADDR1").HeaderText = "CUSTOMER"  dgvMachSumm.Columns("Part_Number").Width = 85 dgvMachSumm.Columns("Part_Number").HeaderText = "PART #"  dgvMachSumm.Columns("I_GAUGE").Width = 40 dgvMachSumm.Columns("I_GAUGE").HeaderText = "GA"  dgvMachSumm.Columns("I_DIM1").Width = 60 dgvMachSumm.Columns("I_DIM1").HeaderText = "WIDTH"  dgvMachSumm.Columns("Quantity").Width = 75 dgvMachSumm.Columns("Quantity").HeaderText = "QTY DUE"  dgvMachSumm.Columns("Quantity_Completed").Width = 75 dgvMachSumm.Columns("Quantity_Completed").HeaderText = "COMPLETE"   End Sub]            

:code:

This post has been edited by modi123_1: 19 May 2016 - 07:58 AM
Reason for edit:: Please use the '[ code ]' button in the editor


Is This A Good Question/Topic? 0

  • +

#2 PeterH User is offline

  • D.I.C Regular

Reputation: 60

  • View blog
  • Posts: 289
  • Joined: 03-September 09

Re: Changing background color of certain rows in datagridview

Posted 19 May 2016 - 05:24 AM

I set individual cell colours using this method e.g.

dgvMachSumm.Item("TheColumnname", rowindex).Style.BackColor = Color.Yellow            

#3 deerhntr22 User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 10
  • Joined: 19-May 16

Re: Changing background color of certain rows in datagridview

Posted 19 May 2016 - 06:17 AM

View PostPeterH, on 19 May 2016 - 05:24 AM, said:

I set individual cell colours using this method e.g.

dgvMachSumm.Item("TheColumnname", rowindex).Style.BackColor = Color.Yellow              

That didn't change anything...I believe my code is right...I am unsure on how to check to see if the program finds any value before changing the color?

#4 PeterH User is offline

  • D.I.C Regular

Reputation: 60

  • View blog
  • Posts: 289
  • Joined: 03-September 09

Re: Changing background color of certain rows in datagridview

Posted 19 May 2016 - 06:35 AM

Then add a breakpoint at the point you are checking the cell values and see if they contains what you expect.

#5 deerhntr22 User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 10
  • Joined: 19-May 16

Re: Changing background color of certain rows in datagridview

Posted 19 May 2016 - 07:54 AM

View PostPeterH, on 19 May 2016 - 06:35 AM, said:

Then add a breakpoint at the point you are checking the cell values and see if they contains what you expect.

That worked...the program wasn't finding "G-SL02" because in the database it is "G-SL02 " with spaces...

So I used the .Trim method...and it works now.

Thanks all...

#6 maceysoftware User is offline

Reputation: 396

  • View blog
  • Posts: 1,672
  • Joined: 07-September 13

Re: Changing background color of certain rows in datagridview

Posted 20 May 2016 - 01:53 AM

I keep changing my mind about if i should mention this or not.

As each row is added to the datagridview, the datagridview fires a "Cell Formatting" event.

Instead of re-scanning each row afterwards would it be better to do it here? i am in two minds with this one, on one hand it means you won't be re-scanning the rows just to add formatting.

On the other it fires for each cell, so you would have to be careful otherwise you may end up checking for each cell in the row instead of just the once. However you could do something like:

              Private Sub DataGridView1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting         If e.Value Is Nothing Then Exit Sub ' Basically if there is nothing just leave the sub           ' So basically we can go along and check to see if the column is the correct column          If e.ColumnIndex = DataGridViewTextboxColumnExample.DisplayIndex Then ' or you can use index, i always prefer display index              ' Check to see if the value of the cell = example text             If e.Value.ToString = "Example Text" Then                 ' if it does set the whole row's backcolor                 DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Red             End If          End If      End Sub            

This post has been edited by maceysoftware: 20 May 2016 - 01:53 AM

Change Button Color in Datagridview Vb.net

Source: https://www.dreamincode.net/forums/topic/393451-changing-background-color-of-certain-rows-in-datagridview/

0 Response to "Change Button Color in Datagridview Vb.net"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel