私は数日間、いくつかの練習問題を行ってきました。私はデータベースなしでDataGridViewを使用して作業しています。 1つの問題を除けば、すべてがうまくいくようです。レコードを選択せずに削除ボタンまたは更新ボタンをクリックすると、フォームがクラッシュします。更新機能は次のとおりです。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/AAAAAAAAA//7y8kxTeDwDg/s1600/DataGridView+in+c%23+form+2.PNG –