如何更改GridView的当前所选行颜色 [英] How to change current selected row color of GridView

查看:288
本文介绍了如何更改GridView的当前所选行颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我选择GridView行时,当前行会突出显示,而前一行会显示原始颜色。这该怎么做?使用asp.net c#代码。并且在回发后该行保持其颜色。

when I select a GridView row then,current row get highlighted, and previous row displayed with original color. how to do this? using asp.net c# code behind. and after postback that row persist its colors.

推荐答案

参考: ASP.Net GridView突出显示行:更改CheckBox Checked和MouseOver上的行颜色 [ ^ ]


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Demo.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
   </head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="MessageLabel" ForeColor="Red" runat="server" />
    <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"

        runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"



        onrowcreated="GridView1_RowCreated">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
            <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
            <asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center" HeaderStyle-Width="50">
                <ItemTemplate>
                    <asp:Button ID="btnView" runat="server" Text="View" CommandName="select" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <SelectedRowStyle BackColor="Red" />
    </asp:GridView>
    </form>
</body>
</html>





C#代码





C# Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Demo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
                dt.Rows.Add(1, "John Hammond", "USA");
                dt.Rows.Add(2, "Mudassar Khan", "India");
                dt.Rows.Add(3, "Suzanne Mathews", "France");
                dt.Rows.Add(4, "Robert Schidner", "UK");
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = 0;
            if (e.CommandName == "Select")
            {
                index = Convert.ToInt32(e.CommandArgument.ToString());
                GridViewRow row = GridView1.Rows[index];
                MessageLabel.Text = row.Cells[1].Text;
            }
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                e.Row.ToolTip = "Click to select row";
                e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select


+ e.Row.RowIndex);
}

}
受保护 覆盖 void 渲染(HtmlTextWriter编写器)
{
for int i = 0 ; i < GridView1.Rows.Count; i ++ )
ClientScript.RegisterForEventValidation(GridView1.UniqueID, 选择
" + e.Row.RowIndex); } } protected override void Render(HtmlTextWriter writer) { for (int i = 0; i < GridView1.Rows.Count; i++) ClientScript.RegisterForEventValidation(GridView1.UniqueID, "Select


这篇关于如何更改GridView的当前所选行颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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