1.动态创建行,有两种方式,整理来自于链接https://www.cnblogs.com/allan5204/p/4113080.htm,下面为整理代码,
其中方式2的代码中包含了初始化单元格的背景色与前景色功能及单元格文字居中功能。
//方式1,每行的单元格中添加文本信息.
Random rd = new Random();
for (int i = 0; i < 5; i++)
{
int index = this.dataGridView1.Rows.Add();
this.dataGridView1.Rows[index].Cells[0].Value = "Component" + i;
float l_value1 = (float)rd.Next(100, 900) / (float)10;
this.dataGridView1.Rows[index].Cells[1].Value = string.Format("{0:0.00}", l_value1);
this.dataGridView1.Rows[index].Cells[2].Value = "%";
this.dataGridView1.Rows.Add(row);
}
//方式2,每行的单元格中添加控件.
Random rd = new Random();
for (int i = 0; i < 5; i++)
{
DataGridViewRow row = new DataGridViewRow();
DataGridViewButtonCell dg_btn_cell = new DataGridViewButtonCell();
dg_btn_cell.Value = "Component" + i;
//下面代码设置单元格的背景色与前景色.
dg_btn_cell.Style.BackColor = Color.DarkGray;
dg_btn_cell.Style.ForeColor = Color.Black;
//设置文字居中
dg_btn_cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
row.Cells.Add(dg_btn_cell);
float l_value1 = (float)rd.Next(100, 900) / (float)10;
DataGridViewTextBoxCell dg_txt_cell = new DataGridViewTextBoxCell();
dg_txt_cell.Value = string.Format("{0:0.00}", l_value1);
//下面代码设置单元格的背景色与前景色.
dg_txt_cell.Style.BackColor = Color.Black;
dg_txt_cell.Style.ForeColor = Color.Lime;
//设置文字居中
dg_txt_cell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
row.Cells.Add(dg_txt_cell);
DataGridViewTextBoxCell dg_txt_cell_1 = new DataGridViewTextBoxCell();
dg_txt_cell_1.Value = "%";
//下面代码设置单元格的背景色与前景色.
dg_txt_cell_1.Style.BackColor = Color.Black;
dg_txt_cell_1.Style.ForeColor = Color.Cyan;
//设置文字居中
dg_txt_cell_1.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
row.Cells.Add(dg_txt_cell_1);
dataGridView1.Rows.Add(row);
}
2. 动态创建列,整理来自链接 https://www.cnblogs.com/LuoEast/p/8213562.html,代码中包含了禁止列排序的功能.
DataGridViewTextBoxColumn dg_column = new DataGridViewTextBoxColumn();
dg_column.Name = "column_component";
dg_column.DataPropertyName = "column_component";
dg_column.HeaderText = "Component";
//禁止列排序
dg_column.SortMode = DataGridViewColumnSortMode.NotSortable;
dataGridView1.Columns.Add(dg_column);
3.动态修改单元格的前景色与背景色.
整理来自链接https://bbs.csdn.net/topics/370054555
private void dataGridView1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex == 1)
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.ForeColor = Color.Blue;
}
}
4.隐藏自动生成的左边列
整理来自链接https://bbs.csdn.net/topics/120004565
dataGridView1.RowHeadersVisible = false;
5.设置所有行的字体
整理来自链接dataGridView设置数据字体格式_datagridview defaultcellstyle.font = new font(col.-CSDN博客
dataGridView1.RowsDefaultCellStyle.Font = new Font("宋体", 10F, FontStyle.Regular);
6.设置标题的字体
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 20F, FontStyle.Regular);
7.设置标题的高度
整理来自链接https://bbs.csdn.net/wap/topics/360262266
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridView1.ColumnHeadersHeight = 40;
8.设置标题的前景色与背景色
整理来自链接https://bbs.csdn.net/topics/390561358,修改DataGridView背景颜色和选中颜色_avalonia 修改表格背景色-CSDN博客
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Red;
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor= Color.Blue;
9.设置标题文字居中
整理来自链接https://bbs.csdn.net/wap/topics/360262266
dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;