dataGridView是十分有用、方便的数据表格控件。现保存一些有用的语句以方便以后调用,查找。
-
/*初始化dgvFlashTable*/
-
DataSet dsFlashTable = new DataSet();
-
DataTable dtFlashTable = new DataTable("RW_GD_GW");
-
//创建数据行
-
DataRow drFlashTable = dtFlashTable.NewRow();
-
//创建数据列
-
DataColumn DAY_NUM = new DataColumn("DAY_NUM", typeof(int));
-
DataColumn DAY_SUM = new DataColumn("DAY_SUM", typeof(int));
-
DataColumn BL_DAY_NUM = new DataColumn("BL_DAY_NUM", typeof(int));
-
DataColumn GW_NAME = new DataColumn("GW_NAME", typeof(string));
-
//将列添加到表中
-
dtFlashTable.Columns.Add(DAY_NUM);
-
dtFlashTable.Columns.Add(BL_DAY_NUM);
-
dtFlashTable.Columns.Add(DAY_SUM);
-
dtFlashTable.Columns.Add(GW_NAME);
-
//将行中的数据添加进去,随便写的
-
drFlashTable["DAY_NUM"] = 1111; //日完成量
-
drFlashTable["BL_DAY_NUM"] = 11; //日不良量
-
drFlashTable["GW_NAME"] = "GW_NAME";
-
//将行加入到DataTable中
-
dtFlashTable.Rows.Add(drFlashTable);
-
//将DataTable加到DataSet中
-
dsFlashTable.Tables.Add(dtFlashTable);
-
//将DataSet作为dataGridView的数据源
-
dgvFlashTable.DataSource = dsFlashTable.Tables[0].DefaultView;
-
//设置第一行标题的名字
-
dgvFlashTable.Columns[0].HeaderText = "地址";
-
dgvFlashTable.Columns[1].HeaderText = "十进制";
-
dgvFlashTable.Columns[2].HeaderText = "描述";
-
//隐藏第一列
-
dgvFlashTable.RowHeadersVisible = false;
显示第一列行号的办法:
-
//设置行号
-
private void dgvFlashTable_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
-
{
-
var grid = sender as DataGridView;
-
var rowIdx = (e.RowIndex + 1).ToString();
-
-
var centerFormat = new StringFormat()
-
{
-
// right alignment might actually make more sense for numbers
-
Alignment = StringAlignment.Center,
-
LineAlignment = StringAlignment.Center
-
};
-
-
var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
-
e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
-
}
但是这种方法在大数据量的时候性能比较差,每次滚动数据都会触发RowPostPaint事件。
参考资料:
http://blog.chinaunix.net/uid-25498312-id-3798727.html
http://blog.csdn.net/xieyufei/article/details/9769631显示行号
阅读(751) | 评论(0) | 转发(0) |