列如我表中有这样的数据1   a
1   b
1   c
2   a
2   b我想再页面中显示成这样
----------
    a
1   b
    c
----------
   a
2  b
----------
有其他控件能弄成这样的效果嘛? 但我感觉这种都是拿数据源绑定的格式的灵活度不好控制~

解决方案 »

  1.   

    其实就是跨列显示或者跨行显示的操作了,网上示例多得很。只需要在datarowbound事件做一些判断处理就行了,不需要原始数据特殊处理
      

  2.   


    GridView 纵向合并单元格
      

  3.   

    控制单元格的RowSpan  单元格隐藏
      

  4.   

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = getDataTable();
                GridView1.DataBind();
            }
        }
        public DataTable getDataTable()
        {
            string[] value = { "a", "b", "c", "a", "b" };
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", Type.GetType("System.Int32"));
            dt.Columns.Add("Value", Type.GetType("System.String"));
            for (int i = 0; i < value.Length; i++)
            {
                DataRow row = dt.NewRow();
                if (i > 2)
                    row[0] = 2;
                else
                    row[0] = 1;
                row[1] = value[i];
                dt.Rows.Add(row);
            }
            return dt;
        }
        int row = 0;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int rowindex = e.Row.RowIndex;
            if (rowindex - 1 < 0) return;
            if (e.Row.Cells[0].Text == GridView1.Rows[rowindex - 1].Cells[0].Text)
            {
                if (GridView1.Rows[row].Cells[0].RowSpan == 0) 
                    GridView1.Rows[row].Cells[0].RowSpan++;
                GridView1.Rows[row].Cells[0].RowSpan++;
                e.Row.Cells[0].Visible = false;
            }
            else
                row = rowindex;
        }    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = getDataTable();
                GridView1.DataBind();
            }
        }
        public DataTable getDataTable()
        {
            string[] value = { "a", "b", "c", "a", "b" };
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", Type.GetType("System.Int32"));
            dt.Columns.Add("Value", Type.GetType("System.String"));
            for (int i = 0; i < value.Length; i++)
            {
                DataRow row = dt.NewRow();
                if (i > 2)
                    row[0] = 2;
                else
                    row[0] = 1;
                row[1] = value[i];
                dt.Rows.Add(row);
            }
            return dt;
        }
        int row = 0;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int rowindex = e.Row.RowIndex;
            if (rowindex - 1 < 0) return;
            if (e.Row.Cells[0].Text == GridView1.Rows[rowindex - 1].Cells[0].Text)
            {
                if (GridView1.Rows[row].Cells[0].RowSpan == 0) 
                    GridView1.Rows[row].Cells[0].RowSpan++;
                GridView1.Rows[row].Cells[0].RowSpan++;
                e.Row.Cells[0].Visible = false;
            }
            else
                row = rowindex;
        }
      

  5.   

       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowdatabound="GridView1_RowDataBound">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" />
            <asp:BoundField DataField="Value" HeaderText="Value" />
        </Columns>
        </asp:GridView>
       protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = getDataTable();
                GridView1.DataBind();
            }
        }
        public DataTable getDataTable()
        {
            string[] value = { "a", "b", "c", "a", "b" };
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", Type.GetType("System.Int32"));
            dt.Columns.Add("Value", Type.GetType("System.String"));
            for (int i = 0; i < value.Length; i++)
            {
                DataRow row = dt.NewRow();
                if (i > 2)
                    row[0] = 2;
                else
                    row[0] = 1;
                row[1] = value[i];
                dt.Rows.Add(row);
            }
            return dt;
        }
        int row = 0;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int rowindex = e.Row.RowIndex;
            if (rowindex - 1 < 0) return;
            if (e.Row.Cells[0].Text == GridView1.Rows[rowindex - 1].Cells[0].Text)
            {
                if (GridView1.Rows[row].Cells[0].RowSpan == 0) 
                    GridView1.Rows[row].Cells[0].RowSpan++;
                GridView1.Rows[row].Cells[0].RowSpan++;
                e.Row.Cells[0].Visible = false;
            }
            else
                row = rowindex;
        }