Click here to Skip to main content
14,868,570 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
for example :
Status Column
============
Active
Active
Active
Unactive - This Status is Unactive Then Unactive Column Color change.




So my requirement is, if the Status is Column is Unactive, THE background color of that partciular cell should be changed to red color in GRIDVIEW.
Posted
Updated 20-Jan-14 15:55pm
v2

try like this..

XML
<asp:GridView id="girdview" runat="server" AutoGenerateColumns="false" OnRowDataBound="girdview_RowDataBound">
            <Columns>
                <asp:BoundField DataField="Status" HeaderText="Status" />
                <asp:BoundField DataField="others" HeaderText="others" />
            </Columns>

        </asp:GridView>


C#
protected void girdview_RowDataBound(object sender, GridViewRowEventArgs e)
       {
           int statuscolumnIndex = 0; // check in your gridview
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               string status = DataBinder.Eval(e.Row.DataItem, "Status").ToString();
               if (status == "Unactive")
                   e.Row.Cells[statuscolumnIndex].Style["background-color"] = "Red";

           }
       }
   
v2
Comments
   
My 5. Perfect. :)
Karthik_Mahalingam 20-Jan-14 22:35pm
   
Thanks TD :)
   
Check my solution. You will smile for sure. :):D
Karthik_Mahalingam 20-Jan-14 22:42pm
   
ha ha :)
krishna97 20-Jan-14 22:38pm
   
This code not working
krishna97 20-Jan-14 22:38pm
   
This code not working color not change
Karthik_Mahalingam 20-Jan-14 22:41pm
   
post your gridview code..
krishna97 20-Jan-14 22:45pm
   
<asp:GridView ID="gradevents" runat="server" CssClass="gridview" DataKeyNames="CityId" AutoGenerateColumns="False" Width="100%" AllowPaging="True">
<alternatingrowstyle cssclass="gridview_alter">
<columns>
<asp:TemplateField HeaderText="Select">
<itemtemplate>
<asp:CheckBox ID="chkDel" runat="server" />


<asp:TemplateField HeaderText="S.NO.">
<itemtemplate>
<%#Container.DataItemIndex+1 %>



<asp:TemplateField Visible="false">
<itemtemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("CityId") %>'>
</label>



<asp:TemplateField HeaderText="City Name">
<itemtemplate>
<label>
<%#Eval("CityName") %>
</label>


<edititemtemplate>
<asp:TextBox ID="txtcityname" runat="server" Text='<%#Eval("CityName") %>'>





<asp:TemplateField HeaderText="Status">
<itemtemplate>
<asp:Label ID="lblstat" runat="server" Text='<%#Eval("Status") %>' >



<edititemtemplate>
<asp:DropDownList ID="Dllstatus" runat="server">
<asp:ListItem Value="1">Active
<asp:ListItem Value="2">UnActive




<asp:CommandField HeaderText="Process" ShowDeleteButton="false" ShowEditButton="True" />



<PagerStyle CssClass="gridview_pager"></PagerStyle>

Karthik_Mahalingam 20-Jan-14 22:49pm
   
check my updated solution..
Omprakash Kukana 20-Jan-14 23:44pm
   
my 5++ good job
Karthik_Mahalingam 20-Jan-14 23:57pm
   
Thanks OmPrakash :)
Just to alert you that there is no such word like "Unactive", rather it is "Inactive".

For your issue, you can follow Karthik's Solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900