locked
Datagridview row background color RRS feed

  • Question

  • Hello Everyone,

    This seems so simple, but I'm having all kinds of problems.  The problem is that when I change the color of the rows on a datagrid, I cannot change the color of all the rows back to the color I want.  I can change the individual rows, but I'm sure there's a way to change all of the rows' background colors without having to iterate through each row.  The DefaultCellsStyle, AlternatingRowsDefaultCellsStyle, and RowsDefaultCellStyle have all been set to display rows with a white background. 

    Specifically, my problem is that after a gridview is populated, then I make a change to a some rows' color, such as DataGridView1.CurrentRow.DefaultCellStyle=Color.Red.  I can change the color of each selected row through: DataGridView1.Rows(ChgTheRow).DefaultCellStyle.BackColor = Color.White.  However, I cannot change the color of all the rows without iterating through each row.  I thought that simply changing either DefaultCellStyle or RowsDefaultCellStyle would work, such as DataGridView1.DefaultCellStyle.BackColor=Color.White or DataGridView1.DefaultRowsCellStyle.BackColor=Color.White, but that does nothing, the rows remain the same color.  I get the feeling I'm missing something very simple here, any suggestions?

    STI

    novice
    Wednesday, January 14, 2009 6:17 PM

Answers

  • hi ,
    Suppose u r Datagridview Name is DgView

    DgView.Rows(<Rowno>).DefaultCellStyle.BackColor   =  Color.Red




    Simillarly u can change the Forecolor And other properties.




    Thanks

    Satya Ranjan Biswal
    Wednesday, January 14, 2009 6:45 PM

All replies

  • hi ,
    Suppose u r Datagridview Name is DgView

    DgView.Rows(<Rowno>).DefaultCellStyle.BackColor   =  Color.Red




    Simillarly u can change the Forecolor And other properties.




    Thanks

    Satya Ranjan Biswal
    Wednesday, January 14, 2009 6:45 PM
  • Stop_the_insanity said:

    Hello Everyone,

    This seems so simple, but I'm having all kinds of problems.  The problem is that when I change the color of the rows on a datagrid, I cannot change the color of all the rows back to the color I want.  I can change the individual rows, but I'm sure there's a way to change all of the rows' background colors without having to iterate through each row.  The DefaultCellsStyle, AlternatingRowsDefaultCellsStyle, and RowsDefaultCellStyle have all been set to display rows with a white background. 

    Specifically, my problem is that after a gridview is populated, then I make a change to a some rows' color, such as DataGridView1.CurrentRow.DefaultCellStyle=Color.Red.  I can change the color of each selected row through: DataGridView1.Rows(ChgTheRow).DefaultCellStyle.BackColor = Color.White.  However, I cannot change the color of all the rows without iterating through each row.  I thought that simply changing either DefaultCellStyle or RowsDefaultCellStyle would work, such as DataGridView1.DefaultCellStyle.BackColor=Color.White or DataGridView1.DefaultRowsCellStyle.BackColor=Color.White, but that does nothing, the rows remain the same color.  I get the feeling I'm missing something very simple here, any suggestions?

    STI


    novice

    I cant remember there is anything to reset it without iterating. But looping is not a big problem is it? Depends on the size of datatable :)


    Arjun Paudel
    Wednesday, January 14, 2009 6:53 PM
  • Stop_the_insanity said:

    Specifically, my problem is that after a gridview is populated, then I make a change to a some rows' color, such as DataGridView1.CurrentRow.DefaultCellStyle=Color.Red.  I can change the color of each selected row through: DataGridView1.Rows(ChgTheRow).DefaultCellStyle.BackColor = Color.White.  However, I cannot change the color of all the rows without iterating through each row.  I thought that simply changing either DefaultCellStyle or RowsDefaultCellStyle would work, such as DataGridView1.DefaultCellStyle.BackColor=Color.White or DataGridView1.DefaultRowsCellStyle.BackColor=Color.White, but that does nothing, the rows remain the same color.  I get the feeling I'm missing something very simple here, any suggestions?


    Thank you for your friendly help.

    Hi,
    Welcome to MSDN forums!

    I test it with the following code sample, and it works fine which can c
    hange back color for all content rows in DataGridView without iterating through each row. Please give it a try again.

    Prerequisites: DataGridView1 and Button1 on Form1.

    Imports System.Data.OleDb  
     
    Public Class Form1  
     
        Private Sub Form1_Load(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles MyBase.Load  
            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=E:\myDB.mdb")  
            Dim cmd As OleDbCommand = New OleDbCommand("Select * From UserTable", con)  
            con.Open()  
            Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)  
            Dim myDataSet As DataSet = New DataSet()  
            myDA.Fill(myDataSet, "MyTable")  
            DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView  
        End Sub 
     
        ' Change back color for all content rows in DataGridView  
        Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
            DataGridView1.DefaultCellStyle.BackColor = Color.Green 'Works fine  
            DataGridView1.RowsDefaultCellStyle.BackColor = Color.Yellow 'Works fine  
        End Sub 
     
    End Class 

    Here is the illustration:


    Best regards,
    Martin Xie
    Monday, January 19, 2009 7:58 AM

  • "I can change the color of each selected row through: DataGridView1.Rows(ChgTheRow).DefaultCellStyle.BackColor = Color.White.  However, I cannot change the color of all the rows without iterating through each row."


     Dear Martin!
    As far as I understand OP wants to reset colour of all cells after setting it. He knows how to change the cell color, but his problem is resetting.

    Thanks for the response

    "This seems so simple, but I'm having all kinds of problems.  The problem is that when I change the color of the rows on a datagrid, I cannot change the color of all the rows back to the color I want.  I can change the individual rows, but I'm sure there's a way to change all of the rows' background colors without having to iterate through each row.  The DefaultCellsStyle, AlternatingRowsDefaultCellsStyle, and RowsDefaultCellStyle have all been set to display rows with a white background. "


    Arjun Paudel
    Monday, January 19, 2009 8:04 AM