4
Answers

fill datagridview using datareader

Shyja Abraham

Shyja Abraham

13y
6k
1




hai,

I have a form with datagridview. the column names of datagridview are sno,itemcode,itemdescription,unit,qty,rate,discount,value . I want to insert the database values into datagridview . I use this code for that,but It will not work.no error,but no values fill in to the datagridview, plz help.
 

 for (i = 0; i < lpogridview.Rows.Count - 1; i++)

          {

 

                cmd.CommandText = "select lpoid, itemcode,itemname,unit,lpoqty,lporate,lpodiscount,lpovalue,nar from lpo l inner join item i on l.itemid=i.itemid where lpono='" + textBox2.Text + "'";

            dr = cmd.ExecuteReader();

            while (dr.Read())

            {

 

 

                lpogridview.Rows[i].Cells[0].Value = Convert.ToString(dr[0]);

                lpogridview.Rows[i].Cells[1].Value = Convert.ToString(dr[1]);

                lpogridview.Rows[i].Cells[2].Value = Convert.ToString(dr[2]);

                lpogridview.Rows[i].Cells[3].Value = Convert.ToString(dr[3]);

                lpogridview.Rows[i].Cells[4].Value = Convert.ToString(dr[4]);

                lpogridview.Rows[i].Cells[5].Value = Convert.ToString(dr[5]);

                lpogridview.Rows[i].Cells[6].Value = Convert.ToString(dr[6]);

                lpogridview.Rows[i].Cells[7].Value = Convert.ToString(dr[7]);

            }

                     dr.Close();

               

 

            }

 

Answers (4)
1
Sam Hobbs

Sam Hobbs

NA 28.7k 1.3m 15y

I don't know how you are doing it so I don't know if there is an easier way. My guess is that there is not an easier way.
 
Note that often the "secret" to making a program that is easy to use is complex development. In other words, sometimes we must write complicated program to make one that is easy to use. Windows and .Net and database software such as SQL Server does a lot for us, but sometimes we must do some programming outselves.
 
My guess is that the code necessary for the search page is not difficult except for many details that need to be done.
 
Probably what I would do is to create a List<String> for each filter (selection criteria) then concatenate them all together as needed.
0
Vikas Ahlawat

Vikas Ahlawat

NA 577 661.6k 15y
Ya code is not difficult.
Thanks for your reply.