vb.netdatagridviewdatagridviewcellstyle

How To Save DataGridView Cell Style to database


I am working with a DataGridView in vb.net and I am trying to work on saving changes that are made in the DataGridView to my Access Database. The problem is, The user can change Individual cell background colors by selecting whatever cells they want and then changing the color. I can't figure out how I can save the individual cell background color so that when the program is run again, the colors show up. When the user clicks on save the color format gets erased. I also don't think I can do this on the Access side of it by adding a color column because there will be multiple colors used in each row and column. Is there a way to save cell style formatting? Here is code... This is the save button

 Private Sub DsfToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DsfToolStripMenuItem.Click
    BaggersTableAdapter.Update(RentalsDataSet.Tables(0))
    RentalsDataSet.Tables(0).AcceptChanges()


End Sub

And The button that is clicked to change the color of the selected cells.

  Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
    For i As Integer = 0 To DataGridView1.SelectedCells.Count - 1
        'Dim a As String = (DataGridView1.SelectedCells(i).RowIndex.ToString)
        Dim colIndex As Integer = DataGridView1.SelectedCells(i).ColumnIndex
        Dim rowIndex As Integer = DataGridView1.SelectedCells(i).RowIndex
        DataGridView1.Item(colIndex, rowIndex).Style.BackColor = Color.LightGreen


    Next
End Sub

Solution

  • I would create tables for saving design colors. Something like grid_style_header (header_id, grid_id, user_id) grid_style_cells (id, header_id, row, column, color)...

    The problems can happen if you allow filtering of the grid. If that happens, your columns will probably remain, but rows may not or they may move up or down.

    better solution if you can implement it: grid_style_cells (id, header_id, row_guid (whatever primary key in your database id), column_name, color)

    This makes the row identifier absolute so you don't have any problem with filtering. Column names also get rid of column reording problem.

    I'm not aware of any in-built solution for this.