【发布时间】:2016-06-13 09:09:20
【问题描述】:
|Column 1|Column 2|
|1 |1 |
|2 |2 |
|3 |3 |
|4 |4 |
|5 |5 |
|6 |6 |
|7 |7 |
|8 |8 |
|9 |9 |
| | |
假设我在我的 datagridview 中显示这个,现在我正在添加新行(第 10 行)。我正在检查用户是否在稍后使用该数据之前将任何列留空,现在如果用户在第 1 列中插入了某些内容,并且将第 2 列留空,我会检查它并向他显示消息“第 2 列不能为空”,但之后我希望他的选择(当前行)位于那个空单元格,我的问题是新行没有索引(它有,但是当我使用它时,我得到错误,我不能使用大于 max 的索引行)。我该如何解决。
这是我正在检查的代码部分
int nRows = dataGridView1.Rows.Count;
int currColumn = dataGridView1.CurrentCell.ColumnIndex;
int currRow = dataGridView1.CurrentCell.RowIndex;
string id = dataGridView1.Rows[currRow].Cells[0].Value.ToString();
string opis = dataGridView1.Rows[currRow].Cells[1].Value.ToString();
if (string.IsNullOrEmpty(id))
{
MessageBox.Show("Polje broj ne moze biti prazno!");
dataGridView1.CurrentCell = dataGridView1[0, nRows];
}
else if (string.IsNullOrEmpty(opis))
{
MessageBox.Show("Polje opis ne moze biti prazno!");
dataGridView1.CurrentCell = dataGridView1[1, nRows];
}
【问题讨论】:
-
如果您发布您所描述的代码将会很有帮助。这样可以更轻松地为您提供帮助
-
我在帖子中添加了代码
标签: c# datagridview