menu

Monday, May 23, 2011

Row Number in GridView


<asp:GridView ID="grd_Select" runat="server" AutoGenerateColumns="False" Width="100%" CssClass="mGrid" >
<Columns>
<asp:TemplateField HeaderText="SNo">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="User_Id" HeaderText="UserID" />
</Columns>
</asp:GridView>

Grid View Row Color on RewDataBound Event


Grid View:

<asp:GridView ID="grd_List" runat="server" AutoGenerateColumns="False" onrowdatabound="grd_List_RowDataBound">
<Columns>
<asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="true">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Product" HeaderText="Product" ReadOnly="true">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
</Columns>
</asp:GridView>

Grid View RewDataBound Event:

protected void grd_DenominationList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
if (drv["Status"].ToString() == "Close")
{
e.Row.BackColor = System.Drawing.Color.Salmon;
}
else if (drv["Status"].ToString() == "Open")
{
e.Row.BackColor = System.Drawing.Color.LightGreen;
}
}

Repeater Row Color on ItemDataBound Event

Repeater:

<asp:Repeater ID="rep_ServiceStatus" runat="server"
onitemcommand="rep_ServiceStatus_ItemCommand"
onitemdatabound="rep_ServiceStatus_ItemDataBound">
<HeaderTemplate>
<div class="container">
<div class="header">Change User Service Status</div>
<table title="Bank Account Information" class="mGrid" cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_grd_BankAccount" style="border:1px solid #525252;width: 100%; background-color: #ffffff; margin-top: 5px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; border-collapse: collapse;">
<tbody>
<tr>
<th scope="col" style="padding-top: 4px; padding-right: 2px; padding-bottom: 4px; padding-left: 2px; color: #ffffff; background-image: initial; background-color: #424242; border-left-style: solid; border-left-width: 1px; border-left-color: #525252; font-size: 10px;">User ID</th>
<th scope="col" style="padding-top: 4px; padding-right: 2px; padding-bottom: 4px; padding-left: 2px; color: #ffffff; background-image: initial; background-color: #424242; border-left-style: solid; border-left-width: 1px; border-left-color: #525252; font-size: 10px;">Service ID</th>
<th scope="col" style="padding-top: 4px; padding-right: 2px; padding-bottom: 4px; padding-left: 2px; color: #ffffff; background-image: initial; background-color: #424242; border-left-style: solid; border-left-width: 1px; border-left-color: #525252; font-size: 10px;">Registration Request Status</th>
<th scope="col" style="padding-top: 4px; padding-right: 2px; padding-bottom: 4px; padding-left: 2px; color: #ffffff; background-image: initial; background-color: #424242; border-left-style: solid; border-left-width: 1px; border-left-color: #525252; font-size: 10px;">Service Status</th>
<th scope="col" style="padding-top: 4px; padding-right: 2px; padding-bottom: 4px; padding-left: 2px; color: #ffffff; background-image: initial; background-color: #424242; border-left-style: solid; border-left-width: 1px; border-left-color: #525252; font-size: 10px;">Change Status</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr id="tr_Service" runat="server">
<td><%#Eval("UserID")%></td>
<td><%#Eval("ServiceID")%></td>
<td><%#Eval("RegistrationRequestStatus")%></td>
<td id="td_ServiceStatus" runat="server"><%#Eval("ServiceStatus")%></td>
<td><asp:LinkButton ID="lnk_ChangeStatus" runat="server" CommandName='<%#Eval("ServiceRegistrationID")%>' CommandArgument='<%#Eval("ServiceStatus")%>'>Change Status</asp:LinkButton></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</div>
</FooterTemplate>
</asp:Repeater>

ItemDataBound Event of Repeater

protected void rep_ServiceStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
foreach (Control c in e.Item.Controls)
{
if (c is System.Web.UI.HtmlControls.HtmlTableRow)
{
HtmlTableRow tr = (HtmlTableRow)c;
foreach (Control c1 in tr.Controls)
{
LinkButton myLink = (LinkButton)c1.FindControl("lnk_ChangeStatus");
if (myLink.CommandArgument.ToString() == "Active")
{
tr.BgColor = "#99FFCC";
}
else if (myLink.CommandArgument.ToString() == "InActive")
{
tr.BgColor = "#FFCCFF";
}
}
}
}
}
}