private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
DataGridViewRow row = dataGridView1.CurrentRow;//获取选中的行数
try
{
if (e.RowIndex > -1)//是否有数据存在
{
ID.Text = row.Cells[0].Value.ToString();
name.Text = row.Cells[1].Value.ToString();
name_py.Text = row.Cells[2].Value.ToString();
JG.Text = row.Cells[3].Value.ToString();
jgpy.Text = row.Cells[4].Value.ToString();
}
}
catch
{
MessageBox.Show("无数据");
}
}为什么在判断的时候为-1而不为0 在dataGridView无数据时为0 。
本文介绍了一个关于DataGridView的数据绑定及读取问题,探讨了如何通过C#代码正确地从DataGridView中读取数据,并解释了为何在判断行索引时使用-1而非0。
463

被折叠的 条评论
为什么被折叠?



