dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gold;第一次无效

private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{}//修改
DataGridViewCellStyle sty=new DataGridViewCellStyle();
dgv.Rows[e.RowIndex].DefaultCellStyle = sty;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果C#的DataGridView控件中的Cells[j].Visible是只读的,您可以使用以下方法来合并第二行数据相同的列并且居中: ```csharp private void MergeRows() { int rowCount = dataGridView1.Rows.Count; for (int i = rowCount - 2; i >= 0; i--) { DataGridViewRow currentRow = dataGridView1.Rows[i]; DataGridViewRow previousRow = dataGridView1.Rows[i + 1]; for (int j = 0; j < dataGridView1.Columns.Count; j++) { if (currentRow.Cells[j].Value == previousRow.Cells[j].Value) { // 设置当前单元格的高度为前一行单元格的高度 currentRow.Cells[j].Style.Padding = new Padding(0, 0, 0, 0); currentRow.Cells[j].Style.Alignment = DataGridViewContentAlignment.MiddleCenter; currentRow.Cells[j].Style.BackColor = previousRow.Cells[j].Style.BackColor; currentRow.Cells[j].Style.ForeColor = previousRow.Cells[j].Style.ForeColor; DataGridViewTextBoxCell cell = new DataGridViewTextBoxCell(); cell.Value = ""; cell.Style.BackColor = dataGridView1.BackgroundColor; currentRow.Cells[j] = cell; // 隐藏前一行单元格 previousRow.Cells.RemoveAt(j); previousRow.Cells.Insert(j, new DataGridViewTextBoxCell()); previousRow.Cells[j].Style.BackColor = dataGridView1.BackgroundColor; previousRow.Cells[j].Style.ForeColor = dataGridView1.BackgroundColor; } } } // 居中显示 DataGridViewCellStyle style = new DataGridViewCellStyle(); style.Alignment = DataGridViewContentAlignment.MiddleCenter; foreach (DataGridViewColumn col in dataGridView1.Columns) { col.DefaultCellStyle = style; } } ``` 在这个方法中,我们使用RemoveAt和Insert方法来隐藏前一行单元格,以达到合并单元格的效果。我们还将前一行单元格的背景色和前景色设置为与DataGridView的背景色相同,以便它们在视觉上消失。最后,我们将所有列的单元格对齐方式设置为居中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值