private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { if (infos[e.RowIndex].HaveDetails == "不存在") //此条件会导致无限的刷新 { dgv.Rows[e.RowIndex].Cells[11] = new DataGridViewTextBoxCell(); dgv.Rows[e.RowIndex].Cells[11].Value = "无"; } }
原因: new DataGridViewTextBoxCell() 程序导致 dgv_RowPrePaint事件重绘,所以就进入了无休止的 刷新
正确代码
private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) { if (infos[e.RowIndex].HaveDetails == "不存在"&& dgv[11, e.RowIndex].GetType() == typeof(DataGridViewButtonCell)) { dgv.Rows[e.RowIndex].Cells[11] = new DataGridViewTextBoxCell(); dgv.Rows[e.RowIndex].Cells[11].Value = "无"; } }
附添加按钮程序
DataGridViewButtonColumn btn = new DataGridViewButtonColumn(); btn.Name = "Details"; btn.HeaderText = "查看详情"; btn.DefaultCellStyle.NullValue = ""; btn.Width = 45; dgv.Columns.Add(btn);
按钮点击事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { int index = dataGridView1.SelectedCells[0].RowIndex; var id = filesdt.Rows[index][0].ToString(); if (this.dataGridView1.Columns[e.ColumnIndex].Name == "down") { } else if (this.dataGridView1.Columns[e.ColumnIndex].Name == "del") { } }
标签:11,RowPrePaint,Rows,RowIndex,dgv,刷新,btn,死循环 From: https://www.cnblogs.com/zuochanzi/p/16925557.html