Click here to Skip to main content
15,906,081 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I have a datagridview in which there are 4 columns
________________________________________________
|Product Name | Old Stock | New Stock | Current Stock|
|A|
|B|
|C|
|D|
|E|_____________________________________________
in first Column there are products A, B,C,D,E. that is connected to database.
I want to load values in New Stock column i.e. 3rd column from table New_Stk on form Load event. The New_Stk table has following columns Date, Product_Name, Qty. Value should be loaded in following way: for eg. in datagridview first column vales are loaded, so the first cell value is A, then while getting value from New_Stk table, it should match the product name i.e A and current date and then show value in cell of 3rd column in datagridview.

I have written following Code:-
C#
string date, M_name, cnstp, query;
            date = DateTime.Today.ToShortDateString();            
            cnstp = ConfigurationManager.ConnectionStrings["Your_ConnectionString"].ConnectionString;
            SqlConnection conr = new SqlConnection(cnstp);
            for (int i = 0; i <= dgvStock.Rows.Count; i++)
            {
                M_name = dgvStock.Rows[i].Cells[0].Value.ToString();
                query = "Select distinct Qty from New_Stk where Product_Name = '" + "M_name" + " and Date = " + "date" + "'";
                SqlDataAdapter adpi = new SqlDataAdapter("query", conr);
                DataSet ds = new DataSet();
                adpi.Fill(ds);
                dgvStock.DataSource = ds.Tables[0].DefaultView;
                dgvStock.Rows[i].Cells[0].Value = query;


Thanks in Advance !!!
Posted

1 solution

You should just write your sql to pull all the values you want at once, and do a data bind. What you're doing here is wildly inefficient. why can't you just get all the values you need in one sql call ?
 
Share this answer
 
Comments
Ank_ush 21-Jul-12 4:31am    
Product Name comes from one table, Old stock from another and New Stock from another table, and current stock is total of Old + New Stock...
Christian Graus 21-Jul-12 4:53am    
Not sure your table design sounds sensible, but, so what ? Join them together and get all the data you need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900