繁体   English   中英

根据刚在单元格中输入的值,从数据库的C#datagridview中的当前行填充数据

[英]Fill data in current row in a C# datagridview from a database according to the value just entered in a cell

我的datagridview有5个标头

Item_code, Item_desc,Item_qty,Item_rate,Item_total.

现在,该用户应仅输入Item_descItem qty Cell Item_desc将自动完成用户输入的字符串。 因此,在用户输入item_desc我希望应用程序使用相应的详细信息填充其余单元格。 然后用户将输入数量,应用程序应在相应的单元格中计算并显示Item total 但是我无法从item_desc列的autocomplete属性进一步item_desc 请帮忙。 提前致谢。 在这里,我附上了我的应用程序的屏幕截图。

屏幕截图

假设您使用的是SQL数据库,它应该不会那么难:

  private void datagridview1_KeyDown()
    if (e.KeyCode == Keys.Enter)
      {
       int rowindex = dataGridView1.CurrentCell.RowIndex; //Here we get the selected row's index of the dataGridView
       int columnindex = dataGridView1.CurrentCell.ColumnIndex; ////Here we get the selected column's index of the dataGridView
       SqlCommand cmd = new SqlCommand("Select * from tablename WHERE desc=@desc",connection);
       cmd.Parameters.Add("@desc",SqlDbType.VarChar).Value = dataGridView1.Rows[rowindex].Cells[columnindex].Value.ToString();
       SqlDataReader dr = cmd.ExecuteReader();
       While (dr.Read())
        {
          dataGridView1.Rows[rowindex].Cells[1].Value = dr[1].ToString
         ///Keep continuing for each column
        }
       dr.Close();
       }

最后,我发现我可以在同一个事件处理函数中同时完成这两个任务。

//在完成项目描述输入时在每一行中填充项目详细信息//在完成数量输入时在每一行中显示项目总计

<

private void dtgrdvw_items_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {


            if (e.ColumnIndex == 1)
            {
                int rowindex = dtgrdvw_items.CurrentCell.RowIndex;
                int columnindex = dtgrdvw_items.CurrentCell.ColumnIndex;
                SqlCommand cmd = new SqlCommand("Select * from item WHERE item_desc=@inv_item_desc", con);
                cmd.Parameters.Add("@inv_item_desc", SqlDbType.VarChar).Value = dtgrdvw_items.Rows[rowindex].Cells[columnindex].Value.ToString();
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    dtgrdvw_items.Rows[rowindex].Cells[0].Value = dr[0].ToString();
                    dtgrdvw_items.Rows[rowindex].Cells[1].Value = dr[1].ToString();
                    dtgrdvw_items.Rows[rowindex].Cells[3].Value = dr[2].ToString();
                }
                dr.Close();
            }
         if (e.ColumnIndex == 2)
            {

                int cell1 = Convert.ToInt32(dtgrdvw_items.CurrentRow.Cells[2].Value);

                Decimal cell2 = Convert.ToDecimal(dtgrdvw_items.CurrentRow.Cells[3].Value);

                if (cell1.ToString() != "" && cell2.ToString() != "")
                {

                    dtgrdvw_items.CurrentRow.Cells[4].Value = cell1 * cell2;

                }
            }
        }

>

在CellEndEdit中,我检查了当前列的索引,并在输入项目描述(第2列)后从项目表中检索了项目详细信息。 然后在输入数量(第3列)后,它将使用比率(第4列)进行计算,并在总计列(第5列)中立即显示总计。感谢您的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM