【问题标题】:How to highlight a selected row in a gridview?如何突出显示网格视图中的选定行?
【发布时间】:2012-07-30 09:40:37
【问题描述】:

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


    }

我想在选择相应的dropdownlist的任何值时突出显示一行。 我该怎么做?

提前致谢。


我试过了:

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

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

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

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

【问题讨论】:

    标签: c# html.dropdownlistfor


    【解决方案1】:

    你可以试试这个:

    GridView1.Rows[GridView1.SelectedIndex].BackColor = Color.Red 在您的GridView1_SelectedIndexChanged 内?

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

    【讨论】:

    • 谢谢你。我在你回复之前试过了,它正在改变当前行的颜色。 DropDownList ddl = (DropDownList)sender; GridViewRow 行 = (GridViewRow)ddl.NamingContainer; row.BackColor = 颜色.黄色; int abc=row.RowIndex;在 abc 中,我正在获取当前行的索引。请告诉我如何增加它(语法),以便我可以突出显示任何选定下拉列表下方的第三行。
    • 如果abc是当前行的index,那么你可以做GridView1.SelectedIndex + abc然后改变该行的背景颜色。
    【解决方案2】:

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

    【讨论】:

    • 我做了:((row.RowIndex) + 3).BackColor = Color.Black;但它显示错误:错误 1“int”不包含“BackColor”的定义,并且找不到接受“int”类型的第一个参数的扩展方法“BackColor”(您是否缺少 using 指令或程序集引用?
    • 如果你写 (row.RowIndex + 3) 它是一个 int 并且 int 没有 backcolor 属性。你应该写 GridView1.Rows[e.NewSelectedIndex + 3].BackColor = System.Drawing.Color.Black;
    【解决方案3】:

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

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

    感谢支持。

    【讨论】:

      【解决方案4】:

      函数更改行颜色(行){

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

      【讨论】:

        【解决方案5】:
            <!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;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-07
          • 1970-01-01
          • 2015-03-19
          • 1970-01-01
          相关资源
          最近更新 更多