Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to change the background color of a selected row in datagridview

how to change the background color of a selected row in datagridview

Scheduled Pinned Locked Moved C#
tutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NarVish
    wrote on last edited by
    #1

    In a gridview, how to change the background color of a selected row, on click of a cell in datagridview. Thanks in advance

    OriginalGriffO L 2 Replies Last reply
    0
    • N NarVish

      In a gridview, how to change the background color of a selected row, on click of a cell in datagridview. Thanks in advance

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Handle the CellClick event:

      private void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
      {
      DataGridView dgv = sender as DataGridView;
      if (dgv != null)
      {
      dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
      }
      }

      If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      N L 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Handle the CellClick event:

        private void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
        DataGridView dgv = sender as DataGridView;
        if (dgv != null)
        {
        dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
        }
        }

        If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

        N Offline
        N Offline
        NarVish
        wrote on last edited by
        #3

        Thanks for your reply. The code which you sent changing the back color of a row, wherever I click in the gridview. So entire grid resulted in red background color My issue is to change the selected background color. To describe more, If I click on first row, background color should change to Dark Gray color. after that, if I click on second row, first row should get reverted to previous color and second row background color should be in darkgray color.

        OriginalGriffO 1 Reply Last reply
        0
        • N NarVish

          In a gridview, how to change the background color of a selected row, on click of a cell in datagridview. Thanks in advance

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Set your row default selection colour in your form design, or add the following during initialisation:

          gridView.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Red; // or other colour

          And add something similar to the following to your cell click event:

          gridView.Rows[e.RowIndex].Selected = true;

          N 1 Reply Last reply
          0
          • N NarVish

            Thanks for your reply. The code which you sent changing the back color of a row, wherever I click in the gridview. So entire grid resulted in red background color My issue is to change the selected background color. To describe more, If I click on first row, background color should change to Dark Gray color. after that, if I click on second row, first row should get reverted to previous color and second row background color should be in darkgray color.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            Do you expect me to do everything for you? Keep a "I changed this" row index, and change it back. Then set the new row, and save the index. It's not exactly rocket science...

            If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • OriginalGriffO OriginalGriff

              Handle the CellClick event:

              private void myDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
              {
              DataGridView dgv = sender as DataGridView;
              if (dgv != null)
              {
              dgv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
              }
              }

              If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Strange how we both chose Red. ;)

              OriginalGriffO 1 Reply Last reply
              0
              • L Lost User

                Set your row default selection colour in your form design, or add the following during initialisation:

                gridView.Rows[e.RowIndex].DefaultCellStyle.SelectionBackColor = Color.Red; // or other colour

                And add something similar to the following to your cell click event:

                gridView.Rows[e.RowIndex].Selected = true;

                N Offline
                N Offline
                NarVish
                wrote on last edited by
                #7

                In CellClick event, below statement worked. Thank you. dgvTestCaseDetails.Rows[e.RowIndex].Selected = true;

                1 Reply Last reply
                0
                • L Lost User

                  Strange how we both chose Red. ;)

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  :laugh: Easy to type!

                  If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups