DataGridView常用操作

1、自动适应列宽
DataGridView.AutoSizeColumns(DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows);
DataGridView.AutoSizeColumn(DataGridViewAutoSizeColumnCriteria.HeaderOnly, 2, false);
DataGridView.AutoSizeRow(DataGridViewAutoSizeRowCriteria.Columns, 2, false);
DataGridView.AutoSizeRows(DataGridViewAutoSizeRowCriteria.HeaderAndColumns, 0, dataGridView1.Rows.Count, false);

2、改变表格线条风格
this.dataGridView1.GridColor = Color.BlueViolet;
this.dataGridView1.BorderStyle = BorderStyle.Fixed3D;
this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
this.dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;

3、动态改变列是否显示,和动态改变列的显示顺序
customersDataGridView.Columns["CustomerID"].Visible = false;
customersDataGridView.Columns["ContactName"].DisplayIndex = 0;
customersDataGridView.Columns["ContactTitle"].DisplayIndex = 1;
customersDataGridView.Columns["City"].DisplayIndex = 2;
customersDataGridView.Columns["Country"].DisplayIndex = 3;
customersDataGridView.Columns["CompanyName"].DisplayIndex = 4;

4、在列中显示图像
Icon treeIcon = new Icon(this.GetType(), "tree.ico");
DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
iconColumn.Image = treeIcon.ToBitmap();
iconColumn.Name = "Tree";
iconColumn.HeaderText = "Nice tree";
dataGridView1.Columns.Insert(2, iconColumn);

5、格式化显示内容
this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c";
this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d";
this.dataGridView1.DefaultCellStyle.NullValue = "no entry";
this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewWrapMode.Wrap;
this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment =DataGridViewContentAlignment.MiddleRight;

6、更改列名
dataGridView1.Columns[0].HeaderText = "序号";
dataGridView1.Columns["SortID"].HeaderText = "分类ID";

posted @ 2010-12-13 15:23  顺德早茶  阅读(1039)  评论(1编辑  收藏  举报