如何使用datagridview减去Access数据库中的数据

时间:2016-06-26 07:25:24

标签: c# datagridview ms-access-2007

我有一个未连接到数据库的datagridview,只有一列连接到数据库,该数据库是值检索的。如果用户选择该值并获得插入数据的确认,则表格列未显示为可更新的datagridview。

假设用户选择了一种XYZ药物,并且在用户选择12种数量到特定药物然后在后端(使用MS Access数据库)之后,他的可用性为22,22-12 = 10可用性可以针对该特定选择药物进行更新我正在使用代码,但我得到一个例外:

  

没有给出一个或多个必需参数的值

这里我粘贴我的代码和图片也请帮助我

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
    string connectionString = null;
    connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
    con.ConnectionString = connectionString;

    string medicinename = dataGridView1.Rows[e.RowIndex].Cells["Medicine_Name"].Value.ToString();

    DialogResult dialogResult = MessageBox.Show("Are you sure you want to insert data", "Data insert Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

    if (dialogResult == DialogResult.Yes)
    {
        cmd = new OleDbCommand("update Medicine_Available_Detail set [total_available]=[@total_available]-1 where [Medicine_Name]=@Medicine_Name", con);
        //cmd.Parameters.AddWithValue("@total_available", medicineavailable);
        cmd.Parameters.AddWithValue("@Medicine_Name", medicinename);

        con.Open();
        int n = cmd.ExecuteNonQuery();
        con.Close();

        MessageBox.Show("Record Updated Successfully");
        userlist();

        dataGridView1.Refresh();
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

是的,因为下面这行。您没有传递参数@total_available。取消注释以下注释行,它应该工作。此外,无需在参数名称[]

周围使用[@total_available]
//cmd.Parameters.AddWithValue("@total_available", medicineavailable);

您的查询需要两个参数@total_available@Medicine_Name

update Medicine_Available_Detail set [total_available]=[@total_available]-1 
where [Medicine_Name]=@Medicine_Name

答案 1 :(得分:0)

假设您有一个字段(或文本框的某个位置),您可以在其中键入所售药品的数量。此值是应从表格中的字段private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e) { string connectionString = null; connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString; con.ConnectionString = connectionString; string medicinename = dataGridView1.Rows[e.RowIndex].Cells["Medicine_Name"].Value.ToString(); // This is the hypothetical field where the user types the quantity of medicine sold. // It could be a cell of your datagridview or just a textbox somewhere in // your form. Here I suppose a cell of the grid. int Medicine_SoldQuantity = Convert.ToInt32(dataGridView1.Rows[e.RowIndex] .Cells["Medicine_Sold_Qty"].Value); DialogResult dialogResult = MessageBox.Show("Are you sure you want to insert data", "Data insert Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dialogResult == DialogResult.Yes) { // Here the field total_available is used in the expression // to decrement itself of the value extracted from Medicine_SoldQuantity cmd = new OleDbCommand(@"update Medicine_Available_Detail set [total_available]=[total_available]-@medicineSoldQty where [Medicine_Name]=@Medicine_Name", con); cmd.Parameters.AddWithValue("@medicineSoldQty", Medicine_SoldQuantity); cmd.Parameters.AddWithValue("@Medicine_Name", medicinename); con.Open(); int n = cmd.ExecuteNonQuery(); if(n > 0) MessageBox.Show("Record Updated Successfully"); con.Close(); userlist(); dataGridView1.Refresh(); } } 中减去的值。

因此,您首先从网格行获取该值,然后准备一个查询,其中从Total_Available字段中减去参数。
此时您的表格已正确更新

BTNode<ElemType>* BinaryTree<ElemType>::_Copy( BTNode<ElemType>* T){ 
    if (T == NULL){
        return NULL;
    }
    BTNode<ElemType> *p;
    p = new BTNode<ElemType>;
    p->data = T->data;
    p->lchild = _Copy(T->lchild);
    p->rchild = _Copy(T->rchild);
    return p; 
}