Format Currency without currency symbol

Here is a way I came up with in a TemplateField: 

(my machine’s culture is US, so may need to change the Replace currency symbol if your culture is different)

<asp:TemplateField HeaderText="amount" SortExpression="amount">

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text=’<%# Convert.ToString(Eval("amount","{0:c4}")).Replace("$", "") %> ></asp:Label>

</ItemTemplate>

</asp:TemplateField>

Advertisements

Add Another Footer to GridView

I came across this link to find out another approach to add another Footer to the GridView(or not show Footer)

http://hi.baidu.com/myaspdotnet/blog/item/715437ddde7b73dd8c1029d3.html

 

protected void GridView1_OnPreRender(object sender, EventArgs e)
    {
        if (GridView1.Controls.Count > 0 && GridView1.Controls[0].Controls.Count > 1)
        {
 
// //public GridViewRow(
//    int rowIndex,
//    int dataItemIndex,
//    DataControlRowType rowType,
//    DataControlRowState rowState
//)

 
GridViewRow gr = new GridViewRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
TableCell cell = new TableCell();
cell.Text = "&lt;hr style='color:yellow; height: 5px' />";
cell.Attributes["colspan"] = GridView1.Columns.Count.ToString(); //merge columns
gr.Controls.Add(cell);
//gr.Cells[0].HorizontalAlign = HorizontalAlign.Center;
GridView1.Controls[0].Controls.AddAt(GridView1.Controls[0].Controls.Count - 1, gr);
}


Merge columns in GridView footer and add a line

Merge columns in the footer is not that straightforward. Here is another solution other than through CSS to put an <hr> in the footer:

<asp:GridView ID="GridView1" ShowFooter="true"  OnRowDataBound="GridView1_OnRowDataBound" …>

 

protected void GridView1_OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            //remove all cells but one, so GridView1.Columns.Count-1
            for (int i = 0; i < GridView1.Columns.Count-1; i++)
            {
                e.Row.Cells.RemoveAt(0);
            }
           
           e.Row.Cells[0].ColumnSpan = GridView1.Columns.Count;
           e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;
           e.Row.Cells[0].Text = " <hr style='color:yellow; height: 5px' />";

        }
        
    }
 CSS way:

Add a bottom border style to your gridview(you may not need your footer):

  <asp:GridView ID="GridView1" CssClass="bottomBorder".......................>

<

head runat="server">

<title>Your Page</title>

 <style type="text/css">
.bottomBorder {border-bottom: black medium solid; }
</style>

</head>

 


Selective Sorting with AutoGeneratedColumns in GridView

One user from ASP.NET forum was asking how to do this. (http://forums.asp.net/t/1191176.aspx)

Here is the solution for that. 

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {    if (e.Row.RowType == DataControlRowType.Header)

{
    Control ctl0 = e.Row.Controls[2];// column 3
    Control ctl = ctl0.Controls[0]; //first control in column 3

    if (ctl is LinkButton)
    {
     ctl0.Controls.Remove(ctl);
     ctl0.Controls.AddAt(0, new LiteralControl((ctl as LinkButton).Text));
    }
}

GridView1:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataKeyNames="id" AllowSorting="true"
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_OnRowDataBound"></asp:GridView


Update from a DropDownList in ItemTemplate of GridView

protected void changeStatus(object sender, EventArgs e)
    {
        GridViewRow row = ((Control)sender).NamingContainer as GridViewRow;

        if (row != null)
        {
          
            DataKey keys = GridView1.DataKeys[row.RowIndex];
            SqlDataSource1.UpdateParameters.Add("SubID", keys.Values[0].ToString());

            DropDownList myddlStatus = (DropDownList)row.FindControl("ddlStatus");           
            SqlDataSource1.UpdateParameters.Add("Status", myddlStatus.SelectedValue.ToString());

            SqlDataSource1.Update();          

        }
 
 This is the markup of the GridView1:
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SubID" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="SubID" HeaderText="SubID" ReadOnly="True" SortExpression="SubID" />
                <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                <asp:BoundField DataField="SubName" HeaderText="SubName" SortExpression="SubName" />
                <asp:TemplateField HeaderText="Status" >                   
                    <ItemTemplate>
                   <asp:DropDownList ID="ddlStatus" runat="server" Selectedvalue='<%#Bind("Status") %>' AutoPostBack="true" OnSelectedIndexChanged="changeStatus">

<asp:ListItem Text="Make Active" Value="Make Active"></asp:ListItem> 
<asp:ListItem Text="Permanently Delete" Value="Permanently Delete"></asp:ListItem>
<asp:ListItem Text="Archive" Value="Archive"></asp:ListItem>

</asp:DropDownList>

                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"
                      SelectCommand="SELECT [SubID], [Status], [SubName] FROM [CatSubTable2]" 
                      UpdateCommand="UPDATE [CatSubTable2] SET [Status] = @Status WHERE [SubID] = @SubID">

</asp:SqlDataSource>

  


A Sample for TextBox’s OnTextChanged event fired from Repeater for subTotal and Total

This sample includes how to access control in Footertemple of Repeater and hot to get item totals.

Here is one way to access the label in the footer:

Label lblTotal = (Label)Repeater1.Controls[Repeater1.Controls.Count-1].FindControl("lblTotal");

 

protected void PriceTextBox_OnTextChanged(object sender, EventArgs e)

{

TextBox tb0 = ((TextBox)(sender));

RepeaterItem rp1 = ((RepeaterItem)(tb0.NamingContainer));

TextBox tb1 = (TextBox)rp1.FindControl("priceTextBox");

Label lbl2 = (Label)rp1.FindControl("lblprice2");

Label lbl3 = (Label)rp1.FindControl("lblprice3");

Label lbl4 = (Label)rp1.FindControl("lblSubtotal");

lbl2.Text =

Convert.ToString(Convert.ToDouble(tb1.Text) * 0.2);

lbl3.Text = Convert.ToString(Convert.ToDouble(tb1.Text) * 1.2);

lbl4.Text =

Convert.ToString(Convert.ToDouble(tb1.Text) + Convert.ToDouble(lbl2.Text) + Convert.ToDouble(lbl3.Text));

 

gTotal = 0;

foreach (RepeaterItem Item in Repeater1.Items)

{

double subtotal = Convert.ToDouble(((Label)Item.FindControl("lblSubtotal")).Text);

gTotal += subtotal;

}

Label lblTotal = (Label)Repeater1.Controls[Repeater1.Controls.Count-1].FindControl("lblTotal");

lblTotal.Text = gTotal.ToString();  

}

double gTotal = 0.0;

protected void Repeater1_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)

{

 

RepeaterItem item = e.Item;

if ((item.ItemType == ListItemType.Item) ||

(item.ItemType == ListItemType.AlternatingItem))

{

Label tbsubTotal = (Label)e.Item.FindControl("lblSubtotal");

 

DataRowView drv = (DataRowView)item.DataItem;

double p1;

double p2;

double p3;

if (drv["price"] == DBNull.Value)

{

p1 = 0;

}

else

{

p1 =

Convert.ToDouble(drv["price"]);

}

if (drv["price2"] == DBNull.Value)

{

p2 = 0;

}

else

{

p2 =

Convert.ToDouble(drv["price2"]);

}

if (drv["price3"] == DBNull.Value)

{

p3 = 0;

}

else

{

p3 =

Convert.ToDouble(drv["price3"]);

}

double pTotal = p1 + p2 + p3;

 

 

tbsubTotal.Text = pTotal.ToString();

 

double subtotal = Convert.ToDouble(((Label)e.Item.FindControl("lblSubtotal")).Text);

gTotal += subtotal;

 

}

if (item.ItemType == ListItemType.Footer)

{

Label tbTotal = (Label)e.Item.FindControl("lblTotal");

tbTotal.Text = gTotal.ToString();

}

 }

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound" >

<ItemTemplate>

<table><tr><td>myindex1:<%# Container.ItemIndex %> </td>

<td><asp:Label ID="IDLabel1" runat="server" Text=’<%# Eval("ID") %>‘></asp:Label></td>

<td> price:

<asp:TextBox ID="priceTextBox" runat="server" Text=’<%# Bind("price") %> AutoPostBack="true" OnTextChanged="PriceTextBox_OnTextChanged"></asp:TextBox>

</td>

<td> price2:

<asp:Label ID="lblprice2" runat="server" Text=’<%# Bind("price2") %> />

</td>

<td> price3:

<asp:Label ID="lblprice3" runat="server" Text=’<%# Bind("price3") %> />

</td>

<td> >>>: <asp:Label ID="lblSubtotal" runat="server" /> </td>

</tr>

</

ItemTemplate>

<

FooterTemplate>

<tr><td colspan="6" align="right">Total:<asp:Label ID="lblTotal" runat="server" /></td></tr>

</table>

 

</

FooterTemplate>

</asp:Repeater>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:test_for_forumConnectionString %>"

SelectCommand="SELECT [ID],[price], [price2], [price3] FROM [money1]" >

</asp:SqlDataSource>


Alternate color by group of rows in a gridview

Here is a sample to alternate color by group of rows in a gridview. The grouping is based on a column named "CatName" in my sample. You can use whatever column you want in your code with your gridview for grouping.

  string strGroup = "";
    Int32 t = 0; 
  
    protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            DataRowView drv = (DataRowView)e.Row.DataItem;

            if (!(drv["CatName"].ToString() == strGroup))
            {
             
                strGroup = drv["CatName"].ToString();            
                t += 1;
            }
            if (t % 2 == 0)
            {
                e.Row.BackColor = System.Drawing.Color.Azure;
            }
            else
            {
                e.Row.BackColor = System.Drawing.Color.GhostWhite;
            }

        
        }
    }