简体   繁体   中英

How to auto fill datagridview fields on textbox leav event of the same datagridview

I am doing a windows application sales module where i am adding multiple items into a datagridview, In my datagridview i have itemcode, barcode, itemname(combobox), quantity, rate ect. When i enter the item code i need to auto fill the rest of my coloums with the values according to the itemcode saved in database mydatagrid

If you use entity framework (since you do not specify much of your code) you can do it this way:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
          var query = from p in db.Items_
                    where p.itemCode== this.mydatagrid.CurrentCell.Value.ToString();
                    select p;
        this.mydatagrid.Rows.Clear();
        foreach (var items in query)
        {

            mydatagrid.Rows.Add(
                                         items.barcode.ToString(),
                                         items.itemName.ToString(),
                                         items.quantity.ToString(),
                                         items.rate.ToString()
                                         //etc
                );
        }
        }
        catch
        {
        }

    }

PS: keep in mind that this example is vulnerable to sql injections.

You can use the CellValueChanged event of the data grid view control. Below is a simple example demonstrating the capture of the item code entered, passing this to a business layer which in turn calls a database layer and returns you a populated item for use on the grid.

 private void Form1_Load(object sender, EventArgs e)
 {
       //Bind cell value changed event or set in designer.
       dgGridView.CellValueChanged += dgGridView_CellValueChanged;
 }

    private void dgGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == -1)
            return;

        if (e.ColumnIndex > 0)
            return;

        var myBL = new MyBL();

        switch (e.ColumnIndex)
        {
            case 0: //Item Code Column
                var itemCodeEntered = dgGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                var itemData = myBL.GetItemData(itemCodeEntered);
                dgGridView.Rows[e.RowIndex].Cells[0].Value = itemData.ItemCode;
                dgGridView.Rows[e.RowIndex].Cells[1].Value = itemData.BarCode;
                break;

            case 1: //Bar code column
                // Do other stuff here.
                break;

            //etc...
        }

    }

Ensure you encapsulate you business and data layer actions in a separate class. I return a class called "MyItemData"

    public class MyItemData
    {
        public string ItemCode { get; set; }
        public string BarCode { get; set; }
        //etc...
    }

public class MyBL
{
    public MyItemData GetItemData(string itemCode)
    {
        var myDL = new MyDBLayer();
        var itemDataSet = myDL.ReturnItemData(itemCode);

        var newItem = new MyItemData();
        newItem.ItemCode = itemCode;
        newItem.BarCode = itemDataSet.Tables[0].Rows[0]["BarCode"].ToString();

        //etc...

        return newItem;
    }
}


public class MyDBLayer
{
    public DataSet ReturnItemData(string itemCode)
    {
        DataSet myDataSet;
        //Query database and set dataSet
        return myDataSet;
    }
}

Hope that helps thanks

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.

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