fill datagridview using datareader

Jun 12 2011 7:57 AM




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)