C# DataGridView 常用操作

来源:互联网 发布:在线视频网站cms 编辑:程序博客网 时间:2024/05/16 09:47

DataGridView左侧显示行号方法

DataGridView的RowPostPaint事件


private void planDataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)        {            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,                e.RowBounds.Location.Y,                PlanDataGridView.RowHeadersWidth - 4,                e.RowBounds.Height);            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),                PlanDataGridView.RowHeadersDefaultCellStyle.Font,                rectangle,                PlanDataGridView.RowHeadersDefaultCellStyle.ForeColor,                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);        }

设置DataGridView默认不选中单元格或行

窗口载入事件,否则不起作用

private void MainForm_Load(object sender, EventArgs e)        {            ///窗口加载时默认不选中单元格            PlanDataGridView.ClearSelection();            // or  PlanDataGridView.CurrentCell = null;        }


DataGridView单击一个单元格选择一行

this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//设置为整行被选中

0 0