Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I read the data from data table but i dont know to add in gridview if u know kindly help me

My Code Is

C#
 string qry = "select distinct category,invoice,tot,model,price,quantity,tax,total,grandtoal,discount,discription,taxamount from Client_Bill_TB where invoice='48/072014'";
                dt = cbbal.GetData(qry); //cbbbal is method to exec data table
foreach (DataRow Drow in dt.Rows)
               {
                   txtcategory.Text = Drow.ItemArray[0].ToString();
                   tot.Text = Drow.ItemArray[7].ToString();
                   txtdescrip.Text = Drow.ItemArray[10].ToString();
                   txtPrice.Text = Drow.ItemArray[4].ToString();
                   txtQuantity.Text = Drow.ItemArray[5].ToString();
                   txtdiscount.Text = Drow.ItemArray[9].ToString();
                   txtInvoice.Text = Drow.ItemArray[1].ToString();
                   txttaxamt.Text = Drow.ItemArray[11].ToString();
                   txtTotal.Text = Drow.ItemArray[7].ToString();
                   lblGrandTotal.Text = Drow.ItemArray[8].ToString();
                   dt.Rows.Add(Drow); //Error
               }


if i have two rows when exec that query but this code display last row only where its in gridview

How Can solve this problem
Posted
Updated 14-Jul-14 22:49pm
v3
Comments
Anand Gunasekaran 15-Jul-14 4:53am    
Datagridview1.DataSource=dt;

1 solution

There is some mistakes.

DataTable dt = new DataTable();

DataTable dt1 = cbbal.GetData(qry);

foreach(DataRow dr in dt1.Rows)
{
DataRow dr1 = dt.NewRow();
dr1["Name"] = dr["Name"].ToString();
dt.Rows.Add(dr1);
}

If you have any confusion then you can ask me.
 
Share this answer
 
Comments
prasanna.raj 15-Jul-14 5:09am    
Thanks for replay

here i have error "Column 'Name' does not belong to table ."
Sarvesh Kumar Gupta 15-Jul-14 5:14am    
Please write your column name here, because I have given only example. If you have some more question you can ask me.
prasanna.raj 15-Jul-14 5:17am    
i just give like this

Drow2["category"] = Drow["category"].ToString();
prasanna.raj 15-Jul-14 5:21am    
my Qry is

string qry = "select distinct category,invoice,tot,model,price,quantity,tax,total,grandtoal,discount,discription,taxamount from Client_Bill_TB where invoice='48/072014'";
Sarvesh Kumar Gupta 15-Jul-14 5:27am    
then you write
dr1["category"] = dr["category"].ToString();
dr1["invoice"] = dr["invoice"].ToString();
and so on for each field

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