Reputation: 454
In my project, there is a datagridview if there are no data and click on the UPDATE button, I should get error message. Here if I click directly on update button I am getting error message, but if I click on the datagridview ( even though there is no data in the datagridview ) and click on update, I am getting message as updated. Please tell what is code that I should use instead of (dataGridView2.SelectedCells.Count == 0). The code I am using is:
private void btnUpdate_Click(object sender, EventArgs e)
{
if (dataGridView2.SelectedCells.Count == 0)
{
MessageBox.Show("There are no any records to update");
}
else
{
SqlConnection con = Helper.getconnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandType = CommandType.Text;
string PrjName = txtPrjNmae.Text;
string Description = txtPrjdescription.Text;
DateTime Date = dateUpdate.Value;
dateUpdate.Format = DateTimePickerFormat.Custom;
dateUpdate.CustomFormat = "dd/MM/yy";
string Size = txtPrjSize.Text;
string Manager = txtPrjManager.Text;
cmd.CommandText = "Update Projects set Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectName= '" + PrjName + "' ";
MessageBox.Show("Project Details are updated");
dataGridView2.Update();
dataGridView2.Refresh();
cmd.ExecuteNonQuery();
con.Close();
}
BindData3();
}
Upvotes: 1
Views: 88
Reputation: 7619
I think that the problem here is that you set the AllowUserToAddRows
property of your DataGridView
to true
. In this way there always be an empty row in the bottom of your DataGridView
so RowCount
will return almost 1 (the empty row used to create a new entry).
There are almost 2 possibilities to get rid of the problem:
AllowUserToAddRows
to false if you don't want to allow the user to add new rows.
private void btnUpdate_Click(object sender, EventArgs e)
{
if (dataGridView2.SelectedCells.Count == 0 || dataGridView2.RowCount <= 1)
{
MessageBox.Show("There are no any records to update");
}
else
{
SqlConnection con = Helper.getconnection();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandType = CommandType.Text;
string PrjName = txtPrjNmae.Text;
string Description = txtPrjdescription.Text;
DateTime Date = dateUpdate.Value;
dateUpdate.Format = DateTimePickerFormat.Custom;
dateUpdate.CustomFormat = "dd/MM/yy";
string Size = txtPrjSize.Text;
string Manager = txtPrjManager.Text;
cmd.CommandText = "Update Projects set Description='" + Description + "', DateStarted='" + Date + "',TeamSize='" + Size + "',Manager='" + Manager + "' where ProjectName= '" + PrjName + "' ";
MessageBox.Show("Project Details are updated");
dataGridView2.Update();
dataGridView2.Refresh();
cmd.ExecuteNonQuery();
con.Close();
}
BindData3();
}
Hope this helps.
Upvotes: 1
Reputation: 73
Try to use the following code and see if it works:
dataGridView2.RowCount == 0
Upvotes: 1
Reputation: 63317
Try this:
if (dataGridView2.RowCount == 0){
MessageBox.Show("There are no any records to update");
}
Upvotes: 1