This code for dynamically creating controls and gridview also..
see this example code...
public void Tally()
{
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 = " " + "Branch Date:" + " ";
tc.Controls.Add(lblbranchdate);
Label lblbranchdatedata = new Label();
lblbranchdatedata.ID = "lbltotnumbird1" + i.ToString();
lblbranchdatedata.Text = branchdate + " ";
tc.Controls.Add(lblbranchdatedata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lbltotwt1 = new Label();
lbltotwt1.ID = "lbltotwt1" + i.ToString();
lbltotwt1.Text = "Total Weight:" + " ";
tc.Controls.Add(lbltotwt1);
Label lbltotwt1data = new Label();
lbltotwt1data.ID = "lbltotwt1data" + i.ToString();
lbltotwt1data.Text = totwt + " ";
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 = "Total Birds:" + " ";
tc.Controls.Add(lbltotbirds);
Label lbltotbirddata = new Label();
lbltotbirddata.ID = "lbltotbirdata" + i.ToString();
lbltotbirddata.Text = totbirds + " ";
tc.Controls.Add(lbltotbirddata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lblmeatyield = new Label();
lblmeatyield.ID = "lblmeatyield" + i.ToString();
lblmeatyield.Text = "Meat Yield in Kgs:" + " ";
tc.Controls.Add(lblmeatyield);
Label lblmeatyielddata = new Label();
lblmeatyielddata.ID = "lblmeatyied" + i.ToString();
lblmeatyielddata.Text = meatyield + " ";
tc.Controls.Add(lblmeatyielddata);
tr.Cells.Add(tc);
tbldynamic.Rows.Add(tr);
Label lblavgweight = new Label();
lblavgweight.ID = "lblavgweight" + i.ToString();
lblavgweight.Text = "Avg Weight:" + " ";
tc.Controls.Add(lblavgweight);
Label lblavgweightdata = new Label();
lblavgweightdata.ID = "lblavgweightdata" + i.ToString();
lblavgweightdata.Text = Avgweight + " ";
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.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);
gv.AutoGenerateColumns = false;
gv.ShowFooter = true;
gv.DataSource = dtdcDetails;
gv.DataBind();
GridViewRow footerRow = gv.FooterRow;
if (footerRow != null)
{
double totalSum = 0; double totalsum1 = 0;
foreach (DataColumn c in dtdcDetails.Columns)
{
if (c.ColumnName.ToUpper() == "TOTALWEIGHT")
{
double sum = 0; double total = 0;
int k = 0;
foreach (DataRow rw in dtdcDetails.Rows)
{
if (dtdcDetails.Rows[k]["Birdname"].ToString() != "Eggs")
{
sum = sum + Convert.ToDouble(rw[c].ToString());
footerRow.Cells[dtdcDetails.Columns.IndexOf(c.ColumnName) + 1].Text = sum.ToString();
}
else {
total = total + Convert.ToDouble(rw[c].ToString());
}
k++;
}
totalSum = Convert.ToDouble(Math.Round((totalSum + sum),2));
totalsum1 = Convert.ToDouble(Math.Round((totalsum1 + total),2));
}
else if (c.ColumnName.ToUpper() == "birdname")
{
footerRow.Cells[dtdcDetails.Columns.IndexOf(c.ColumnName)].Text = "Sub Total";
}
}
lblTotal.Text = "Total Meat: " + totalSum+"Kg"+" ";
lbltotaleggs.Text = "Total Eggs: " + totalsum1;
Style s = new Style();
s.BorderStyle = BorderStyle.Groove;
s.Font.Bold = true;
s.ForeColor = System.Drawing.Color.DimGray;
footerRow.ApplyStyle(s);
double totsum;
totsum = Convert.ToSingle(meatyield.ToString());
if (totsum == totalSum)
{
lblmeatyield.ForeColor = System.Drawing.Color.Green;
lblmeatyielddata.ForeColor = System.Drawing.Color.Green;
}
else
{
lblmeatyield.ForeColor = System.Drawing.Color.Red;
lblmeatyielddata.ForeColor = System.Drawing.Color.Red;
}
}
divmeat.Controls.Add(tbldynamic);
divmeat.Controls.Add(gv);
divmeat.Controls.Add(lblTotal);
divmeat.Controls.Add(lbltotaleggs);
LiteralControl @break = default(LiteralControl);
@break = new LiteralControl("<br />");
divmeat.Controls.Add(@break);
LiteralControl @break1 = default(LiteralControl);
@break1 = new LiteralControl("<br />");
divmeat.Controls.Add(@break1);
}
}
}
}