日期:2014-05-17  浏览次数:20909 次

UserControl中Load事件设置datagridview颜色无效
1.下面这个是UserControl1中的Load事件的代码:

private void UserControl1_Load(object sender, EventArgs e)
        {
            DataTable dt=new DataTable();
            dt.Columns.Add("test");
            for (int i = 0; i < 6; i++)
            {
                DataRow dataRow=dt.NewRow();
                dataRow["test"] = "test";
                dt.Rows.Add(dataRow);
            }

            dataGridView1.DataSource = dt;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (i % 2 == 0)
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
                else
                {
                    dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.RoyalBlue;
                }
            }

        }


2.引用UserControl1控件的Form


加载的用户控件没有背景色,只剩下20分了,请帮忙看看,谢谢!

------解决方案--------------------
你想设置隔行的背景色不用这么麻烦,在用户控件中选择dataGridView控件,设置一下属性就可以了。
 dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.RoyalBlue;

------解决方案--------------------
引用:
你想设置隔行的背景色不用这么麻烦,在用户控件中选择dataGridView控件,设置一下属性就可以了