Sign in to follow this  
faculaganymede

C# DataGridView and Cell Style??

Recommended Posts

C# Gurus, I am creating a simple DataGridView table and trying to set the background colors for some rows, as shown in the code below. Somehow, the last two lines of code don't seem to have any effects when the application is executed. Does anyone know why? I notice if these two lines were put inside a button click function, then they do work.
       public Form1()
        {
            InitializeComponent();

            initTable();
        }

        private void initTable()
        {
            m_dataset.Tables.Add("my table");

            m_dataset.Tables[0].Columns.Add("A");
            m_dataset.Tables[0].Columns.Add("B");

            for(int i=0; i<10; i++)
                m_dataset.Tables[0].Rows.Add(i*10, i*20);

            DataView dv = new DataView(m_dataset.Tables[0]);
            dataGridView1.DataSource = dv;

            dataGridView1.Rows[3].DefaultCellStyle.BackColor = Color.Green;
            dataGridView1.Rows[4].DefaultCellStyle.BackColor = Color.Green;
        }


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this