dataGridView中的索引超出范围
我写了一个程序,该程序获取学生的名称,家庭和年龄,将它们保存在数据库中,并在datagridview
中显示它们。在datagridview
中添加它后,我可以看到添加的学生,但我无法单击它。错误是索引超出范围(索引-1没有值)
private void btnRegister_Click(object sender, EventArgs e)
{
try
{
string name = txtName.Text;
string family = txtFamily.Text;
int age = int.Parse(txtAge.Text);
foreach (var item in Database.tbl_student)
{
if (item.Name.Trim().ToLower().Equals(name.Trim().ToLower())
&& item.Family.Trim().ToLower().Equals(family.Trim().ToLower()))
{
MessageBox.Show("this student is duplicate..");
return;
}
}
Database.tbl_student.Add(new Student() { Name = name, Family = family, Age = age });
dataGridView1.DataSource = null;
dataGridView1.DataSource = Database.tbl_student;
} catch(Exception error)
{
MessageBox.Show(error.Message);
}
}
I wrote a program that gets the name, family and age of student, saves them in the database and shows them in DataGridView
. I can see added student after adding it in DatagridView
but I can't click it. Error is index out of range (index -1 does not have value)
private void btnRegister_Click(object sender, EventArgs e)
{
try
{
string name = txtName.Text;
string family = txtFamily.Text;
int age = int.Parse(txtAge.Text);
foreach (var item in Database.tbl_student)
{
if (item.Name.Trim().ToLower().Equals(name.Trim().ToLower())
&& item.Family.Trim().ToLower().Equals(family.Trim().ToLower()))
{
MessageBox.Show("this student is duplicate..");
return;
}
}
Database.tbl_student.Add(new Student() { Name = name, Family = family, Age = age });
dataGridView1.DataSource = null;
dataGridView1.DataSource = Database.tbl_student;
} catch(Exception error)
{
MessageBox.Show(error.Message);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论