Results 1 to 6 of 6

Thread: [RESOLVED] DataGridView first row

  1. #1
    Member
    Join Date
    Mar 11
    Posts
    58

    Resolved [RESOLVED] DataGridView first row

    On opening a form (and the DataGridView in it), I try to set the back color of my entire first row as follows :

    DataGridView1.Rows(0).SelectionBackColor = Color.BlanchedAlmond

    However, this seems not to be correct. I have tried several alternatives, but no luck so far.

    Anyone who knows the proper coding ?

  2. #2
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: DataGridView first row

    When changing the color of a row, you actually have to change the color of each cell in the row.

    Here is how to change the default back color of each cell in a row:

    Code:
    DataGridView1.Rows(0).DefaultCellStyle.BackColor = Color.BlanchedAlmond
    Here is another way... Enumerate through each cell in a row, and change the back color one by one:

    Code:
     For Each dgvc as DataGridViewCell In DataGridView1.Rows(0).Cells
         dgvc.Style.BackColor = Color.BlanchedAlmond
     Next
    Last edited by MotoX646; Sep 2nd, 2012 at 05:48 AM.

  3. #3
    Member
    Join Date
    Mar 11
    Posts
    58

    Re: DataGridView first row

    Dear MotoX646,


    Thanks but neither of these two solutions is working for me. The first one I already tried myself before posting this thread.

    But maybe I am not properly formulating my problem.
    What I really want to do, is to get rid of the blue background color in cell (0,0) whenever I open the Form with the DataGridView in it.

    I am already using background colors in combination with MouseEnter, MouseLeave etc. … on my rows, but the blue background for cell (0,0) only disappears when I enter cell/row (0).
    I want to be cell (0,0) white from the very start so that my BlanchedAlmond will be the only color in the grid.

    I assume your solutions come in prior to VS setting the background color to standard blue (and therefore your solutions are overruled).

  4. #4
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: DataGridView first row

    Ooooh... You must be talking about the "selected cell" that is selected by default. The blue color indicates the cell is selected. In that case, you just want to clear all the selected.

    Code:
    DataGridView1.ClearSelection()
    If you just want to deselect cell(0,0) you can do this:

    Code:
    DataGridView1.Rows(0).Cells(0).Selected = False

  5. #5
    Member
    Join Date
    Mar 11
    Posts
    58

    Re: DataGridView first row

    That seems to be working ... and it is of course a much easier and more direct approach than what I had in mind.

    Thanks !

  6. #6
    Lively Member
    Join Date
    May 10
    Posts
    103

    Re: DataGridView first row

    No problem...

    If you want to change the selection color of "selected" rows / cells, you can do this:

    Code:
     DataGridView1.RowsDefaultCellStyle.SelectionBackColor = Color.Red
    Or, if you want to change the selection color of a specific "selected" cell, you can do this:

    Code:
    DataGridView1.Rows(0).Cells(0).Style.SelectionBackColor = Color.Red
    That way you can change the default selection color of blue to another color.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •