In Previous posts I explained lot of articles regarding Gridview. Now I will explain how to implement beautiful looking gridview and filter gridview records with dropdownlist selection using asp.net. To implement this first design the table in database and give name UserInfomation

ColumnName
DataType
UserId
Int(set identity property=true)
UserName
varchar(50)
LastName
varchar(50)
Location
varchar(50)
After completion table creation write some of css classes to change the appearance of gridview and design aspx page like this
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> Beautiful Gridview theme with Filtering option</title>
<%--Styles to Change the appearance of Girdview --%>
<style type="text/css">
.GridviewDiv {font-size: 100%; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Verdana, Arial, Helevetica, sans-serif; color: #303933;}
Table.Gridview{border:solid 1px #df5015;}
.GridviewTable{border:none}
.GridviewTable td{margin-top:0;padding: 0; vertical-align:middle }
.GridviewTable tr{color: White; background-color: #df5015; height: 30px; text-align:center}
.Gridview th{color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;padding:0.5em 0.5em 0.5em 0.5em;text-align:center
.Gridview td{border-bottom-color:#f0f2da;border-right-color:#f0f2da;padding:0.5em 0.5em 0.5em 0.5em;}
.Gridview tr{color: Black; background-color: White; text-align:left}
:link,:visited { color: #DF4F13; text-decoration:none }
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="GridviewDiv">
<table style="width: 420px" border="0" cellpadding="0" cellspacing="1" class="GridviewTable">
<tr >
<td style="width: 40px;">
UserId
</td>
<td style="width: 120px;" >
LastName
</td>
<td style="width: 130px;">
UserName
</td>
<td style="width: 130px;">
Location
</td>
</tr>
<tr >
<td style="width: 40px;">
</td>
<td style="width: 120px;">
</td>
<td style="width: 130px;">
<asp:DropDownList ID="ddlUserName" runat="server" DataSourceID="dsUserName" DataValueField="UserName" AutoPostBack="true" Width="120px" Font-Size="11px" AppendDataBoundItems="true">
<asp:ListItem Text="All" Value="%"/>
</asp:DropDownList>
</td>
<td style="width: 130px;">
<asp:DropDownList ID="ddlLocation" runat="server" DataSourceID="dsLocation" DataValueField="Location" AutoPostBack="true" Width="120px" Font-Size="11px" AppendDataBoundItems="true">
<asp:ListItem Text="All" Value="%"/>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="4">
<asp:GridView runat="server" ID="gvdetails" ShowHeader="false" AllowPaging="true" PageSize="10" DataSourceID="dsdetails" AutoGenerateColumns="false" Width="420px"  CssClass="Gridview">
<Columns>
<asp:BoundField DataField="UserId" HeaderText="UserId" ItemStyle-Width="40px" />
<asp:BoundField DataField="LastName" HeaderText="LastName" ItemStyle-Width="120px" />
<asp:BoundField DataField="UserName" HeaderText="UserName" ItemStyle-Width="130px"/>
<asp:BoundField DataField="Location" HeaderText="Location" ItemStyle-Width="130px"/>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
<asp:SqlDataSource ID="dsUserName" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>" SelectCommand="Select Distinct UserName from UserInformation"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsLocation" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>" SelectCommand="Select Distinct Location from UserInformation"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>" SelectCommand="select * from UserInformation" FilterExpression=" UserName Like '{0}%' and Location Like '{1}%'">
<FilterParameters>
<asp:ControlParameter Name="UserName" ControlID="ddlUserName" PropertyName="SelectedValue" />
<asp:ControlParameter Name="Location" ControlID="ddlLocation" PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
If you observe above code in header section I written css classes by using those we can change the appearance of gridview and written code to bind dropdownlists, gridview and bind the gridview records based on dropdownlists selection.
Here don’t forgot to set the connection string in web.config file here I am getting database connection from web.config file for that reason you need to set the connectionstring in web.config file like this
<connectionStrings>
<add name="dbconnection" connectionString="Data Source=sagar;Integrated Security=true;Initial Catalog=MySampleDB"/>
</connectionStrings>
You can apply backcolor to grid row in 

    Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        e.Row.BackColor = Drawing.Color.AliceBlue 

    End Sub
Introduction

Here I will explain how to call a JavaScript function at regular intervals of time or run/execute JavaScript function every 10 seconds or execute JavaScript function at specific time in asp.net.



To execute function repeatedly with fixed time delay we have a function with two parameters called setInterval(functionname, timedelay).

In this function we need to call the required function in functionname field and we need to set the required time delay to execute the function in timedelay field. If you want to see it in example you need to write the code like as shown below

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Run JavaScript function at specific intervals of time</title>
<script type="text/javascript">
var count=0;
function changeColor() {

// Call function with 500 milliseconds gap
setInterval(starttimer, 500);
}
function starttimer() {
count += 1;
var oElem = document.getElementById("divtxt");
oElem.style.color = oElem.style.color == "red" ? "blue" : "red";
document.getElementById("lbltxt").innerHTML = "Your Time Starts: "+count;
}
</script>
</head>
<body>
<div id="divtxt">
<label id="lbltxt" style="font:bold 24px verdana" />
</div>
<button onclick="changeColor();">Start Timer</button>
</body>
</html>
private void export_to_word()
    {

        try
        {
            data_grid_print.Visible = true;
            //grd_detail.Visible = true;
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.word";

            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            data_grid_print.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());

            Response.End();
            htmlWrite.Flush();
            //grd_detail.Visible = false;
            data_grid_print.Visible = false;
        }
        catch (Exception ex)
        {
        }
    }
 private void export_to_pdf()
    {
        try
        {

            data_grid_print.Visible = true;
            HtmlForm form = new HtmlForm();
            form.Controls.Add(data_grid_print);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
            form.Controls[0].RenderControl(hTextWriter);
            string html = sw.ToString();
            Document Doc = new Document();
            PdfWriter.GetInstance
            (Doc, new FileStream(Environment.GetFolderPath
            (Environment.SpecialFolder.Desktop)
            + "\\Pin_report_unsoldpin.pdf", FileMode.Create));
            Doc.Open();
            Chunk c = new Chunk
            ("PDF Report For Unsoldpin  \n",
            FontFactory.GetFont("Verdana", 10));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Add(c);
            Chunk chunk1 = new Chunk
            (" \n", FontFactory.GetFont("Verdana", 6));
            Paragraph p1 = new Paragraph();
            p1.Alignment = Element.ALIGN_RIGHT;
            p1.Add(chunk1);

            Doc.Add(p);
            Doc.Add(p1);
            System.Xml.XmlTextReader xmlReader =
            new System.Xml.XmlTextReader(new StringReader(html));
            HtmlParser.Parse(Doc, xmlReader);

            Doc.Close();
            string Path = Environment.GetFolderPath
            (Environment.SpecialFolder.Desktop)
            + "\\Pin_report_unsoldpin.pdf";


            //ShowPdf(Path);
            data_grid_print.Visible = false;

        }
        catch (Exception ex)
        {
        }
        finally
        {
        }

    }
private void export_to_excel()
    {
        try
        {
            //grd_detail.Visible = true;
            data_grid_print.Visible = true;
            Response.Clear();

            Response.AddHeader("content-disposition", "attachment;filename=Customerlist.xls");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            data_grid_print.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            data_grid_print.Visible = false;
            //grd_detail.Visible = false;
        }
        catch (Exception ex)
        {
        }
        finally
        {
        }

    }
                              
     SmtpClient smtpclient;
        MailMessage message;
            string str234 = "Name=" + txtname.Text + "\n Address=" + txtaddress.Text 
            + "" + "'\n Phone No=" + txtmobileno.Text + "\n Fax No=" + txtfax.Text
            + "\n Email=" + txtemail.Text + "\n Message=" + txtmessage.Text;

            smtpclient = new SmtpClient();
            message = new MailMessage();

            message.From = new MailAddress("developerlifestyle@gmail.com");
            message.To.Add("developerlifestyle@gmail.com");  //send email to gmail

            message.Subject = "Contact Form";
            message.Body = str234;
            smtpclient.Host = "smtp.gmail.com";
            smtpclient.EnableSsl = true;
            smtpclient.UseDefaultCredentials = true;
            System.Net.NetworkCredential network = new System.Net.NetworkCredential();
            network.UserName = "developerlifestyle@gmail.com"; 
            network.Password = "*******"; //password
            smtpclient.UseDefaultCredentials = true;
            smtpclient.Credentials = network;
            smtpclient.Port = 25;
            smtpclient.Send(message);

Gridview in Asp.Net displays the data in tabular format using rows and columns. Sometimes you may have requirement to group or merge same rows based on data.

For example you are displaying the country data by using Gridview. Grid has CountryId, CountryName and State data. We have requirement to display the country states as group. To merge the Gridview rows we have to use Gridview PreRender event to compare and merge the related rows as shown below.

using System; 
using System.Data; 
using System.Web.UI.WebControls;

namespace GridviewMergeRows 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
        protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
                CreateTable(); 
        }

        private void CreateTable() 
        { 
            try 
            { 
                DataTable dt = new DataTable(); 
                dt.Columns.Add("CountryId"); 
                dt.Columns.Add("CountryName"); 
                dt.Columns.Add("State"); 

                dt.Rows.Add("1", "US", "Alabama"); 
                dt.Rows.Add("1", "US", "Hawaii"); 
                dt.Rows.Add("1", "US", "Georgia"); 
                dt.Rows.Add("1", "US", "Alaska"); 
                dt.Rows.Add("2", "UK", "United Kingdom"); 
                dt.Rows.Add("3", "India", "Delhi"); 
                dt.Rows.Add("3", "India", "Andhra Pradesh"); 
                dt.Rows.Add("3", "India", "Maharashtra");

                gv1.DataSource = dt; 
                gv1.DataBind(); 
            } 
            catch (Exception ex) 
            { } 
        } 

        private void MergeGridviewRows(GridView gridView) 
        { 
            for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--) 
            { 
                GridViewRow row = gridView.Rows[rowIndex]; 
                GridViewRow previousRow = gridView.Rows[rowIndex + 1]; 

                if (row.Cells[0].Text == previousRow.Cells[0].Text && row.Cells[1].Text == previousRow.Cells[1].Text) 
                { 
                    row.Cells[0].RowSpan = previousRow.Cells[0].RowSpan < 2 ? 2 : previousRow.Cells[0].RowSpan + 1; 
                    row.Cells[1].RowSpan = previousRow.Cells[1].RowSpan < 2 ? 2 : previousRow.Cells[1].RowSpan + 1; 

                    previousRow.Cells[0].Visible = false; 
                    previousRow.Cells[1].Visible = false; 
                } 
            } 
        } 

        protected void gv1_PreRender(object sender, EventArgs e) 
        { 
            MergeGridviewRows(gv1); 
        }
     }
} 

As shown above, we are merging the Gridview rows in MergeGridviewRows method by comparing CountryId and CountryName fields. We are calling this MergeGridviewRows method in Gridview PreRender event. 
After merging the related Gridview rows, the output displays as shown below.