The below code will export the gridview contents to the excel file
protected void Button1_Click(object sender, EventArgs e)
{
Response.AddHeader("content-disposition", "attachment;filename=ExportData.xls");
Response.Charset = String.Empty;
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter strWr = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmWr = new HtmlTextWriter(strWr);
GridView1.RenderControl(htmWr);
Response.Write(strWr.ToString());
Response.End();
}
#Summary in the footer of the gridview
To display the summary in the footer of the gridview , try the following code.
decimal priceTotal = 0;
int quantityTotal = 0;
void detailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// add the UnitPrice and QuantityTotal to the running total variables
priceTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, _
"UnitPrice"));
quantityTotal += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, _
"Quantity"));
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Text = "Totals:";
// for the Footer, display the running totals
e.Row.Cells[1].Text = priceTotal.ToString();
e.Row.Cells[2].Text = quantityTotal.ToString();
e.Row.Cells[1].HorizontalAlign = _
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Font.Bold = true;
}
}
#Confirmation message on button click in GridView
To ask the user for the confirmation message on the button click, try the below code
#Change the back color of a GridView Row based on some condition
To change the gridview row color based on some conditions on record, try the below code
---
---
---
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = (Label)e.Row.Cells[1].FindControl("lblFlagNew");
if (lbl.Text == "True")
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.BackColor = System.Drawing.Color.Black;
}
}
}
#Alternative rows back color in gridview
To display the gridview with alternative rows back color, try the below code
----
----
----
----
you can also use css class , try the below code
----
----
----
----
#Message for empty grid
---
---
---
---
---
#generate row number in the grid
<%# Container.DataItemIndex + 1 %>
No comments:
Post a Comment