Monday, 28 May 2012

Subtotal in Gridview control

Subtotal in Gridview control in order Details page on Raasa.


Step 1 :

<asp:GridView ID="gvworderdetails" runat="server"  AutoGenerateColumns="False"
                                onrowdatabound="gvworderdetails_RowDataBound" ShowFooter="True"
                                Width="100%" >
                                <Columns>
                                    <asp:BoundField DataField="type" HeaderText="Type" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="20px"/>
                                    <asp:BoundField DataField="item_desc" HeaderText="Item Description" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="20px"/>
                                    <asp:BoundField DataField="status" HeaderText="Status" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="20px"/>
                                    <%--<asp:BoundField DataField="details" HeaderText="Details" />--%>
                                    <asp:BoundField DataField="price" HeaderText="Price" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"/>
                                       
                                    <asp:TemplateField HeaderText="Qty">
                                    <FooterStyle Font-Bold="true" Font-Size="13px" />
                                        <FooterTemplate>Grand Total :</FooterTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblqty" runat="server" Text='<%# Eval("qty") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" Height="20px"/>
                                        <FooterStyle HorizontalAlign="Right" Height="20px"/>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Sub Total">
                                        <ItemTemplate>
                                            <asp:Label ID="lblsubtot" runat="server" Text='<%# Eval("SubTotal") %>'></asp:Label>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:Label ID="lbltotal" runat="server" Font-Bold="True" Font-Size="13px"></asp:Label>
                                        </FooterTemplate>
                                        <ItemStyle HorizontalAlign="Center" Height="20px"/>
                                        <FooterStyle HorizontalAlign="Center" Height="20px"/>
                                    </asp:TemplateField>                                      
                                </Columns>
                                <HeaderStyle Height="25px" BackColor="#3b6c01" ForeColor="#ffffff" />
                            </asp:GridView>




Step 2 :



decimal total = 0;
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        DataSet ds2 = new DataSet();


 public void BindGrid(string order_id)
        {
            //
            bl_raasa csobj = new bl_raasa();
            ds2 = csobj.GetGridBind(order_id);
            if (ds2.Tables[0].Rows.Count > 0)
            {
                gvworderdetails.DataSource = ds2.Tables[0];
                gvworderdetails.DataBind();
            }
        }

        protected void gvworderdetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                total += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "SubTotal"));
              
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lbl = (Label)e.Row.FindControl("lblTotal");
                lbl.Text = total.ToString() ;
            }
        }




Used Raddiobutton in Datalist control





Raasa.in (Impact)

<script  language="javascript" type="text/javascript">
    function CheckOnes(spanChk) {

        var oItem = spanChk.children;
        var theBox = (spanChk.type == "radio") ? spanChk : spanChk.children.item[0];

        xState = theBox.unchecked;
        elm = theBox.form.elements;

        for (i = 0; i < elm.length; i++) {
            if (elm[i].type == "radio" && elm[i].id != theBox.id) {
                elm[i].checked = xState;
            }
        }
    }

    function IsNumericOnly(evt) {
        var charCode = ((evt.which) ? evt.which : evt.keyCode);
        if (charCode == 37 || charCode == 39)
            return true;
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
    }
</script>







<asp:DataList ID="dl_address" runat="server" BackColor="White"
                                BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="0" CellPadding="6" CellSpacing="6"
                                ForeColor="Black" GridLines="None"
                                onitemdatabound="dl_address_ItemDataBound" RepeatDirection="Horizontal" RepeatColumns="3">
                                <ItemTemplate>
                                    <div align="center" style="width:200px;background-color:#f3f3f3; border:4px solid #666;">
                                        <table width="100%" cellpadding="1" cellspacing="1" border="0">
                                            <tr>
                                                <td align="left">

                                                    <span style="font-size:13px; font-weight:bold;"><%#Eval("name") %></span> <br />
                                                    <%#Eval("street_addr")%> <br />
                                                    <%#Eval("landmark")%><br />
                                                    <%#Eval("city")%> <br />
                                                    <%#Eval("state") %>, <%#Eval("country")%> <br />
                                                    <%#Eval("pincode")%>, <%#Eval("ph_no")%><br />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="center"><br /><hr style="width:95%;"/>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="center">
                                                        <asp:RadioButton ID="rdb" runat="server"  OnCheckedChanged="rdb_click" AutoPostBack="True" />&nbsp;Default Address           
                                                        <asp:HiddenField ID="HiddenField1" runat="server" Value = '<%#Eval("ID")%>' />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="center"><br /><hr style="width:95%;"/>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td align="center">
                                                    <asp:LinkButton ID="lnkdeladdr" runat="server"  CausesValidation="false"
                                                        CommandArgument='<%#Eval("ID")%>' onclick="lnkdeladdr_Click">Delete Address</asp:LinkButton>
                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                </ItemTemplate>
                                <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                                <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                                <SelectedItemStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                            </asp:DataList>


.cs




  public void Bind(string email)
        {
            bl_raasa csobj = new bl_raasa();
            ds = csobj.GetAddressDetails("raasa_user_shipaddress", "email='" + email + "' and status='A'");

            if (ds.Tables[0].Rows.Count > 0)
            {
                //Session["Dataset"] = ds;
                dl_address.DataSource = ds.Tables[0];
                dl_address.DataBind();
            }
        }
     
        protected void dl_address_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            RadioButton rdb;
            rdb = (RadioButton)e.Item.FindControl("rdb");
            if (rdb != null)
                rdb.Attributes.Add("onclick", "CheckOnes(this);");
        }
        protected void rdb_click(object sender, EventArgs e)
        {
            for (int i = 0; i < dl_address.Items.Count; i++)
            {
                RadioButton rdb;
                rdb = (RadioButton)dl_address.Items[i].FindControl("rdb");
                if (rdb != null)
                {
                    if (rdb.Checked)
                    {
                        HiddenField hf = (HiddenField)dl_address.Items[i].FindControl("HiddenField1");
                        bl_raasa csobj = new bl_raasa();
                        int errNo = 0;
                        string errMsg = null;
                        csobj.Update_Address_status(Convert.ToInt32(hf.Value),out errNo,out errMsg);
                        //Response.Write(hf.Value);
                        if (errNo == 0)
                        {

                        }
                        else
                        {

                        }
                    }

                }
            }
        }


Search and highlight Results in Grdiview


Step1:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="searchhigh.aspx.cs" Inherits="searchhigh" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search and highlight Results in Grdiview
</title>
<style type="text/css">
.highlight {text-decoration:none; font-weight:bold;
color:black; background:yellow;}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1"
                  runat="server" />
<asp:UpdatePanel ID="UpdatePanel1"
                runat="server">
<ContentTemplate>
<div>
<asp:GridView ID="grdSearch" runat="server"
    BackColor="white" BorderColor="Aqua"
    AutoGenerateColumns="false" ShowFooter="true"
    OnRowCommand="grdSearch_RowCommand">
    <FooterStyle BackColor="AliceBlue"
    ForeColor="AntiqueWhite" />
    <PagerStyle BackColor="#99CCCC"
    ForeColor="#003399" HorizontalAlign="Left" />
    <HeaderStyle BackColor="#003399"
    Font-Bold="True" ForeColor="#CCCCFF" />

<Columns>
    <asp:TemplateField HeaderText="FirstName">
    <ItemTemplate>
    <asp:Label ID="lblname" runat="server"
Text='<%# Highlight(Eval("name").ToString()) %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox ID="txtSearch" runat="server"
         Width="70px"></asp:TextBox>
    <asp:Button ID="btnSearch"
                CommandName="Search"
                runat="server" Text="Search"
                Width="60px" />
    </FooterTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="organization"
         HeaderText="Org" />
</Columns>
     </asp:GridView>
     </div>
     </ContentTemplate>
     </asp:UpdatePanel>
     <asp:UpdateProgress ID="UpdateProgress1"
                         runat="server">
     <ProgressTemplate>
     <br />
     <img src="Images/ajax.gif"
          alt="Searchig"  />
     </ProgressTemplate>
     </asp:UpdateProgress>
</form>
</body>
</html>


Step 2 :

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

public partial class searchhigh : System.Web.UI.Page
{
    public SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private DataTable GetRecords()
    {

        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select top 5 * from msppt_com_impact.ASAXPartnerEDM_Reg order by id asc";
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        DataSet objDs = new DataSet();
        dAdapter.Fill(objDs);
        return objDs.Tables[0];

    }
    private void BindGrid()
    {
        DataTable dt = GetRecords();
        if (dt.Rows.Count > 0)
        {
            grdSearch.DataSource = dt;
            grdSearch.DataBind();
        }
    }
    private void SearchText(string strSearchText)
    {
        DataTable dt = GetRecords();
        DataView dv = new DataView(dt);
        string SearchExpression = null;
        if (!String.IsNullOrEmpty(strSearchText))
        {
            SearchExpression = string.Format("{0} '%{1}%'",
            grdSearch.SortExpression, strSearchText);

        }
        dv.RowFilter = "name like" + SearchExpression;
        grdSearch.DataSource = dv;
        grdSearch.DataBind();
    }
    protected void grdSearch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        System.Threading.Thread.Sleep(2000);
        if (e.CommandName == "Search")
        {
            TextBox txtGrid = (TextBox)grdSearch.FooterRow.FindControl("txtSearch");
            SearchText(txtGrid.Text);
        }
    }
    public string Highlight(string InputTxt)
    {
        GridViewRow gvr = grdSearch.FooterRow;
        if (gvr != null)
        {
            TextBox txtExample = (TextBox)grdSearch.FooterRow.FindControl("txtSearch");

            if (txtExample.Text != null)
            {
                string strSearch = txtExample.Text;
                // Setup the regular expression

                Regex RegExp = new Regex(strSearch.Replace(" ", "|").Trim(),
                               RegexOptions.IgnoreCase);


                //Highlight keywords by calling the delegate
                //each time a keyword is found.
                return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));

                // Set the RegExp to null.
                RegExp = null;

            }
            else
                return InputTxt;
        }
        else
        {
            return InputTxt;
        }
    }

    public string ReplaceKeyWords(Match m)
    {
        return "<span class=highlight>" + m.Value + "</span>";
    }
}

Step 1: 


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="serchhilight.aspx.cs" Inherits="serchhilight" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Grid Search example</title>
    <style type="text/css">
    .highlight {
    text-decoration:none; font-weight:bold;
    color:black; background:yellow;}
    </style>
</head>
<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />

    <div>
            Enter name to search:
    </div>
    <div>
        <asp:TextBox ID="txtSearch" runat="server"
AutoPostBack="True"
OnTextChanged="txtSearch_TextChanged">
</asp:TextBox>&nbsp;
    </div>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                 <asp:GridView ID="grdSearch" runat="server" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField HeaderText="Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblname"
runat="server"
Text='<%# Highlight(Eval("name").ToString()) %>'>
</asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Org">
                                <ItemTemplate>
                                    <asp:Label ID="lblorg" runat="server" Text='<%#(Eval("organization")) %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                            <asp:TemplateField HeaderText="Des">
                                <ItemTemplate>
                                    <asp:Label ID="lbldes" runat="server" Text='<%#(Eval("designation")) %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                </asp:GridView>
            </ContentTemplate>
        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="txtSearch" EventName="TextChanged" />
        </Triggers>
        </asp:UpdatePanel>

    </div>

</form>
</body>
</html>




Step 2:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class serchhilight : System.Web.UI.Page
{
    public SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Con"].ToString());

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private DataTable GetRecords()
    {
       
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select top 5 * from msppt_com_impact.ASAXPartnerEDM_Reg order by id asc";
        SqlDataAdapter dAdapter = new SqlDataAdapter();
        dAdapter.SelectCommand = cmd;
        DataSet objDs = new DataSet();
        dAdapter.Fill(objDs);
        return objDs.Tables[0];

    }
    private void BindGrid()
    {
        DataTable dt = GetRecords();
        if (dt.Rows.Count > 0)
        {
            grdSearch.DataSource = dt;
            grdSearch.DataBind();
        }
    }
    private void SearchText()
    {
        DataTable dt = GetRecords();
        DataView dv = new DataView(dt);
        string SearchExpression = null;
        if (!String.IsNullOrEmpty(txtSearch.Text))
        {
            SearchExpression = string.Format("{0} '%{1}%'",
            grdSearch.SortExpression, txtSearch.Text);

        }
        dv.RowFilter = "name like" + SearchExpression;
        grdSearch.DataSource = dv;
        grdSearch.DataBind();

    }
    public string Highlight(string InputTxt)
    {
        string Search_Str = txtSearch.Text.ToString();
        // Setup the regular expression and add the Or operator.
        Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(),
        RegexOptions.IgnoreCase);

        // Highlight keywords by calling the
        //delegate each time a keyword is found.
        return RegExp.Replace(InputTxt,
        new MatchEvaluator(ReplaceKeyWords));

        // Set the RegExp to null.
        RegExp = null;

    }

    public string ReplaceKeyWords(Match m)
    {

        return "<span class=highlight>" + m.Value + "</span>";

    }

    protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        SearchText();
        txtSearch.Focus();
    }
}