I have developed an application in vb.net with MS access. In a form I have a Datagridview and I retrieve the data by databindingsource.
I am copying the data from datagridview to textbox and then to another table in database.
I want- after being copied the color of selected row should be changed , so next time whenever I retrive data in datagridview, the color of selected rows should have been changed.
Is this possible?

Example. Iterate over all columns to change the entire row.

dgvNames.Rows(row).Cells(0).Style.BackColor = Color.Thistle

Another method would be:
Set: SelectionMode to FullRowSelect of your datagrid

Private Sub DataGridView1_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        For Each row As DataGridViewRow In DataGridView1.Rows
            DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.Red
            If e.RowIndex > -1 Then
                DataGridView1.Rows(e.RowIndex).Selected = True
            End If
        Next
    End Sub

Edited 1 Year Ago by Minimalist

The another method works but sir I want put this code after I have made necessary changes in the row and updated the data. So that the next time I retrieve the data, the rows that I have viewed (changes made) will show the red color.The remaining will show the default color.

Well, show the code you have sofar,
otherwise put your copying code between lines 2 and 3:

For Each row As DataGridViewRow In DataGridView1.Rows
'Put your code here
            DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.Red

Edited 1 Year Ago by Minimalist

Yes,You can do this after necessary changes in the row.
just store the CurrentRowIndex value of the selected datagridview row then in button click event after saving the data to the database you can change the color of that row.
Declare a formlaval variable ro hold the datagridview selected row index value.

Dim slctRW As Integer = -1

Now in RowEnter event store the rowindex value.

Private Sub DataGridView1_RowEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter

    slctRW = e.RowIndex

End Sub

Now do your necessary works and change the color in save/edit button click event

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Do here all your 
        'necessary works.

        'Now chage the backcolor of the selectedrow
        DataGridView1.Rows(slctRW).DefaultCellStyle.BackColor = Color.<Your Color Name>

End Sub

Hope it can help you.

Edited 1 Year Ago by Shark_1

Sir my code to update data is as -

Private Sub HPReportBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HPReportBindingNavigatorSaveItem.Click
    Me.Validate()
    Me.HPReportBindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.Histopathology_NewDataSet1)           
End Sub

So i put your code here, but no response.
So what to do? Please.

So that the next time I retrieve the data, the rows that I have viewed (changes made) will show the red color.The remaining will show the default color.

In order to do that you will have to add a column to the database table indicating that the row is to be displayed in a different colour. You could store the display colour as the column value and just set all rows to their associated colour. If you have bound the datagridview to the table then you will likely have to do this manually. I'm not that familiar with databound controls so I could be wrong.

Sir here is my complete code. But i got this error -"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

Imports System.Data.OleDb
Imports System.Data

Public Class Register

    Private Sub HPReportBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HPReportBindingNavigatorSaveItem.Click
        Dim slctRW As Integer = -1
        Me.Validate()
        Me.HPReportBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Histopathology_NewDataSet1)
        HPReportDataGridView.Rows(slctRW).DefaultCellStyle.BackColor = Color.Red
    End Sub

     Private Sub Register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Histopathology_NewDataSet1.HPReport' table. You can move, or remove it, as needed.
        Me.HPReportTableAdapter.Fill(Me.Histopathology_NewDataSet1.HPReport)

    End Sub

    Private Sub HPReportDataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles HPReportDataGridView.RowEnter
        Dim slctRW As Integer = -1
        slctRW = e.RowIndex
    End Sub
End class

The variable should be declared as a form Classlevel variable not as a procedure lavel variable. Just declare it after Public Class Register.

Public Class Register
    Dim slctRW As Integer = -1

Now store the RowIndex value

 Private Sub HPReportDataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles HPReportDataGridView.RowEnter

        slctRW = e.RowIndex

 End Sub

Now do your works and change the color

  Private Sub HPReportBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HPReportBindingNavigatorSaveItem.Click

        Me.Validate()
        Me.HPReportBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Histopathology_NewDataSet1)

        HPReportDataGridView.Rows(slctRW).DefaultCellStyle.BackColor = Color.Red
    End Sub

Hope it can help you.

Yes the code is working, the only problem is that when I retrieve the data, the row that I updated is not in red color.
I want whenever I retrieve the data, the row which i updated should be red colored .How can i do this?

Reverend Jim has given you a pointer already. You need to store the row numbers that are colored and retrieve these when loading.

Sir i am not getting this. I am very new to this.
So please explain

You add a column named something like Changed or AlternateColour then you step through all of the rows in the grid and do

If Changed Then
    For Each cell In dgvData.Rows(row).Cells
        cell.Style.BackColor = Color.Red
    Next
End If

Just replace Changed with an expression to get the boolean vslue from the table, and Color.Red with an appropriate colour.

Edited 1 Year Ago by Reverend Jim

Put it immediately after the code that either binds the table to the grid, or the code that manually populates the grid from the table. Using the Microsoft PUBS database the following code displays the Authors table with all last names starting with "S" in red.

Dim con As New SqlConnection("Server=.\SQLEXPRESS;Database=PUBS;Trusted_Connection=yes;")
Dim qry As String = "SELECT * FROM Authors"
Dim daa As New SqlDataAdapter(qry, con)
Dim das As New DataSet()

con.Open()
daa.Fill(das, "Authors_table")
con.Close()

dgvData.DataSource = das
dgvData.DataMember = "Authors_table"

For Each row In dgvData.Rows
    Dim lname As String = row.cells(1).Value
    If lname.StartsWith("S") Then
        For Each cell In row.cells
            cell.Style.BackColor = Color.Red
        Next
    End If
Next

Sir , my problem is that I want make updated row red colored and save it as red color, so that the next time when I retrieve data, the updated row should be in red color.

I just showed you how to do that. You have to add a column to "remember" which rows have been changed, then you have to iterate over the rows to set the colour. To be more specific with my example

For Each row In dgvData.Rows
    If row.cells("Changed").Value Then
        For Each cell In row.cells
            cell.Style.BackColor = Color.Red
        Next
    End If
Next

If you don't want to add a column to your table (either a boolean or a Change_Date) then you'll have to figure out some other way to determine when a row has been changed.

Edited 1 Year Ago by Reverend Jim

This article has been dead for over six months. Start a new discussion instead.