我在Windows窗体数据网格中使用CanUserAddRows属性,以允许用户将行添加到绑定到数据库表的网格中。我想在该空行的第一列显示一个文本,以指示用户必须填充该行以创建新行。当用户单击或进入该行时,文本应该消失。我怎么能这样做?
答案 0 :(得分:0)
找到解决方案。
private void dgvKlanten2_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) {
int rows = ds1.Tables["Klanten"].Rows.Count;
//when loading
if (e.RowIndex == rows - 1) {
dgvKlanten2.Rows[e.RowIndex+1].Cells[2].Value = "add a record here";
dgvKlanten2.Rows[e.RowIndex+1].Cells[2].Style.ForeColor = Color.Gray;
}
//when adding a new row
if (e.RowIndex > rows ) {
dgvKlanten2.Rows[rows].Cells[1].Value = getNieuwKlantNummer();
dgvKlanten2.Rows[e.RowIndex].Cells[2].Value = "add a record here";
dgvKlanten2.Rows[e.RowIndex].Cells[2].Style.ForeColor = Color.Gray;
dgvKlanten2.Rows[e.RowIndex-1].Cells[2].Style.ForeColor = Color.Black;
dgvKlanten2.Rows[e.RowIndex-1].Cells[2].Value = String.Empty;
}
}