zoukankan      html  css  js  c++  java
  • 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();
                
                }
            }
  • 相关阅读:
    状压DP【p1879】[USACO06NOV]玉米田Corn Fields
    Tarjan缩点+Spfa最长路【p3627】[APIO2009] 抢掠计划
    Tarjan缩点【p1726】上白泽慧音
    分层图【p4822】[BJWC2012]冻结
    Tarjan缩点+LCA【p2783】有机化学之神偶尔会做作弊
    线段树【p1607】[USACO09FEB]庙会班车Fair Shuttle
    better-scroll踩坑合集
    在浏览器上安装 Vue Devtools工具
    无法执行vue初始化命令
    vue-cli创建第一个项目(用git bash解决上下键移动选择问题)
  • 原文地址:https://www.cnblogs.com/xiaowie/p/8651684.html
Copyright © 2011-2022 走看看