Monday, July 28, 2008

Creation of new Column in Gridview at Runtime

Output is:










Html Code is:





Source Code(c#) is:

using System.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();
}


}
}

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...