Category: Data Controls

Dec 18 2009

Gridview BoundField row textbox width changing in EditMode

Add controlStyle property inbetween BoundField  control

<ControlStyle Width=”50px” />

———————————————————————————————————

<asp:BoundField DataField=”LOPDays” HeaderText=”LOPDays” >

<ControlStyle Width=”50px” />

</asp:BoundField>

—————————————————————————————————————-

Sample Design code

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

onrowediting=”GridView1_RowEditing” DataKeyNames=”EmpSalID”

onrowcancelingedit=”GridView1_RowCancelingEdit” Width=”900px”

onrowupdating=”GridView1_RowUpdating” ForeColor=”Black” GridLines=”None” >

<FooterStyle BackColor=”#1C5E55″ Font-Bold=”True” ForeColor=”White” />

<RowStyle BackColor=”#E3EAEB” /> <Columns>

<asp:BoundField DataField=”EmpName” HeaderText=”EmployeeName” ReadOnly=”True” />

<asp:BoundField DataField=”EmpCode” HeaderText=”EmpCode” ReadOnly=”True” />

<asp:BoundField DataField=”Salary” HeaderText=”Salary” ReadOnly=”True” />

<asp:BoundField HeaderText=”MonthYear” ReadOnly=”True” />

<asp:BoundField DataField=”LOPDays” HeaderText=”LOPDays” >

<ControlStyle Width=”50px” />

</asp:BoundField>

<asp:BoundField DataField=”Bonus” HeaderText=”Bonus” >

<ControlStyle Width=”100px” />

</asp:BoundField>

<asp:CommandField ShowEditButton=”true”  >                                                                                <ControlStyle Width=”50″ />

</asp:CommandField>

</Columns>

<HeaderStyle BackColor=”#1C5E55″ Font-Bold=”True” ForeColor=”Black”

CssClass=”strip” />

<EditRowStyle BackColor=”#7C6F57″ />

<AlternatingRowStyle BackColor=”White” />

</asp:GridView>

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