DataGridView 隔行顯示不同的顏色


兩種方法
第一種
DataGridview1.Rows[i].DefultCellStyle.backcolor
第二種
AlternatingRowsDefutCellstyle 屬性
獲取或設置應用於DataGridview的奇數行的默認單元格樣式。

RowsDefultCellStyle 屬性
  獲取或設置應用於DataGridview的行單元格的默認樣式。

        private void Form1_Load(object sender, EventArgs e)
        {
            string str = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1";
            SqlConnection mycon = new SqlConnection(str);
            try
            {
                mycon.Open();
                DataSet mydt = new System.Data.DataSet();//建立填充數據庫
                SqlDataAdapter mydpt = new SqlDataAdapter("select * from book",mycon);//建立適配器
                mydpt.Fill(mydt);
                dataGridView1.DataSource = mydt.Tables[0];//數據源綁定的是表不是數據庫,所以要指定表,索引值從0開始  說明book這個表是數據庫中第一個表

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

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                mycon.Close();
            
            }
        }

 

        private void Form1_Load(object sender, EventArgs e)
        {
            string str = "server=192.168.100.222;user=sa;pwd=p@ssw1rd;database=pwd1";
            SqlConnection mycon = new SqlConnection(str);
            try
            {
                mycon.Open();
                DataSet mydt = new System.Data.DataSet();//建立填充數據庫
                SqlDataAdapter mydpt = new SqlDataAdapter("select * from book",mycon);//建立適配器
                mydpt.Fill(mydt);
                dataGridView1.DataSource = mydt.Tables[0];//數據源綁定的是表不是數據庫,所以要指定表,索引值從0開始  說明book這個表是數據庫中第一個表

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (i % 2 == 0)
                    {
                        this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Beige;
                    }
                    else
                    {
                        this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    
                    }
                }
        
         //第二種方法
                this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red;//DataGridView行單元格默認顏色
                this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Violet;//奇數行單元格默認顏色

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            finally
            {
                mycon.Close();
            
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM