我就是想选中GridView一列数据。
最好的效果是,点击header下的所有行变色,并且可以传出一个这列的header名。
如 :H1   H2   H3
     1    2    3
    a1   a2   a3  也可以这样,让这个header都是连接。点中后让传出一个这个列名。这个样是不是间的一点,或者,每一个header的名字下面有一个单选按钮,点击单选按钮执行一个方法,也可以,我主要是不知道怎么动态的在header中添加一个控件。不过我的GridView的数据都是自动生成列的,这点要注意,

解决方案 »

  1.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="DataList" %><!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>
        <script>
        function changeBgColor(x)
        {
            var table=document.getElementById('<%=gv.ClientID %>');
            for(var i=1;i<table.rows.length;i++)
            {        
                for(var j=0;j<table.rows[i].cells.length;j++)
                {
                    if(j==x)
                        table.rows[i].cells[x].style.backgroundColor=table.rows[i].cells[x].style.backgroundColor==''?'red':'';
                    else
                        table.rows[i].cells[j].style.backgroundColor='';
                }
            }
            //alert(table.rows[0].cells[x].innerText);
            document.getElementById('txt').value=table.rows[0].cells[x].innerText;
        }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound">
        </asp:GridView>
        <input type="text" id="txt" />
        </form>
    </body>
    </html>
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class DataList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {        gv.DataSource = getDateTable();
            gv.DataBind();    }
        private DataTable getDateTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
            dt.Columns.Add(new DataColumn("Name", typeof(String)));
            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "消";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = 2;
            dr[1] = "里";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = 3;
            dr[1] = "网";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = 4;
            dr[1] = "涨";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = 5;
            dr[1] = "刘";
            dt.Rows.Add(dr);
            return dt;
        }
        
        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    e.Row.Cells[i].Attributes.Add("onclick", "changeBgColor('" + i.ToString() + "');");
                }
            }
        }
    }
      

  2.   

    这些问题真不知道怎么给你说
    你绑定gridview不要自动生成列,最好手动添加列,容易控制
    如果想操作某一列的话,你可以添加一个模板列,里面在放上需要的控件
      强烈建议lz看看gridview的基本操作
      

  3.   

    我也想编辑模板,可是,首先我的GridView的列不能确定,其次是我发现编辑模板的时候是编辑了一列的数据,我只想给头加东西。
      

  4.   

    Hi Sandy945   万分谢谢你, 你写的非常好。不知道这header能不能是个超链接一样的东西,这样能使用户知道点header了。
      

  5.   

    Sandy945 如果可以的话给点提示吧! 不胜感激。
      

  6.   


        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    e.Row.Cells[i].Text = "<a  href=\"javascript:changeBgColor('" + i.ToString() + "');\">" + e.Row.Cells[i].Text + "</a>";                    
                        
                }
            }
        }
    刚才下班有点忙,没看到。现在下班回家了,有问题你在这问,我看到会回复的。
      

  7.   

    Sandy945  十分感谢,不是万分感谢。