dataGridView几个小技巧

1)dataGridView隔行变色
RowsDefaultCellStyle :获取或设置应用于 DataGridView 的行单元格的默认样式。
AlternatingRowsDefaultCellStyle 属性:获取或设置应用于 DataGridView 的奇数行的默认单元格样式。
 dataGridView1.RowsDefaultCellStyle.BackColor = Color.Aqua;
 dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;  
2)dataGridView表头加checkbox全选的方法
public class AddCheckBoxToDataGridView
        {
            public static System.Windows.Forms.DataGridView dgv;
            public static void AddFullSelect()
            {                
                if (dgv.Rows.Count < 1)
                {
                    return;
                }
                System.Windows.Forms.CheckBox ckBox = new System.Windows.Forms.CheckBox();                             
                System.Drawing.Rectangle rect = dgv.GetCellDisplayRectangle(0, -1, true);
                ckBox.Size = new System.Drawing.Size(dgv.Columns[1].Width - 12, 12); //大小               
                Point point = new Point(rect.X + 10, rect.Y + 3);
                ckBox.Location = point;//位置
                ckBox.CheckedChanged += new EventHandler(ckBox_CheckedChanged);
                dgv.Controls.Add(ckBox);
            }
            static void ckBox_CheckedChanged(object sender, EventArgs e)
            {
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    dgv.Rows[i].Cells[0].Value = ((System.Windows.Forms.CheckBox)sender).Checked;
                }
                dgv.EndEdit();
            }
        }  
3)鼠标拖动窗口大小时,设定窗口最小尺寸
private void GetSendBankMsg_ResizeEnd(object sender, EventArgs e)
        {
            if (this.Width <= 500)           
                this.Width = 500;  
            if (this.Height <= 335)            
                this.Height = 335;            
        }

4)删除DataGridView没有数据时的默认空白行
dataGridView1.AllowUserToAddRows  = false;
5)让DataGridView的所有列正好占据着整个DataGridView的宽度
把AutoSizeColumnsMode设置为FIll就行了

你可能感兴趣的:(dataGridView几个小技巧)