如何在网格视图中突出显示一行 [英] how to highlight a row in grid view

查看:135
本文介绍了如何在网格视图中突出显示一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hii

i hbave表在sqlserver中命名为employee,并且有一个dateof join.i想要在asp.net网页的gridview中显示数据。然后我必须计算持续时间员工。如果它将超过5年,那么在网格中使用带有yes和no集合的radiobutton列表。

我的任务是如果员工的持续时间超过5年,那么用户将选择是的。然后突出显示entiere行。

im面临突出gridview中行的问题。

请帮助我

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

推荐答案

使用以下代码。



Use Following code.

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";
    }
}


试试这样..





Try like this..


<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>













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;
                }



            }

        }






    }
}


试试这个并更改coaded: -



try this and make changes in coading:-

<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>



















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;
             }

         }

     }


这篇关于如何在网格视图中突出显示一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆