datagridview样式设置

1.DataGridView样式设置
public   static   void   InitDataGridView(DataGridView   dgv) 
                { 
                        //只读属性设置 
                        dgv.ReadOnly   =   true; 
                        //尾行自动追加 
                        dgv.AllowUserToAddRows   =   false; 
                        dgv.AllowUserToDeleteRows   =   false; 
                        //行幅自动变化               
                        dgv.AllowUserToResizeRows   =   true; 
                        //高度设定               
                        dgv.ColumnHeadersHeightSizeMode   =   DataGridViewColumnHeadersHeightSizeMode.DisableResizing; 
                        //标头设定                 
                        dgv.RowHeadersVisible   =   true; 
                        //标题行高 
                        dgv.ColumnHeadersHeight   =   25; 
                        dgv.RowTemplate.Height   =   23; 
                        //行选择设定 
                        dgv.SelectionMode   =   DataGridViewSelectionMode.FullRowSelect; 
                        //多行选择 
                        dgv.MultiSelect   =   false; 
                        //选择状态解除 
                        dgv.ClearSelection(); 
                        //head文字居中       
                        dgv.ColumnHeadersDefaultCellStyle.Alignment   =   DataGridViewContentAlignment.MiddleCenter; 
                        //选择状态的行的颜色 
                        dgv.DefaultCellStyle.SelectionBackColor   =   Color.LightSteelBlue; 
                        dgv.DefaultCellStyle.SelectionForeColor   =   Color.Black; 
                        //设定交替行颜色 
                        dgv.AlternatingRowsDefaultCellStyle.BackColor   =   Color.White; 
                        dgv.RowsDefaultCellStyle.BackColor   =   Color.LightGray; 
                        //行副填充时自动调整宽度 
                        dgv.AutoSizeColumnsMode   =   DataGridViewAutoSizeColumnsMode.Fill; 
                        dgv.AutoGenerateColumns   =   false; 
                        //可否手动调整行大小 
                        dgv.AllowUserToResizeRows   =   false; 
                        dgv.AutoGenerateColumns   =   false; 
                }
2.下面是一个根据DataGridView列的值,改变行的颜色的例子
foreach  (DataGridViewRow row  in  mydataGridView.Rows)
{    
   string  RowType  =  row.Cells[0].Value.ToString();  
   if  (RowType  ==   " Type A " )   
 {       
     row.DefaultCellStyle.BackColor  =  Color.Red;   
     row.DefaultCellStyle.ForeColor  =  Color.White;  
 }   
   else   if  (RowType  ==   " Type B " )  
   {      
   row.DefaultCellStyle.BackColor  =  Color.Yellow;   
      row.DefaultCellStyle.ForeColor  =  Color.Black;   
  } 

添加上面这段代码到UpdateDataGridViewRowColors() 方法中,你的DataGridView绑定或者从新绑定数据的时候调用它。
3.DataGridView中的行如何根据不同的值显示不同的行背景色
private   void   dataGridView1_RowPrePaint(object   sender,   DataGridViewRowPrePaintEventArgs   e)   
                  {   
                          if   (e.RowIndex   >=   dataGridView1.Rows.Count)   
                                  return;   
                          DataGridViewRow   dgr   =   dataGridView1.Rows[e.RowIndex];   
                          try   
                          {   
                                  if   (dgr.Cells["列名"].Value.ToString()   ==   "比较值")   
                                  {   
                                          dgr.DefaultCellStyle.BackColor   =   设置的颜色;   
                                  }   
                          }   
                          catch   (Exception   ex)   
                          {   
                                  MessageBox.Show(ex.Message);   
                          }   

                  }  
4.在C#中如何通过编程实现让DataGridView控件隔行显示不同的颜色。如果该dataGridView是跟数据库绑定的,则可以触发DataBindingComplete事件。
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) 
  { 
  if (this.dataGridView1.Rows.Count != 0) 
  { 
  for (int i = 0; i < this.dataGridView1.Rows.Count; ) 
  { 
  this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Pink; 
  i += 2; 
  } 
  } 
  }
如果没有绑定数据库,那么当dataGridView中的数据有所改变或显示的时候可以添加以下的代码:
if (this.dataGridView1.Rows.Count != 0) 
  { 
  for (int i = 0; i < this.dataGridView1.Rows.Count; ) 
  { 
  this.dataGridView1.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Pink; 
  i += 2; 
  } 
  }
5.有时我们希望DataGridView在加载数据时,能够根据表记录中某些数据的值,做一个判断,根据判断的结果,将对应的记录显示成不同的背景颜色,例如我们希望学生信息表中如果是男生,则将性别显示成红色背景。
这可以通过自定义DataGridView控件的CellFormatting事件,来实现,具体代码如下:
注意因为DataGridView控件在显示数据时,会自动在表记录,末尾记录后天就一行空的记录,这时DataGridView1.Rows.Count等于表中实际记录个数+1,所以判断时,要防止DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value是空值,因此需要加上e.RowIndex < DataGridView1.Rows.Count - 1这个条件。当然了,也有其他的判断方法可以用。
Private Sub DataGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    If (e.RowIndex >= 0 And e.ColumnIndex >= 0 And e.RowIndex < DataGridView1.Rows.Count - 1) Then
        If (DataGridView1.Columns(e.ColumnIndex).Name = "性别") Then
            Dim sex As String = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
            If Not IsDBNull(sex) Then
                If (sex.ToString() = "男") Then
                    e.CellStyle.BackColor = Color.Red
                End If
            End If
        End If
6.for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历DataGridView的每一行
            {
                if (i%2 != 0)  //如果是奇数行
                {
                  dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gray; //设置此行的背景颜色为灰色
                }
            } 
7.Winform中的DataGridView如何给每一行设置颜色,实现隔行换色显示的效果,或者根据其他不同颜色的效果。只需要在DataGridView的RowPrePaint事件中定义颜色代码即可。
private void Gv_Web_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)        
        {        
            DataGridViewRow dgr = Gv_Web.Rows[e.RowIndex];    
                //获得DataGridViewRow     
            if (dgr.Cells["state"].Value.ToString() == "开启")    
                //如果状态为开启,条件可以自定义     
            {        
                dgr.DefaultCellStyle.BackColor = Color.LightGreen;    
                //设置行背景色为浅绿色     
            }        
            else       
            {        
                dgr.DefaultCellStyle.BackColor = Color.LightPink;    
                //设置行背景色为浅粉色     
            }        
        }    
8.winform DataGridView根据光标位置设置所在行的背景颜色。
private void gv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                object prevIndex = gv.Tag;
                if (prevIndex == null || !prevIndex.Equals(e.RowIndex))
                {
                    gv.Tag = e.RowIndex;
                    gv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                    gv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Blue;
                }
            }
        }
        private void gv_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                gv.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
                gv.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.FromArgb(0, 64, 64);
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮特大熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值