如何突出显示gridview中的选定行?

时间:2012-07-30 09:40:38

标签: c# html.dropdownlistfor

我使用模板字段为gridview添加下拉列表:

 <asp:TemplateField HeaderText="Change Color">
                    <ItemTemplate>
                        <asp:DropDownList ID="dropdownid" DataSourceID="sqldatasource_id" DataTextField="username"
                            BackColor="GrayText"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AppendDataBoundItems="True" runat="server" AutoPostBack="True">

                            <asp:ListItem Text="--Select One--" Value="" Selected="True" />
                        </asp:DropDownList>

SqlDataSource是:

 <asp:SqlDataSource ID="sqldatasource_id" runat="server" ConnectionString="<%$ ConnectionStrings:crudconnection %>"
            SelectCommand="SELECT [username] FROM [crudtable]"></asp:SqlDataSource>

Indexchange-Event如下:

 protected void GridView1_SelectedIndexChanged(object sender,EventArgs e)
    {


    }

我想在选择相应下拉列表中的任何值时突出显示一行。 我该怎么办?

提前致谢。


我试过了:

  GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.Red;

但是,当我从任何下拉列表中选择任何值时,它仍会给出例外情况。

指数超出范围。必须是非负数且小于集合的大小。 参数名称:index

我已经提到了所选行的索引号。我不能从那里增加它并且也可以使用背景属性吗?

5 个答案:

答案 0 :(得分:0)

你可以尝试一下:

GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.Red内的

GridView1_SelectedIndexChanged

应将所选行的背景颜色设置为红色

答案 1 :(得分:0)

尝试GridView_SelectedIndexChanging()。方法如下:

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    //This is current row. Set it to default color
    GridView1.SelectedRow.BackColor = System.Drawing.Color.White;

    //This is selected row. Set it to color you wish
    GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Black;
}

答案 2 :(得分:0)

它的工作。我做了如下改动。

  int abc=row.rowindex+3;
 GridView1.Rows[abc].BackColor = Color.Yellow;

感谢您的支持。

答案 3 :(得分:0)

函数ChangeRowColor(row){

        row = parseInt(row) + 1;

        if (previousRow == row)
            return; //do nothing
        else if (previousRow != null) {
            document.getElementById("MainContent_gvSimulationManager").rows[previousRow].style.backgroundColor = previouscolor;
        }
        previouscolor = document.getElementById("MainContent_gvSimulationManager").rows[row].style.backgroundColor;
        document.getElementById("MainContent_gvSimulationManager").rows[row].style.backgroundColor = "#888888";

        previousRow = row;

        //Disable and enable Session
        var simulationStatus = document.getElementById("MainContent_gvSimulationManager").rows[row].cells[3].innerText;

        EnableAndDisable(simulationStatus);

答案 4 :(得分:0)

    <!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 runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
            OnRowDataBound="GridView1_RowDataBound" OnSelectedIndexChanged = "OnSelectedIndexChanged">
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <Columns>
                        <asp:BoundField DataField="pub_id" HeaderText="pub_id" />
                        <asp:BoundField DataField="pub_name" HeaderText="pub_name" />
                        <asp:BoundField DataField="city" HeaderText="city" />
                        <asp:BoundField DataField="state" HeaderText="state" />
                        <asp:BoundField DataField="country" HeaderText="country" />
                        <asp:ButtonField Text="Click" CommandName="Select" ItemStyle-Width="30"  />
                    </Columns>
           </asp:GridView>
            <br />
            <asp:Label ID="msg" runat="server" Text=""></asp:Label>
        </div>
        </form>
    </body>
    </html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        int i = 0;
        string sql = null;
        string connetionString = "Data Source=.;Initial Catalog=pubs;User ID=sa;Password=*****";
        sql = "select * from publishers";
        SqlConnection connection = new SqlConnection(connetionString);
        connection.Open();
        SqlCommand command = new SqlCommand(sql, connection);
        adapter.SelectCommand = command;
        adapter.Fill(ds);
        adapter.Dispose();
        command.Dispose();
        connection.Close();
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='aquamarine';";
            e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
            e.Row.ToolTip = "Click last column for selecting this row.";
        }
    }
    protected void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        string pName = GridView1.SelectedRow.Cells[1].Text;
        grdCList.SelectedRow.Cells[3].ForeColor = System.Drawing.Color.Red;
    }