Click here to Skip to main content
14,735,324 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

 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 23: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.
   
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
prasanna.raj 15-Jul-14 5:42am
   
if ma try this Qry

string qry = "select distinct category from Client_Bill_TB where invoice='48/072014'";

i got error
how can i solve this prob...
Sarvesh Kumar Gupta 15-Jul-14 5:45am
   
What error, you are getting
prasanna.raj 15-Jul-14 5:49am
   
Column 'category' does not belong to table
prasanna.raj 15-Jul-14 5:52am
   
My Full Code

int rowIndex = 0;
for (int i = 1; i < gridview1.Rows.Count; i++)
{
TextBox txtcategory = (TextBox)gridview1.Rows[rowIndex].Cells[0].FindControl("txtcategory");
DropDownList ddlmodel = (DropDownList)gridview1.Rows[rowIndex].Cells[1].FindControl("ddlmodel");
TextBox txtdescrip = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txtdescrip");//TextBox txtdescrip = gridview1.FindControl("txtdescrip") as TextBox;
TextBox txtPrice = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txtPrice");//TextBox txtPrice = gridview1.FindControl("txtPrice") as TextBox;
TextBox txtQuantity = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txtQuantity");//TextBox txtQuantity = gridview1.FindControl("txtQuantity") as TextBox;
TextBox txtdiscount = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txtdiscount");//TextBox txtdiscount = gridview1.FindControl("txtdiscount") as TextBox;
DropDownList ddTax = (DropDownList)gridview1.Rows[rowIndex].Cells[2].FindControl("ddTax");
TextBox txttaxamt = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txttaxamt");//TextBox txttaxamt = gridview1.FindControl("txttaxamt") as TextBox;
TextBox txtTotal = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txtTotal");//TextBox txtTotal = gridview1.FindControl("txtTotal") as TextBox;
TextBox tot = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txttotamt");
Label lblGrandTotal = (Label)panelGrd.FindControl("lblTotal");

string qry = "select distinct category from Client_Bill_TB where invoice='48/072014'";
dt = cbbal.GetData(qry);
DataTable Dtable = new DataTable();
foreach (DataRow Drow in dt.Rows)
{
DataRow Drow2 = Dtable.NewRow();
Drow2["category"] = Drow["category"].ToString();

//txtcategory.Text = Drow.ItemArray[0].ToString();
Dtable.Rows.Add(Drow2);
}
rowIndex += 1;
}
Sarvesh Kumar Gupta 15-Jul-14 6:00am
   
oh, mistake

DataTable Dtable = new DataTable();
Dtable = dt.Clone(); // Use this synatx
prasanna.raj 15-Jul-14 6:07am
   
How can i load category value into txtcategory.Text
Sarvesh Kumar Gupta 15-Jul-14 6:20am
   
txtcategory.Text = dt.Rows[i]["category"].ToString();
prasanna.raj 15-Jul-14 6:49am
   
string qry = "select distinct category from Client_Bill_TB where invoice='48/072014'";
dt=cbbal.GetData(qry);
DataTable Dtable = new DataTable();
Dtable = dt.Clone();
foreach (DataRow Drow in dt.Rows)
{
DataRow Drow2 = Dtable.NewRow();
Drow2["category"] = Drow["category"].ToString();
txtcategory.Text = Dtable.Rows[1]["category"].ToString();
Dtable.Rows.Add(Drow2);

}
am using this code but still i am getting error 'There is no row at position 1.'
prasanna.raj 15-Jul-14 6:59am
   
If am using this code

string qry = "select distinct category from Client_Bill_TB where invoice='48/072014'";
dt=cbbal.GetData(qry);
DataTable Dtable = new DataTable();
Dtable = dt.Clone();
foreach (DataRow Drow in dt.Rows)
{
DataRow Drow2 = Dtable.NewRow();
Drow2["category"] = Drow["category"].ToString();
txtcategory.Text = dt.Rows[i]["category"].ToString();
Dtable.Rows.Add(Drow2);

}
it display one category value into griview but that qry return two value
Sarvesh Kumar Gupta 15-Jul-14 7:40am
   
what is coming in dt, please debug

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