[csharp] /// <summary> /// DataGridView添加行號 /// </summary> /// <param name="dgv">DataGridView控件ID</param> public static void AddRowIndex(this DataGridView dgv) { dgv.RowPostPaint += delegate(object sender, DataGridViewRowPostPaintEventArgs e) { SolidBrush b = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor); e.Graphics.DrawString((e.RowIndex + 1).ToString(), dgv.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4); }; } /// <summary> /// DataGridView添加全選 /// </summary> /// <param name="dgv">DataGridView控件ID</param> /// <param name="columnIndex">全選所在列序號</param> public static void AddFullSelect(this DataGridView dgv, int columnIndex) { if (dgv.Rows.Count < 1) { return; } CheckBox ckBox = new CheckBox(); Rectangle rect = dgv.GetCellDisplayRectangle(1, -1, true); ckBox.Size = new Size(dgv.Columns[1].Width - 12, 12); //大小 Point point = new Point(rect.X + 10, rect.Y + 3);//位置 ckBox.Location = point; ckBox.CheckedChanged += delegate(object sender, EventArgs e) { for (int i = 0; i < dgv.Rows.Count; i++) { dgv.Rows[i].Cells[columnIndex].Value = ((CheckBox)sender).Checked; } dgv.EndEdit(); }; dgv.Controls.Add(ckBox); }