簡體   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