让datagridview或girdview指定的一行或多行变色

datagridview的:(注意这种方法不是很好,而且网格线都没有了,自找麻烦了,还用了图形的东西,可以看最下面的那种方法。)利用datagridview中的RowPrePaint事件。 private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) ...{ if (
摘要由CSDN通过智能技术生成

datagridview的:

(注意这种方法不是很好,而且网格线都没有了,自找麻烦了,还用了图形的东西,可以看最下面的那种方法。)

利用datagridview中的RowPrePaint事件。 
private   void  dataGridView1_RowPrePaint( object  sender, DataGridViewRowPrePaintEventArgs e) 

if (e.RowIndex >= dataGridView1.Rows.Count - 1
return
DataGridViewRow dgr 
= dataGridView1.Rows[e.RowIndex]; 
try 

//dgr.Cells[0]是当前性别列的索引值,用以确定判断哪一列的值 
if (dgr.Cells[0].Value.ToString() == ""
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以通过设置 DataGridView 的 CellMerge 属性来实现合并单元格。具体步骤如下: 1. 设置需要合并的单元格的 CellMerge 属性为 DataGridViewTriState.True,例如需要合并第一列的单元格,可以使用以下代码: ``` dataGridView1.Rows[0].Cells[0].Style.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.Rows[0].Cells[0].Value = "A"; for (int i = 1; i < dataGridView1.Rows.Count; i++) { dataGridView1.Rows[i].Cells[0].Value = "B"; dataGridView1.Rows[i].Cells[0].Style.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.Rows[i].Cells[0].Style.ForeColor = Color.Gray; dataGridView1.Rows[i].Cells[0].Style.BackColor = Color.LightGray; dataGridView1.Rows[i].Cells[0].Style.SelectionBackColor = Color.LightGray; dataGridView1.Rows[i].Cells[0].Style.SelectionForeColor = Color.Gray; dataGridView1.Rows[i].Cells[0].Style.Padding = new Padding(10, 0, 0, 0); dataGridView1.Rows[i].Cells[0].Style.Font = new Font(dataGridView1.Font, FontStyle.Italic); dataGridView1.Rows[i].Cells[0].Style.CellTemplate = new DataGridViewTextBoxCell(); dataGridView1.Rows[i].Cells[0].Style.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.Rows[i].Cells[0].Style.CellTemplate.Style.ForeColor = Color.Gray; dataGridView1.Rows[i].Cells[0].Style.CellTemplate.Style.BackColor = Color.LightGray; dataGridView1.Rows[i].Cells[0].Style.CellTemplate.Style.SelectionBackColor = Color.LightGray; dataGridView1.Rows[i].Cells[0].Style.CellTemplate.Style.SelectionForeColor = Color.Gray; dataGridView1.Rows[i].Cells[0].Style.CellTemplate.Style.Padding = new Padding(10, 0, 0, 0); dataGridView1.Rows[i].Cells[0].Style.CellTemplate.Style.Font = new Font(dataGridView1.Font, FontStyle.Italic); dataGridView1.Rows[i].Cells[0].Style.CellTemplate.Style.WrapMode = DataGridViewTriState.True; } dataGridView1.Rows[0].Cells[0].Style.BackColor = Color.LightGray; dataGridView1.Rows[0].Cells[0].Style.ForeColor = Color.Gray; dataGridView1.Rows[0].Cells[0].Style.SelectionBackColor = Color.LightGray; dataGridView1.Rows[0].Cells[0].Style.SelectionForeColor = Color.Gray; dataGridView1.Rows[0].Cells[0].Style.WrapMode = DataGridViewTriState.True; ``` 2. 设置需要合并的单元格的 RowSpan 或 ColumnSpan 属性,例如需要合并第一行的单元格,可以使用以下代码: ``` dataGridView1.Rows[0].Cells[1].Value = "C"; dataGridView1.Rows[0].Cells[1].Style.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.Rows[0].Cells[1].Style.ForeColor = Color.Gray; dataGridView1.Rows[0].Cells[1].Style.BackColor = Color.LightGray; dataGridView1.Rows[0].Cells[1].Style.SelectionBackColor = Color.LightGray; dataGridView1.Rows[0].Cells[1].Style.SelectionForeColor = Color.Gray; dataGridView1.Rows[0].Cells[1].Style.Padding = new Padding(0, 10, 0, 0); dataGridView1.Rows[0].Cells[1].Style.Font = new Font(dataGridView1.Font, FontStyle.Italic); dataGridView1.Rows[0].Cells[1].Style.CellTemplate = new DataGridViewTextBoxCell(); dataGridView1.Rows[0].Cells[1].Style.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.Rows[0].Cells[1].Style.CellTemplate.Style.ForeColor = Color.Gray; dataGridView1.Rows[0].Cells[1].Style.CellTemplate.Style.BackColor = Color.LightGray; dataGridView1.Rows[0].Cells[1].Style.CellTemplate.Style.SelectionBackColor = Color.LightGray; dataGridView1.Rows[0].Cells[1].Style.CellTemplate.Style.SelectionForeColor = Color.Gray; dataGridView1.Rows[0].Cells[1].Style.CellTemplate.Style.Padding = new Padding(0, 10, 0, 0); dataGridView1.Rows[0].Cells[1].Style.CellTemplate.Style.Font = new Font(dataGridView1.Font, FontStyle.Italic); dataGridView1.Rows[0].Cells[1].Style.CellTemplate.Style.WrapMode = DataGridViewTriState.True; dataGridView1.Rows[0].Cells[1].Style.WrapMode = DataGridViewTriState.True; dataGridView1.Rows[0].Cells[1].RowSpan = 2; ``` 注意事项: 1. 合并单元格时需要注意单元格的值、样式等,需要设置合适的值和样式,以便合并后的单元格看起来更加美观。 2. 合并单元格后,不能对合并后的单元格进行编辑,否则可能会出现异常。 3. 在 CellMerge 属性中使用 DataGridViewTriState.True 表示该单元格可以合并,使用 DataGridViewTriState.False 表示该单元格不能合并,使用 DataGridViewTriState.NotSet 表示该单元格是否可以合并由 DataGridView 控件自行决定。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值