Click here to Skip to main content
15,895,829 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hii
i hbave table named employee in sqlserver and there is a column dateof join.i want to display the data in a gridview in asp.net web page.then i have to calculate the duration of the employee. if it will be more than 5 yr then in the grid i m using a radiobutton list having collection of yes and no.
my task is if the duration of employee is more than 5 yr then the user will select yes .then the entiere row will be highlighted.
i m facing problem for highlighting the row in gridview.
pls help me
Posted
Comments
Sarrrva 31-Dec-13 2:42am    
where is your source code? could you please post your code?

Use Following code.

C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        myClass drv = (myClass)e.Row.DataItem;
        if (drv.ShouldHighlight)
            e.Row.CssClass = "highlighted";
    }
}
 
Share this answer
 
Try like this..


ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        Width="100%" onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText=" Candidate name">
                <ItemTemplate>
                    <asp:RadioButtonList ID="rdb" runat="server">
                        <asp:ListItem Text="Yes" />
                        <asp:ListItem Text="No" />
                    </asp:RadioButtonList>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField HeaderText="Date Of Joining" DataField="JoiningDate" />
        </Columns>
    </asp:GridView>




C#
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace POC
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("JoiningDate", typeof(DateTime));
                dt.Rows.Add(DateTime.Now.AddYears(-6));
                dt.Rows.Add(DateTime.Now.AddYears(-4));
                dt.Rows.Add(DateTime.Now.AddYears(-76));
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }



        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                var joiningDate = (DateTime)DataBinder.Eval(e.Row.DataItem, "JoiningDate");
                RadioButtonList rdb = e.Row.FindControl("rdb") as RadioButtonList;
                rdb.SelectedIndex = 1;
                if ((DateTime.Now - joiningDate).TotalDays >= 1825)
                {
                    e.Row.BackColor = System.Drawing.Color.Green;
                    rdb.SelectedIndex = 0;
                }



            }

        }






    }
}
 
Share this answer
 
try this and make changes in coading:-

XML
<asp:GridView ID="GridView1" onrowdatabound="highlightrow" runat="server"
            CellPadding="4" ForeColor="#333333" GridLines="None" >
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>






C#
protected void highlightrow(object sender, GridViewRowEventArgs e)
     {

         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             int years=Convert.ToInt32(e.Row.Cells[2].Text) ;
             if (years > 5)
             {
                 e.Row.BackColor = Color.LightSkyBlue;
             }

         }

     }
 
Share this answer
 
 
Share this answer
 

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