Nov 27 2009

Add Gridview Column programmatically and fill data

Binding Column total with column name TotalList

First add BoundColumn to gridview Modify that BoundColumn HeaderText is ItemsTotal and leave empty to DataField option of BoundField . This Datafield runtime makes total to customer wise items.

<asp:BoundField DataField=”CustomerName” HeaderText=”CustomerName” />

<asp:BoundField DataField=”ItemAPrice” HeaderText=”ItemA_Price” />

<asp:BoundField DataField=”ItemBPrice” HeaderText=”ItemB_Price” />

<asp:BoundField DataField=”ItemCPrice” HeaderText=”ItemC_Price” />

<asp:BoundField HeaderText=”ItemsTotal” />

Note: leave DataField to ItemTotal DataField will add programatically at runtime.

see below figure

gridview add column data

gridview add column data

Refer for above data table and connection and adding gridview footer total

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

}

if (e.Row.RowType == DataControlRowType.Footer)

{

}

int count = GridView1.Rows.Count;

for (int i = 0; i < count; i++)

{

decimal CustItemA = Convert.ToDecimal(GridView1.Rows[i].Cells[1].Text);

decimal CustItemB = Convert.ToDecimal(GridView1.Rows[i].Cells[2].Text);

decimal CustItemC = Convert.ToDecimal(GridView1.Rows[i].Cells[3].Text);

GridView1.Rows[i].Cells[4].Text = (CustItemA + CustItemB + CustItemC).ToString();

}

}

Nov 27 2009

Display Sum total of Asp.net Gridview Footer

Step1: First Create table tblPurchase in Purchase Database

CREATE TABLE [dbo].[tblPurchase](

[PurchaseID] [int] IDENTITY(1,1) NOT NULL,

[CustomerName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,

[ItemAPrice] [decimal](18, 2) NOT NULL,

[ItemBPrice] [decimal](18, 2) NOT NULL,

[ItemcPrice] [decimal](18, 2) NOT NULL

) ON [PRIMARY]

Insert values into tblPurchase table

insert into tblPurchase values(’Mahesh’,125.00,100.00,150.00);

insert into tblPurchase values(’somesh’,120.00,125.00,150.00);

insert into tblPurchase values(’rajesh’,130.00,150.00,160.00);

insert into tblPurchase values(’santhu’,140.00,180.00,100.00);

Step2: In Visual Studio Drag Gridview Control and Bound Above fields

CustomerName, ItemAPrice, ItemBPrice and ItemCPrice

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”

Font-Names=”Verdana” Font-Size=”9pt”>

<Columns>

<asp:BoundField DataField=”CustomerName” HeaderText=”CustomerName” />

<asp:BoundField DataField=”ItemAPrice” HeaderText=”ItemA_Price” />

<asp:BoundField DataField=”ItemBPrice” HeaderText=”ItemB_Price” />

<asp:BoundField DataField=”ItemCPrice” HeaderText=”ItemC_Price” />

</Columns>

</asp:GridView>

using System.Data.SqlClient;

using System.Data;

protected void Page_Load(object sender, EventArgs e)

{

GetPurchaseItems();

}

public void GetPurchaseItems()

{

string ConnectionString = “Data Source=lokesh;Initial Catalog=Purchase;User;Password=xyz;”;

SqlConnection objConn = new SqlConnection(ConnectionString);

string query = “select *from tblPurchase”;

SqlCommand objCmd = new SqlCommand(query, objConn);

objCmd.CommandType = CommandType.Text;

DataSet objDs = new DataSet();

SqlDataAdapter objDa = new SqlDataAdapter(objCmd);

objConn.Open();

objDa.Fill(objDs);

objCmd.ExecuteNonQuery();

GridView1.DataSource = objDs;

GridView1.DataBind();

//Runtime changing Gridview Footer Style

//uncomment below line when ShowFooter =True in Gridview property

//GridView1.FooterRow.Style.Add(”font-weight”, “bold”);

objConn.Close();

}

ShowFooter=false

ShowFooter=false

Now we are going to sum ItemA_Price, ItemB_Price and ItemC_Price in Gridview Footer Template. To show Sum of gridview Bound Fields first Change the gridview showFooter property. Keep if gridview not showing footer

ShowFooter = True

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False”

Font-Names=”Verdana” Font-Size=”9pt” ShowFooter=”True”>

In Default.aspx.cs

First Declare Decimal values to ItemA,ItemB and ItemC  then double click in Gridview Row Databound.

decimal dItemA = 0, dItemB = 0, dItemC = 0;

protected void Page_Load(object sender, EventArgs e)

{

GetPurchaseItems();

}

public void GetPurchaseItems()

{

//code is above

//changing Gridview Footer style at Runtime

//GridView1.FooterRow.Style.Add(”font-weight”, “bold”);

}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

//gridview total sum gridview total footer

dItemA += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, “ItemAPrice“));

dItemB += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, “ItemCPrice“));

dItemC += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, “ItemCPrice“));

}

if (e.Row.RowType == DataControlRowType.Footer)

{

e.Row.Cells[0].Text = “Total”;

e.Row.Cells[1].Text = dItemA.ToString();

e.Row.Cells[2].Text = dItemB.ToString();

e.Row.Cells[3].Text = dItemC.ToString();

//applying backcolor font font-weight to gridview Footer cell

e.Row.Cells[0].BackColor = System.Drawing.Color.Orange;

}

}

Run the Application

gridview_footer_sum

gridview_footer_sum

happy coding…

Alibi3col theme by Themocracy