如何在动态网格视图中绑定行 [英] How To bind Rows in Dynamically gridview

查看:69
本文介绍了如何在动态网格视图中绑定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里我想绑定数据表中可用的行想要在gridview中动态绑定,但是我的查询用可用的行绑定相同的数据,



cs代码:



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:

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...

推荐答案

你是否意识到你总是在阅读第0行意味着第一个Row值总是。

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

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

    // .......
}



而不是这样做,你应该使用变量i,它会逐渐增加。



所以,它应该是...


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

So, it should be...

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

    // .......
}



再次,我不确定,如果你只有第三个单元格内的所有控件,因为你正在读取 Cells [2]中的所有值代码中的。如果这不正确,那么纠正。


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.


这篇关于如何在动态网格视图中绑定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆