2

我正在使用模板字段向 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)
    {


    }

I want to highlight a row when any value from the corresponding dropdownlist is selected. 我该怎么做?

提前致谢。


我尝试过这个:

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

但是,当我从任何下拉列表中选择任何值时,它仍然会给出如下异常。

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

如前所述,我正在获取所选行的索引号。我不能从那里增加它并且也可以使用背景颜色属性吗?

4

5 回答 5

1

你可以试试这个:

GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.Red在你的里面GridView1_SelectedIndexChanged

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

于 2012-07-30T10:17:59.590 回答
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;
}
于 2012-07-30T10:20:00.807 回答
0

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

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

感谢您的支持。

于 2012-07-30T11:31:26.400 回答
0

函数更改行颜色(行){

        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);
于 2013-02-07T07:52:51.650 回答
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;
    }
于 2021-02-16T09:09:56.393 回答