The following tips will be very useful while creating gridview
#Export Gridview to xls
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
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDel" runat="server" Text="Delete"
CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete?');" />
</ItemTemplate>
</asp:TemplateField>
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
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDel" runat="server" Text="Delete"
CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete?');" />
</ItemTemplate>
</asp:TemplateField>
#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
<asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server" Width="600" OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblFlagNew" runat="server" Text='<%# Eval("FlagNew") %>'></asp:Label>
---
---
---
</asp:gridview>
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
<asp:GridView ID="GridView1" AlternatingRowStyle-BackColor="yellow" RowStyle-BackColor="blue" runat="server">
----
----
----
----
</asp:GridView>
you can also use css class , try the below code
<asp:GridView ID="GridView1" AlternatingRowStyle-CssClass="row1" RowStyle-CssClass="row2" runat="server">
----
----
----
----
</asp:GridView>
#Message for empty grid
<asp:GridView ID="GridView1" EmptyDataText="No records found" runat="server">
---
---
---
---
---
</asp:GridView>
#generate row number in the grid
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
No comments:
Post a Comment