C# DataGridView
一、使用总结
1.改变某一列标题颜色
dgv.EnableHeadersVisualStyles = false;
dgv.Columns[i].HeaderCell.Style.BackColor = Color.Red;
2.添加一列button
DataGridViewButtonColumn btn = new DataGridViewButtonColumn(); btn.Name = "btnModify"; btn.UseColumnTextForButtonValue = true; btn.HeaderText = ""; btn.Text = "修改"; dgv.Columns.Add(btn);
获取点击触发事件
private void dgvCalibration_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (dgvCalibration.Columns[e.ColumnIndex].Name == "btnModify") { MessageBox.Show("行: " + e.RowIndex.ToString() + ", 列: " + e.ColumnIndex.ToString() + "; 被点击了"); } }
更改button样式
DataGridViewButtonCell buttonCell = (DataGridViewButtonCell)dgvCalibration.Rows[1].Cells[10]; buttonCell.FlatStyle = FlatStyle.Popup; buttonCell.Style.BackColor = System.Drawing.Color.Red; buttonCell.Style.ForeColor = System.Drawing.Color.LightGreen;
3.标题居中
// Set the column header style. DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle(); columnHeaderStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; columnHeaderStyle.BackColor = Color.Beige; columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Regular); dgv.ColumnHeadersDefaultCellStyle = columnHeaderStyle; int gdvCellWidth = dgv.Width / headerList.Count- 2; for (int i = 0; i < headerList.Count; i++) { dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; dgv.Columns[i].Name = headerList[i]; dgv.Columns[i].Width = gdvCellWidth; }
4.DataGridView 大小改变,单元格大小跟着改变,占满
private void dgvCalibration_SizeChanged(object sender, EventArgs e) { if (colsCount > 0) { int gdvCellWidth = dgvCalibration.Width / colsCount - 2; for (int i = 0; i < colsCount; i++) { dgvCalibration.Columns[i].Width = gdvCellWidth; } int gdvRowHight = dgvCalibration.Height / (rowsCount + 2); for (int i = 0; i < rowsCount; i++) { dgvCalibration.Rows[i].Height = gdvRowHight; } } }
二、代码举例
private void InitDataGridView() { // Create an unbound DataGridView by declaring a column count. dataGridView1.ColumnCount = 3; dataGridView1.ColumnHeadersVisible = true; dataGridView1.AllowUserToAddRows = false; dataGridView1.AllowUserToDeleteRows = false; dataGridView1.AllowUserToOrderColumns = false; dataGridView1.MultiSelect = false; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.ReadOnly = true; //数据居中 dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //标题居中 dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //设置标题高度 dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; dataGridView1.ColumnHeadersHeight = 40; // Set the column header style. DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle(); columnHeaderStyle.BackColor = Color.Beige; columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Regular); dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle; // Set the column header names. dataGridView1.Columns[0].Name = "C1"; dataGridView1.Columns[0].Width = 40; dataGridView1.Columns[1].Name = "C2"; dataGridView1.Columns[1].Width = 90; dataGridView1.Columns[2].Name = "C3"; dataGridView1.Columns[2].Width = 60; } private void AddDataGridViewData() { for (int i = 0; i < 3; i++) { int index = this.dataGridView1.Rows.Add(); this.dataGridView1.Rows[index].Cells[0].Value = i.ToString() + "1"; this.dataGridView1.Rows[index].Cells[1].Value = i.ToString() + "2"; this.dataGridView1.Rows[index].Cells[2].Value = i.ToString() + "3"; } } private string GetSelectedValue() { int index= dataGridView1.CurrentRow.Index; return dataGridView1.Rows[index].Cells["C2"].Value.ToString(); } //清除DataGridView数据 private void DataGridViewClear() { for (int i = 0; i < dataGridView1.RowCount; i++) { dataGridView1.Rows.Remove(dataGridView1.Rows[i]); i--; } } //右键显示菜单 private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right) { if (e.RowIndex >= 0) { //若行已是选中状态就不再进行设置 if (dataGridView1.Rows[e.RowIndex].Selected == false) { dataGridView1.ClearSelection(); dataGridView1.Rows[e.RowIndex].Selected = true; } //只选中一行时设置活动单元格 if (dataGridView1.SelectedRows.Count == 1) { dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]; } //弹出操作菜单 contextMenuStrip1.Show(MousePosition.X, MousePosition.Y); } } }
public void InitDataGridView(DataGridView dgv) { // Create an unbound DataGridView by declaring a column count. dgv.ColumnCount = 10; dgv.ColumnHeadersVisible = true; dgv.RowHeadersVisible = false; dgv.AllowUserToAddRows = false; dgv.AllowUserToDeleteRows = false; dgv.AllowUserToOrderColumns = false; dgv.MultiSelect = false; //dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dgv.ReadOnly = false; // Set the column header names. //添加一列checkbox,会多一列空白 DataGridViewCheckBoxColumn ChCol0 = new DataGridViewCheckBoxColumn(); ChCol0.Name = "Used"; ChCol0.Width = 40; ChCol0.TrueValue = "1"; ChCol0.FalseValue = "0"; dgv.Columns.Insert(0, ChCol0); dgv.Columns[1].Name = "Addr"; dgv.Columns[1].Width = 40; dgv.Columns[2].Name = "SubAddr"; dgv.Columns[2].Width = 60; //dgv.Columns[3].Name = "Type"; //dgv.Columns[3].Width = 80; DataGridViewComboBoxColumn cbCol3 = new DataGridViewComboBoxColumn(); cbCol3.Items.Add("Single"); cbCol3.Items.Add("Muti"); cbCol3.Name = "Type"; cbCol3.Width = 80; dgv.Columns.Insert(3, cbCol3); dgv.Columns[4].Name = "Tilts"; dgv.Columns[4].Width = 100; dgv.Columns[5].Name = "Suffix"; dgv.Columns[5].Width = 100; dgv.Columns[6].Name = "Calibrate"; dgv.Columns[6].Width = 80; dgv.Columns[6].ReadOnly = true; dgv.Columns[7].Name = "NG"; dgv.Columns[7].Width = 60; dgv.Columns[7].ReadOnly = true; dgv.Columns[8].Name = "SetTilt"; dgv.Columns[8].Width = 60; dgv.Columns[8].ReadOnly = true; dgv.Columns[9].Name = "NG"; dgv.Columns[9].Width = 60; dgv.Columns[9].ReadOnly = true; //多一列空白的删除 dgv.Columns.RemoveAt(10); dgv.Columns.RemoveAt(10); //数据居中 dgv.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; //设置标题高度 dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing; dgv.ColumnHeadersHeight = 30; // Set the column header style. DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle(); columnHeaderStyle.BackColor = Color.Beige; //columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Regular); dgv.ColumnHeadersDefaultCellStyle = columnHeaderStyle; //指定列的颜色 DataGridViewCellStyle column67Style = new DataGridViewCellStyle(); column67Style.BackColor = Color.DeepSkyBlue; dgv.Columns[6].DefaultCellStyle = column67Style; dgv.Columns[7].DefaultCellStyle = column67Style; DataGridViewCellStyle column89Style = new DataGridViewCellStyle(); column89Style.BackColor = Color.LightSeaGreen; dgv.Columns[8].DefaultCellStyle = column89Style; dgv.Columns[9].DefaultCellStyle = column89Style; //去除标题排序箭头 for (int i = 0; i < 10; i++) { dgv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } //标题居中 dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } public void AddDataToDataGridView(DataGridView dgv) { int index = dgv.Rows.Add(); dgv.Rows[index].Cells[0].Value = "1"; dgv.Rows[index].Cells[1].Value = "1"; dgv.Rows[index].Cells[2].Value = "1"; dgv.Rows[index].Cells[3].Value = "Single"; dgv.Rows[index].Cells[4].Value = "2 4 6"; dgv.Rows[index].Cells[5].Value = "-YUI"; }
单元格内容显示不同的颜色
private void dgvTask_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { if (e.RowIndex < 0) { return; } DataGridViewRow dgr = dgvTask.Rows[e.RowIndex]; if(e.ColumnIndex == 3) { if (dgr.Cells[3].Value.ToString() == TaskStatus.execute) { e.CellStyle.ForeColor = Color.Green;//将前景色设置为红色,即字体颜色设置为红色 e.CellStyle.BackColor = Color.Green;//将背景色设置为绿色,即列的颜色设置为红色 // e.CellStyle.BackColor = Color.Green;//将背景色设置为绿色,即列的颜色设置为红色 } } }
e.CellStyle.ForeColor = ColorTranslator.FromHtml("#B3EE3A");