1

在我的项目中,如果没有数据,则有一个 datagridview,然后单击 UPDATE 按钮,我应该会收到错误消息。在这里,如果我直接单击更新按钮,我会收到错误消息,但是如果我单击 datagridview(即使 datagridview 中没有数据)并单击更新,我会收到更新的消息。请告诉我应该使用什么代码而不是(dataGridView2.SelectedCells.Count == 0)。我正在使用的代码是:

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();            
    }       
4

3 回答 3

1

尝试这个:

if (dataGridView2.RowCount == 0){
        MessageBox.Show("There are no any records to update");
}
于 2013-09-24T09:46:01.933 回答
1

我认为这里的问题是您将您的AllowUserToAddRows属性设置DataGridViewtrue. 这样,您的底部总是有一个空行,DataGridView因此RowCount将返回几乎 1(用于创建新条目的空行)。

几乎有两种可能摆脱这个问题:

  1. AllowUserToAddRows如果您不想允许用户添加新行,请将其设置为 false
  2. 用这种方式改变问题:

.

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();            
    }   

希望这可以帮助。

于 2013-10-09T07:36:08.190 回答
1

尝试使用以下代码,看看它是否有效:

dataGridView2.RowCount == 0
于 2013-09-25T05:34:19.623 回答