所以,我一直在做一些练习代码几天。我正在使用DataGridView,没有数据库。一切似乎都有效,除了一个问题。每当我点击删除或更新按钮而没有选择一条记录时,表单就会崩溃。这里的更新功能:DataGridView删除/更新问题
private void btnUpdate_Click(object sender, EventArgs e)
{
if (dgvProfiles.SelectedCells == null)
{
MessageBox.Show("No record was selected to update.");
}
else {
for (int row = 0; row < dgvProfiles.Rows.Count; row++)
{
for (int col = 0; col < dgvProfiles.Columns.Count; col++)
{
if (dgvProfiles.Rows[row].Cells[col].Value != null &&
dgvProfiles.Rows[row].Cells[col].Value.Equals(txtEmail.Text.Trim()))
{
MessageBox.Show("Duplicate email was entered.");
return;
}
}
}
DataGridViewRow newDataRow = dgvProfiles.Rows[indexRow];
newDataRow.Cells[0].Value = txtFirstName.Text;
newDataRow.Cells[1].Value = txtLastName.Text;
newDataRow.Cells[2].Value = txtPhone.Text;
newDataRow.Cells[3].Value = txtEmail.Text;
newDataRow.Cells[4].Value = txtCity.Text;
newDataRow.Cells[5].Value = cbxState.Text;
newDataRow.Cells[6].Value = txtZip.Text;
}
}
在此先感谢!
你想达到什么目的?你想检查是否有任何行选择或什么? –
我正在做这样的事情,但有一些额外的功能,文本框等。单击按钮时提供反馈不是很好,但没有任何反应? http://4.bp.blogspot.com/-QQdixXA53Ss/UyiZTH_FSqI/AAAAAAAAAho/7y8kxTeDwDg/s1600/DataGridView+in+c%23+form+2.PNG –