如何在动态gridview中添加按钮?

时间:2014-05-26 10:29:57

标签: asp.net c#-4.0 gridview

public void BuildMeatTally()
{
    try
    {
        DataTable dtdate = new DataTable();
        objRetailPL.status = 4;
        dtdate = objRetailBAL.GetTypeNew(objRetailPL);

        if (dtdate.Rows.Count > 0)
        {
            for (int i = 0; i < dtdate.Rows.Count; i++)
            {
                string branchdate;
                int totbirds; float totwt, meatyield, Avgweight;
                DateTime datewr;

                DataTable dtdatedetailsTable = new DataTable();
                objRetailPL.branchdate = dtdate.Rows[i]["BranchDate"].ToString();
                objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());

                dtdatedetailsTable = objRetailBAL.getbradisdatewisedet(objRetailPL);

                branchdate = dtdatedetailsTable.Rows[0]["BranchDate"].ToString();
                totbirds = Convert.ToInt32(dtdatedetailsTable.Rows[0]["numofbirds"].ToString());
                totwt = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalweght"].ToString());
                meatyield = Convert.ToSingle(dtdatedetailsTable.Rows[0]["totalmeatyeild"].ToString());
                Avgweight = Convert.ToSingle(dtdatedetailsTable.Rows[0]["AvgWeight"].ToString());

                Table tbldynamic = new Table();
                tbldynamic.BorderStyle = BorderStyle.Ridge;
                tbldynamic.Width = 600;

                TableCell tc = new TableCell();
                TableRow tr = new TableRow();

                Label lblbranchdate = new Label();
                lblbranchdate.ID = "lblbrandate" + i.ToString();
                lblbranchdate.Text = "&nbsp&nbsp" + "<B>Branch Date:</B>" + "&nbsp";
                tc.Controls.Add(lblbranchdate);

                Label lblbranchdatedata = new Label();
                lblbranchdatedata.ID = "lbltotnumbird1" + i.ToString();
                lblbranchdatedata.Text = branchdate + "&nbsp&nbsp&nbsp";

                tc.Controls.Add(lblbranchdatedata);
                tr.Cells.Add(tc);
                tbldynamic.Rows.Add(tr);

                Label lbltotwt1 = new Label();
                lbltotwt1.ID = "lbltotwt1" + i.ToString();
                lbltotwt1.Text = "<B>Total Weight:</B>" + "&nbsp";
                tc.Controls.Add(lbltotwt1);

                Label lbltotwt1data = new Label();
                lbltotwt1data.ID = "lbltotwt1data" + i.ToString();
                lbltotwt1data.Text = totwt + "&nbsp&nbsp&nbsp";

                tc.Controls.Add(lbltotwt1data);
                tr.Cells.Add(tc);
                tbldynamic.Rows.Add(tr);

                tc = new TableCell();
                tr = new TableRow();

                Label lbltotbirds = new Label();
                lbltotbirds.ID = "lbltotbird" + i.ToString();
                lbltotbirds.Text = "<B>Total Birds:</B>" + "&nbsp";
                tc.Controls.Add(lbltotbirds);

                Label lbltotbirddata = new Label();
                lbltotbirddata.ID = "lbltotbirdata" + i.ToString();
                lbltotbirddata.Text = totbirds + "&nbsp&nbsp&nbsp";

                tc.Controls.Add(lbltotbirddata);
                tr.Cells.Add(tc);
                tbldynamic.Rows.Add(tr);

                Label lblmeatyield = new Label();
                lblmeatyield.ID = "lblmeatyield" + i.ToString();
                lblmeatyield.Text = "<B>Meat Yield in Kgs:</B>" + "&nbsp";
                tc.Controls.Add(lblmeatyield);

                Label lblmeatyielddata = new Label();
                lblmeatyielddata.ID = "lblmeatyied" + i.ToString();
                lblmeatyielddata.Text = meatyield + "&nbsp&nbsp&nbsp";

                tc.Controls.Add(lblmeatyielddata);
                tr.Cells.Add(tc);
                tbldynamic.Rows.Add(tr);

                Label lblavgweight = new Label();
                lblavgweight.ID = "lblavgweight" + i.ToString();
                lblavgweight.Text = "<B>Avg Weight:</B>" + "&nbsp";
                tc.Controls.Add(lblavgweight);

                Label lblavgweightdata = new Label();
                lblavgweightdata.ID = "lblavgweightdata" + i.ToString();
                lblavgweightdata.Text = Avgweight + "&nbsp&nbsp&nbsp";

                tc.Controls.Add(lblavgweightdata);
                tr.Cells.Add(tc);
                tbldynamic.Rows.Add(tr);

                DataTable dtdcDetails = new DataTable();
                objRetailPL.sno = Convert.ToInt32(dtdate.Rows[i]["sno"].ToString());
                dtdcDetails = objRetailBAL.dtdateTallyTable(objRetailPL);

                GridView gv = new GridView();
                //gv.ID = "gv" + dtdate.Rows[i]["BranchDate"].ToString();
                gv.Width = 600;
                Label lblTotal = new Label();
                Label lbltotaleggs = new Label();

                BoundField partyname = new BoundField();
                partyname.HeaderText = "PartyName";
                partyname.DataField = "partyname";
                gv.Columns.Add(partyname);

                BoundField birdtype = new BoundField();
                birdtype.HeaderText = "Bird Type";
                birdtype.DataField = "birdname";
                gv.Columns.Add(birdtype);

                BoundField totweight = new BoundField();
                totweight.HeaderText = "Total Weight";
                totweight.DataField = "totalweight";
                gv.Columns.Add(totweight);

                BoundField rateperkg = new BoundField();
                rateperkg.HeaderText = "Rate/Kg";
                rateperkg.DataField = "rateperkg";
                gv.Columns.Add(rateperkg);


                BoundField Bdcno = new BoundField();
                Bdcno.HeaderText = "DCNo";
                Bdcno.DataField = "dcno";
                gv.Columns.Add(Bdcno);

     // Here I want to add Button(confirm) In dynamic Gridview..

                gv.AutoGenerateColumns = false;
                gv.ShowFooter = true;
                gv.DataSource = dtdcDetails;
                gv.DataBind();                   
        }
    }     
}

我如何在动态gridview中添加按钮控件,我尝试使用Templeate字段,但我没有得到完全输出...之后如何才能为它添加Button Click事件?请帮我?

提前致谢... Sambasiva

1 个答案:

答案 0 :(得分:1)

您无法在GridView Control中动态添加控件,

您需要创建一个类并将其扩展为ITemplate。

以下链接将解释它

http://www.codedigest.com/Articles/ASPNET/168_Create_Dynamic_GridView_Control_in_C_ASPNet.aspx

http://www.codeproject.com/Articles/13462/How-to-create-template-columns-dynamically-in-a-gr