Copy & Paste this on .aspx file:
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" CellPadding="4"
BackColor="#5DA63A" ForeColor="White" Width="100%" AllowPaging="false" Style="border-radius: 10px;
border: Solid 3px #5DA63A; text-align: left; word-wrap: break-word; word-break: break-all">
<AlternatingRowStyle BackColor="#F1FCCA"></AlternatingRowStyle>
<RowStyle BackColor="#DEDFDE" ForeColor="Black" CssClass="grid_font" />
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#8EBE12" Font-Bold="False" ForeColor="#E7E7FF" />
<Columns>
<asp:TemplateField ItemStyle-Width="20px">
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True"
oncheckedchanged="chkAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="True"
oncheckedchanged="chkSelect_CheckedChanged" />
</ItemTemplate>
<ItemStyle Width="20px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="EIEUID" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("EIEUid")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Details" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblemail" runat="server" Text='<%#Eval("Email")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblmobile" runat="server" Text='<%#Eval("Mobile")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle Width="120px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Copy & Paste this on .cs Code File:
dataUtility du = new dataUtility();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindGridData();
}
/// This Method is used to bind gridview
private void BindGridData()
{
try
{
DataTable dtnew = new DataTable();
DataTable dt = new DataTable();
dt = du.executeStoteProc("SP_CLIENT 5,'" + Session["Name"] + "','',''");
if (dt.Rows.Count > 0)
{
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
gvDetails.DataSource = dt;
gvDetails.DataBind();
int columncount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvDetails.Rows[0].Cells[0].Text = "No Records Found";
}
}
}
catch (Exception ex)
{
}
}
/// enable gridview control to be rendered
public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ExportToExcell();
}
/// export gridview to excel
private void ExportToExcell()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Details");
dt.Columns.Add("Mobile");
foreach (GridViewRow row in gvDetails.Rows)
{
CheckBox chkCalls = (CheckBox)row.FindControl("chkSelect");
if (chkCalls.Checked == true)
{
int i = row.RowIndex;
Label lblname = (Label)gvDetails.Rows[i].FindControl("lblname");
Label lblemail = (Label)gvDetails.Rows[i].FindControl("lblemail");
Label lblmobile = (Label)gvDetails.Rows[i].FindControl("lblmobile");
DataRow dr = dt.NewRow();
dr["Name"] = Convert.ToString(lblname.Text);
dr["Details"] = Convert.ToString(lblemail.Text);
dr["Mobile"] = Convert.ToString(lblmobile.Text);
dt.Rows.Add(dr);
}
}
GridView gdvExportxls = new GridView();
gdvExportxls.DataSource = dt;
gdvExportxls.DataBind();
gdvExportxls.HeaderStyle.Font.Bold = true;
gdvExportxls.HeaderStyle.ForeColor = Color.White;
gdvExportxls.HeaderStyle.BackColor = Color.Green;
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("attachment;filename=ReferrerDetails.xls", "selectedrows"));
Response.Charset = "";
StringWriter stringwriter = new StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
gdvExportxls.RenderControl(htmlwriter);
Response.Write(stringwriter.ToString().Replace("<div>", " ").Replace("</div>", " "));
Response.End();
}
/// select/unselect all row using header checkbox
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
try
{
CheckBox chkH = (CheckBox)gvDetails.HeaderRow.FindControl("chkAll");
if (chkH.Checked == true)
{
foreach (GridViewRow gv in gvDetails.Rows)
{
CheckBox chk = (CheckBox)gv.FindControl("chkSelect");
chk.Checked = true;
}
}
else
{
foreach (GridViewRow gv in gvDetails.Rows)
{
CheckBox chk = (CheckBox)gv.FindControl("chkSelect");
chk.Checked = false;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
/// select/unselect single row using row checkbox
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
bool isFound = false;
CheckBox cbxall = (CheckBox)gvDetails.HeaderRow.FindControl("chkAll");
foreach (GridViewRow gvr in gvDetails.Rows)
{
CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
if (chkSelect.Checked == false)
{
isFound = true;
break;
}
}
if (isFound)
cbxall.Checked = false;
else
cbxall.Checked = true;
}
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" CellPadding="4"
BackColor="#5DA63A" ForeColor="White" Width="100%" AllowPaging="false" Style="border-radius: 10px;
border: Solid 3px #5DA63A; text-align: left; word-wrap: break-word; word-break: break-all">
<AlternatingRowStyle BackColor="#F1FCCA"></AlternatingRowStyle>
<RowStyle BackColor="#DEDFDE" ForeColor="Black" CssClass="grid_font" />
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#8EBE12" Font-Bold="False" ForeColor="#E7E7FF" />
<Columns>
<asp:TemplateField ItemStyle-Width="20px">
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True"
oncheckedchanged="chkAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="True"
oncheckedchanged="chkSelect_CheckedChanged" />
</ItemTemplate>
<ItemStyle Width="20px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="EIEUID" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("EIEUid")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle Width="60px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Details" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblemail" runat="server" Text='<%#Eval("Email")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle Width="160px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblmobile" runat="server" Text='<%#Eval("Mobile")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
<ItemStyle Width="120px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Copy & Paste this on .cs Code File:
dataUtility du = new dataUtility();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindGridData();
}
/// This Method is used to bind gridview
private void BindGridData()
{
try
{
DataTable dtnew = new DataTable();
DataTable dt = new DataTable();
dt = du.executeStoteProc("SP_CLIENT 5,'" + Session["Name"] + "','',''");
if (dt.Rows.Count > 0)
{
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
gvDetails.DataSource = dt;
gvDetails.DataBind();
int columncount = gvDetails.Rows[0].Cells.Count;
gvDetails.Rows[0].Cells.Clear();
gvDetails.Rows[0].Cells.Add(new TableCell());
gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
gvDetails.Rows[0].Cells[0].Text = "No Records Found";
}
}
}
catch (Exception ex)
{
}
}
/// enable gridview control to be rendered
public override void VerifyRenderingInServerForm(Control control)
{
/*Verifies that the control is rendered */
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ExportToExcell();
}
/// export gridview to excel
private void ExportToExcell()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Details");
dt.Columns.Add("Mobile");
foreach (GridViewRow row in gvDetails.Rows)
{
CheckBox chkCalls = (CheckBox)row.FindControl("chkSelect");
if (chkCalls.Checked == true)
{
int i = row.RowIndex;
Label lblname = (Label)gvDetails.Rows[i].FindControl("lblname");
Label lblemail = (Label)gvDetails.Rows[i].FindControl("lblemail");
Label lblmobile = (Label)gvDetails.Rows[i].FindControl("lblmobile");
DataRow dr = dt.NewRow();
dr["Name"] = Convert.ToString(lblname.Text);
dr["Details"] = Convert.ToString(lblemail.Text);
dr["Mobile"] = Convert.ToString(lblmobile.Text);
dt.Rows.Add(dr);
}
}
GridView gdvExportxls = new GridView();
gdvExportxls.DataSource = dt;
gdvExportxls.DataBind();
gdvExportxls.HeaderStyle.Font.Bold = true;
gdvExportxls.HeaderStyle.ForeColor = Color.White;
gdvExportxls.HeaderStyle.BackColor = Color.Green;
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("attachment;filename=ReferrerDetails.xls", "selectedrows"));
Response.Charset = "";
StringWriter stringwriter = new StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
gdvExportxls.RenderControl(htmlwriter);
Response.Write(stringwriter.ToString().Replace("<div>", " ").Replace("</div>", " "));
Response.End();
}
/// select/unselect all row using header checkbox
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
try
{
CheckBox chkH = (CheckBox)gvDetails.HeaderRow.FindControl("chkAll");
if (chkH.Checked == true)
{
foreach (GridViewRow gv in gvDetails.Rows)
{
CheckBox chk = (CheckBox)gv.FindControl("chkSelect");
chk.Checked = true;
}
}
else
{
foreach (GridViewRow gv in gvDetails.Rows)
{
CheckBox chk = (CheckBox)gv.FindControl("chkSelect");
chk.Checked = false;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
/// select/unselect single row using row checkbox
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
bool isFound = false;
CheckBox cbxall = (CheckBox)gvDetails.HeaderRow.FindControl("chkAll");
foreach (GridViewRow gvr in gvDetails.Rows)
{
CheckBox chkSelect = gvr.FindControl("chkSelect") as CheckBox;
if (chkSelect.Checked == false)
{
isFound = true;
break;
}
}
if (isFound)
cbxall.Checked = false;
else
cbxall.Checked = true;
}
No comments:
Post a Comment