Wednesday, 30 October 2013

How to Export Gridview Data to Excel Format using Asp.net

In this post i am going to Explain how to How to Convert Gridview data to Excel Format .
 In Source Code  we need to write EnableEventValidation="false" for Verify Rendering In Server Form

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="false" %>
first we need to Bind Gridview after that we need to write this code in button click event.

protected void btnExcel_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "EmployeeDetals.xls"));
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        //Change the Header Row back to white color
        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");
        //Applying stlye to gridview header cells
        for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
        {
            GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1");
        }
        int j = 1;
        //This loop is used to apply stlye to cells based on particular row
        foreach (GridViewRow gvrow in GridView1.Rows)
        {
            gvrow.BackColor = Color.White;
            if (j <= GridView1.Rows.Count)
            {
                if (j % 2 != 0)
                {
                    for (int k = 0; k < gvrow.Cells.Count; k++)
                    {
                        gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
                    }
                }
            }
            j++;
        }
        GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

Create view for get Highest Salary for Each Department using SubQuery

CREATE View [dbo].[VU_SecondHSalaryEachDept]
AS
SELECT  Department, MAX(TotalSalary) Salary FROM VU_EMP_Employees  AS TempTable
WHERE TotalSalary <
(SELECT MAX(TotalSalary)
FROM VU_EMP_Employees
WHERE Department = TempTable.Department)
GROUP BY Department
GO

Create One Stored Procedure for Insert, Update ,Select and Delete

CREATE PROC [dbo].[SP_SelectInsertUpdateDeleteEmployee]
(
@Empid int,
@EmployeeNo nvarchar(200),
@EmployeeName nvarchar(200),
@Department nvarchar(200),
@JobRole nvarchar(200),
@Salary decimal(18,3),
@DateOfJoining datetime,
@PhoneNo nvarchar(50),
@Email nvarchar(50),
@Sex nchar(20),
@StatementType nvarchar(20)=''
)
AS
Begin
if(@StatementType='Insert')
Begin
Insert into EMP_Employees(EmployeeNo,EmployeeName,Department,Salary,JobRole,DateOfJoining,PhoneNo,Email,Sex)
values(@EmployeeNo,@EmployeeName,@Department,@Salary,@JobRole,@DateOfJoining,@PhoneNo,@Email,@Sex)
End
if(@StatementType='Select')
Begin
Select * from EMP_Employees
End
if(@StatementType='Update')
Begin
Update EMP_Employees
Set EmployeeNo=@EmployeeNo, EmployeeName=@EmployeeName,Department=@Department,Salary=@Salary,JobRole=@JobRole,
DateOfJoining=@DateOfJoining,PhoneNo=@PhoneNo,Email=@Email,Sex=@Sex
where EMPID=@Empid
End
if(@StatementType='Delete')
Delete from EMP_Employees where EMPID=@Empid
End

Gridview Custom Button Click Event

In this Post i am going to Explain how to get value form Gridview Custom button Click event in side gridiview

in Source code
<div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                <Columns>
                    <asp:BoundField DataField="CreatedBy" Visible="true" />
                    <asp:BoundField DataField="TransactionAmount" Visible="true" />
                    <asp:BoundField DataField="PaymentMode" Visible="true" />
                    <asp:TemplateField HeaderText="Action" ValidateRequestMode="Disabled">
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" Text="Payment" runat="server" OnClick="btnEdit_Click" CausesValidation="false" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <asp:Label ID="lbl1" runat="server"></asp:Label>
            <asp:Label ID="lbl12" runat="server"></asp:Label>
            <asp:Label ID="lbl3" runat="server"></asp:Label>
        </div>



in .cs code

 protected void btnEdit_Click(object sender, EventArgs e)
        {
            Button btnEdit = ((Button)sender);
            GridViewRow grdrow = (GridViewRow)btnEdit.NamingContainer;
            string rowNumber = grdrow.Cells[0].Text;
            string dealId = grdrow.Cells[1].Text;
            string dealTitle = grdrow.Cells[2].Text;
            lbl1.Text = rowNumber; lbl12.Text = dealId; lbl3.Text = dealTitle;
        }

Deveexpress Date Eadit Validating two Dates in Javascript using CustomeValidator

In this post i am going to explain how we can validate two DateEdit control which are Devexpress control using Custom Validator.

First drag and drop two devexpress datedit controls on the form

<dx:ASPxDateEdit ID="ASPxDateEdit1" ClientInstanceName="cliDate1" runat="server"
            DisplayFormatString="dd-MMM-yy" EditFormatString="dd-MMM-yy">
        </dx:ASPxDateEdit>
        <dx:ASPxDateEdit ID="ASPxDateEdit2" runat="server" ClientInstanceName="cliDate2"
            DisplayFormatString="dd-MMM-yy" EditFormatString="dd-MMM-yy">
        </dx:ASPxDateEdit>
        <asp:CustomValidator ID="cvDueDate" runat="server" ValidationGroup="vgCheck" ControlToValidate="ASPxDateEdit2"
            Display="Dynamic" ClientValidationFunction="compare"></asp:CustomValidator>

Script to compare the two dates on client side

<script type="text/javascript">
 function compare1(s, e) {
            var orderDate = cliDate1.GetDate();
            if (orderDate != null) {
                var dueDate = cliDate2.GetDate();
                if (dueDate != null) {
                    var orderYear = orderDate.getFullYear();
                    var orderMonth = orderDate.getMonth();
                    var orderDay = orderDate.getDate();
                    var corderDate = new Date(orderYear, orderMonth, orderDay);

                    var dueYear = dueDate.getFullYear();
                    var dueMonth = dueDate.getMonth();
                    var dueDay = dueDate.getDate();
                    var cDueDate = new Date(dueYear, dueMonth, dueDay);

                    if (cDueDate.toString() === corderDate.toString()) {
                        e.IsValid = true;
                    }
                    else if (cDueDate < corderDate) {
                        e.IsValid = false;
                    }
                    else {
                        e.IsValid = true;
                    }
                }
            }
        }
</script>