Search code examples
c#visual-studiovisual-studio-2015datagridviewsql-server-2014

Add Data in DataGridView According to the user's input


I am a beginner in c#. I made a DataGridView in Visual Studio. I have Item Code , Item Name , Cost Price , Sales Price , Quantity and Total columns in my DataGridView. What I want is that when the user input the code in the Item Code I want to search that code in the Database and display the associated Item Name , Cost Price and Sales Price automatically into the respective columns . Searching and fetching part I will do but how to know when the user has completed his input and then according to the input how to display the data accordingly. I don't have any idea how to Achieve that so I have not posted any code. if you think that you need the code to help, please specify which part of the code you want . thanks for the help in advance.


Solution

  • Please try this:

    private void dtgProductDetail_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //For example:-
            //if(e.ColumnIndex == 0)
            if (e.ColumnIndex == "Put column index of Item Code.")
            {
                //Your function to bind values.
                //Searching of values in respect with Item Code from database.
                string ItemName="Value from db".
                string CostPrice="Value from db".
                string SalesPrice="Value from db".
    
                //Now put values in cells.
                //dtgProductDetail.Rows[e.RowIndex].Cells["Column Name"].Value = "Value to be inserted.";
                dtgProductDetail.Rows[e.RowIndex].Cells["Item Name"].Value = ItemName;
                dtgProductDetail.Rows[e.RowIndex].Cells["Cost Price"].Value = CostPrice;
                dtgProductDetail.Rows[e.RowIndex].Cells["Sales Price"].Value = SalesPrice;
            }
        }
    

    In case you have any query feel free to ask.