开发者

Grid binding in a for loop

I am developing a website in which I need to bi开发者_如何学Pythonnd a grid several times {inside a for loop} and as expected the grid overwrites the previous values and preview s the latest iteration result

You may need the code for help.Here it is:-

for (Int32 i = 0; i < k.Length; i++)
    {
        business.clsplugins obj = new business.clsplugins();
        List<business.clspluginsprp> objprp = new List<business.clspluginsprp>();
        Int32 z = Convert.ToInt32(k.GetValue(i));
        objprp = obj.fnd_plugins(z);
        GridView2.DataSource = objprp;
        GridView2.DataBind();
    }


You need to move your List declaration and GridView assignments to outside the for loop - right now you are creating a new List every iteration, you only want to create one, and then bind that to the Grid.. eg:

List<business.clspluginsprp> objprp = new List<business.clspluginsprp>();
business.clsplugins obj = new business.clsplugins();

for (Int32 i = 0; i < k.Length; i++)
{

    Int32 z = Convert.ToInt32(k.GetValue(i));
    objprp.Add(obj.fnd_plugins(z));

}

GridView2.DataSource = objprp;
GridView2.DataBind();


Cou cannot bind twice. Then you call DataBind(). The Control rebuild it contents based on the current data source. Old contents are discarded. So you should use a List containg ALL your data, assign it to DataSource and then call DataBind()


Try this one.

This approach may be helpful for you.Get the concept from the code and implement in your way.

ASPX:

<asp:PlaceHolder ID="plcSample" runat="server">
</asp:PlaceHolder>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    DataSet dsSample = GetDataSet();
    GridView gvSample;
    if (dsSample.Tables.Count > 0 && dsSample.Tables[0].Rows.Count > 0)
    {
        for (int iCount = 0; iCount < dsSample.Tables.Count; iCount++)
        {
            gvSample = new GridView();
            gvSample.DataSource = dsSample.Tables[iCount];
            gvSample.DataBind();
            plcSample.Controls.Add(gvSample);
        }
    }

}

private DataSet GetDataSet()
{
    DataSet ds = new DataSet();
    DataTable dt;

    dt = new DataTable();
    dt.Columns.Add(new DataColumn("ID", typeof(string)));
    dt.Columns.Add(new DataColumn("Code", typeof(string)));
    DataRow dr;
    dr = dt.NewRow();
    dr["ID"] = 1;
    dr["Code"] = "KK";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr["ID"] = 2;
    dr["Code"] = "Karan";
    dt.Rows.Add(dr);
    ds.Tables.Add(dt);

    dt = new DataTable();
    dt.Columns.Add(new DataColumn("ID", typeof(string)));
    dt.Columns.Add(new DataColumn("Code", typeof(string)));
    dr = dt.NewRow();
    dr["ID"] = 1;
    dr["Code"] = "AA";
    dt.Rows.Add(dr);
    dr = dt.NewRow();
    dr["ID"] = 2;
    dr["Code"] = "Arun";
    dt.Rows.Add(dr);
    ds.Tables.Add(dt);

    return ds;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜