this.dataGridView1.RowHeadersVisible = false;
可以隐藏 我想说的那列。 我现在想做的是 把这列 给添加一个自动增长的编号 。 我该怎么操作这列呢?
可以隐藏 我想说的那列。 我现在想做的是 把这列 给添加一个自动增长的编号 。 我该怎么操作这列呢?
解决方案 »
- C# System.ObjectDisposedException 错误
- 怎样在一个类的构造函数里面获得泛型T的Type?
- 关于C# winform全屏显示 有没有更好的办法?
- 為何我的數據集中某一表中同一字段出現兩次,第二個後面跟了數字1
- 调查:有没有做小灵通短信开发的,集合啦!
- 为什么这个设置DataGrid任意行行高的方法只对DataGrid基础类有效,对自定义的DataGrid就会出错,大家帮帮忙吧,我快神精了
- c# winform 中如何获取窗体上所有的控件包括容器里的控件
- 在C#中如何使用自定义消息啊
- 关于DataGrid绑定数据后,显示错误的问题
- C#完全参考手册下载 (看这个入门足以..)
- 要显示数据库中所有的图像数据,怎么做好?
- 初次接触WCF,请大家告诉我它能做什么,在什么领域用到WCF,谢谢!
private void button1_Click(object sender, EventArgs e)
{
DataTable dt = ((DataTable)(dataGridView1.DataSource));
DataColumn dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32"); //指定该列的数据类型
dc.AutoIncrement = true; //该列为自动增涨列
dc.AutoIncrementSeed = 1; //初始值
dc.AutoIncrementStep = 1; //增量
dc.Caption = "id"; //设置列的标题
dc.ColumnName = "序号"; //设置 列集合对象中的列的名称,datagrid中显示该列名.
dc.Unique = true; //为此列创建唯一性约
dc.AllowDBNull = false; //不允许为空
dt.Columns.Add(dc);
}
/// <summary>
/// 行头写入行号和重绘行头
/// </summary>
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e) {
Rectangle rectangle = new Rectangle(
e.RowBounds.Location.X,
e.RowBounds.Location.Y,
this.RowHeadersWidth - 1,
e.RowBounds.Height); bool mouseOver = e.RowBounds.Contains(this.PointToClient(Cursor.Position)); LinearGradientBrush brush = new LinearGradientBrush(
e.RowBounds,
mouseOver ? Color.PeachPuff : Color.LightGray,
Color.WhiteSmoke,
LinearGradientMode.Vertical); e.Graphics.FillRectangle(brush, rectangle);//重绘行头内部
e.Graphics.DrawRectangle(new Pen(mouseOver ? Color.PeachPuff : Color.LightGray), rectangle);//重绘行头边框 if (this.Rows[e.RowIndex].Selected == true) {
brush = new LinearGradientBrush(
e.RowBounds,
Color.CadetBlue,
Color.WhiteSmoke,
LinearGradientMode.Vertical);
e.Graphics.FillRectangle(brush, rectangle);
e.Graphics.DrawRectangle(new Pen(Color.CadetBlue), rectangle);
} //行头写入行号
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.RowHeadersDefaultCellStyle.Font, rectangle, this.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter); base.OnRowPostPaint(e);
}
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,dataGridView1.RowHeadersWidth, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvCommon.RowHeadersDefaultCellStyle.Font, rectangle, dgvCommon.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);