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

如何使用同一个的函数逻辑来处理不同id的datagridview控件?
大伙好,   我的c#程序里面有5个 datagirdview 控件,需要使用同样的处理逻辑来显示数据。 我想使用一个showdata函数来处理这五个 datagirdview 控件。请问 应该怎么写呢?   我的思路是是 在showdata里面写一个参数,把需要显示的控件名作为参数传递进来处理。但是不知道怎么写,请指点 谢谢。
//下面这段是处理dataGridViewX4控件的代码。我想把它改成可以处理全部5个 dataGridViewX控件的showdata函数。 


        private void dataGridViewX4_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //所以行号的背景色
            for (int i = 0; i < this.dataGridViewX4.Rows.Count; i++)
            {
                if (i % 2 == 0)
                {
                    dataGridViewX4.Rows[i].DefaultCellStyle.BackColor = Color.Lavender;     //偶数行浅蓝色
                }
            }
        }

        private void dataGridViewX4_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
                dataGridViewX4.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Khaki;
            return;
        }

        private void dataGridViewX4_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
            {
                if (e.RowIndex % 2 == 0)
                {
                    dataGridViewX4.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Lavender;     //偶数行浅蓝色
                }
                else
                    dataGridViewX4.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
            }
            return;
        }