Click here to Skip to main content
16,001,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
Here i want to bind however rows available in datatable want to bind dynamically in gridview, But my query bind same data with available rows,

cs code:

C#
protected void BindGridDetails()
    {
        try
        {
            int rowIndex = 0;
            for (int i = 0; i < gridview1.Rows.Count; i++)
            {
                TextBox txtcategory = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txtcategory");
                DropDownList ddlmodel = (DropDownList)gridview1.Rows[rowIndex].Cells[2].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,invoice,tot,model,price,quantity,tax,total,grandtoal,discount,discription,taxamount from Client_Bill_TB where id='" + ViewState["VSID"] + "'";
                string qry = "select distinct category,invoice,tot,model,price,quantity,tax,total,grandtoal,discount,discription,taxamount from Client_Bill_TB where invoice='45/062014'";
                List<Class1> listuser = cbbal.SqlReader(qry);
                if (listuser.ToList().Count > 0)
                {
                    txtcategory.Text = listuser[0].stecategory.ToString();
                    //ddlmodel.DataSource = cbbal.DSet("select distinct model from  Client_Bill_TB where id='" + ViewState["VSID"] + "'");
                    ddlmodel.DataSource = cbbal.DSet("select distinct model from  Client_Bill_TB where invoice='45/062014'");
                    ddlmodel.DataTextField = "model";
                    ddlmodel.DataBind();
                    tot.Text = listuser[0].strtot.ToString();
                    txtdescrip.Text = listuser[0].strdescription.ToString();
                    txtPrice.Text = listuser[0].strprice.ToString();
                    txtQuantity.Text = listuser[0].strquantity.ToString();
                    txtdiscount.Text = listuser[0].strdiscount.ToString();
                    //ddTax.DataSource = cbbal.DSet("select tax,id from  Client_Bill_TB where id='" + ViewState["VSID"] + "'");
                    ddTax.DataSource = cbbal.DSet("select tax,id from  Client_Bill_TB where invoice='45/062014'");
                    ddTax.DataTextField = "tax";
                    ddTax.DataBind();
                    txtInvoice.Text = listuser[0].strInvoice.ToString();
                    txttaxamt.Text = listuser[0].strtaxamount.ToString();
                    txtTotal.Text = listuser[0].strtotal.ToString();
                    lblGrandTotal.Text = listuser[0].strtotalamount.ToString();
                }
                rowIndex++;
            }
        }
        catch (Exception ex)
        {
            string script = "<script type=\"text/javascript\"> window.alert('" + ex.Message + "'); </script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "alertscript", script); 
        }
    }


Help Me...
Posted
Updated 29-Jun-14 23:04pm
v3

1 solution

Do you realize that you are always reading the Row 0 means the first Row values always.
C#
int rowIndex = 0;

for (int i = 0; i < gridview1.Rows.Count; i++)
{
    TextBox txtcategory = (TextBox)gridview1.Rows[rowIndex].Cells[2].FindControl("txtcategory");

    // .......
}

Instead of doing this, you should use the variable "i", which is getting incremented.

So, it should be...
C#
for (int i = 0; i < gridview1.Rows.Count; i++)
{
    TextBox txtcategory = (TextBox)gridview1.Rows[i].Cells[2].FindControl("txtcategory");

    // .......
}

Again, I am not sure, if you have all the controls inside the third cell only, because you are reading all values from Cells[2] in the code. If that is not correct, then rectify.
 
Share this answer
 

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