My datagridview
has 5 header which are
Item_code, Item_desc,Item_qty,Item_rate,Item_total.
Now from this user should enter only Item_desc
& Item qty
. The Cell Item_desc
will autocomplete the string user enter. So once after user enter the item_desc
I want the application to fill out rest of cells with corresponding details. And then user will enter the quantity and application should calculate and show Item total
in respective cell. But I couldn't go further from autocomplete
property for the item_desc
column. Please help. Thanks in advance. Here I am attaching the screen shot of my application.
Assuming you are using SQL database,it should not be that hard :
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();
}
At last I found I can do both in same event handler function.
//to fill item details in each row when item description entry is done //to show item total in each row when quantity entry is done
<
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;
}
}
}
>
Here in CellEndEdit I checked the index of current column and retrieve item details from item table once I enter item description(column 2). Then after I enter quantity (column 3) it will calculate with rate (column 4) and show total instantaneously in total column (column 5)..Thanks for the helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.