Creation of new Column in Gridview at Runtime

Unknown | 7:35 PM |

out put:
Html code:

stem.Data.SqlClient;

public partial class Default2 : System.Web.UI.Page
{
SqlConnection scn = new SqlConnection("Data Source=.;Initial Catalog=test;Integrated Security=True;Pooling=False");
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetData();
GridView1.DataBind();

}
ArrayList count = new ArrayList();
ArrayList rate = new ArrayList();
ArrayList date = new ArrayList();
int ln,a,b;
public DataSet GetData()
{
scn.Open();
SqlCommand cmd = new SqlCommand("select * from purchase",scn);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
count.Add(sdr["count"].ToString());
rate.Add(sdr["rate"].ToString());
date.Add(sdr["date"].ToString());
}
sdr.Close();
scn.Close();
ln =Convert.ToInt32(count.Count.ToString());
DataSet ds = new DataSet();
DataTable dt = new DataTable("Purchase");
DataRow dr;
dt.Columns.Add(new DataColumn("Id", typeof(Int32)));
dt.Columns.Add(new DataColumn("Date", typeof(string)));
dt.Columns.Add(new DataColumn("Price", typeof(Int32)));
for (int i = 0; i <= ln-1; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = date[i].ToString();
a =Convert.ToInt32(count[i].ToString());
b = Convert.ToInt32(rate[i].ToString());

dr[2] =a*b;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
Session["dt"] = dt;
return ds;
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
DataBinder.Eval(e.Row.DataItem, "Price").ToString(); 
}


}
}


Category:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....