问题:如果有数据
星期一 12节  课程1   100
星期一 12节  课程2   99
星期一 34节  课程3   89
星期一 34节  课程4   99
星期一 34节  课程4   89
星期一 34节  课程5   89我想要的数据为
       12   课程1   100
      节   课程2    99
  星       课程3    89
  期  34   课程4    99
  一  节   课程4    89
           课程5    89
在asp.net2.0 vs2005(c#)datagrid里面实现

解决方案 »

  1.   


    int row = 0;
            int row_1 = 0;
            int order;
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (RadioButtonList1.SelectedIndex == 1 && e.Row.Cells[5].Text == "总计")
                {
                    e.Row.Font.Bold = true;
                    //e.Row.ForeColor = System.Drawing.Color.White;
                    e.Row.BackColor = System.Drawing.Color.LightBlue;
                }
                if (e.Row.RowType != DataControlRowType.Footer && e.Row.RowType != DataControlRowType.Header)  //页脚 
                {
                    if (RadioButtonList1.SelectedIndex == 5 )
                    {
                       
                        int rowindex = e.Row.RowIndex;
                        if (rowindex - 1 < 0) 
                        {
                            order = order + 1;
                            e.Row.Cells[0].Text = order.ToString();
                            return;
                        }
                      
                        if (e.Row.Cells[1].Text == GridView1.Rows[rowindex - 1].Cells[1].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;
                            if (GridView1.Rows[row].Cells[1].RowSpan == 0) GridView1.Rows[row].Cells[1].RowSpan++;
                            GridView1.Rows[row].Cells[1].RowSpan++;
                            e.Row.Cells[1].Visible = false;
                            if (GridView1.Rows[row].Cells[2].RowSpan == 0) GridView1.Rows[row].Cells[2].RowSpan++;
                            GridView1.Rows[row].Cells[2].RowSpan++;
                            e.Row.Cells[2].Visible = false;
                            if (GridView1.Rows[row].Cells[3].RowSpan == 0) GridView1.Rows[row].Cells[3].RowSpan++;
                            GridView1.Rows[row].Cells[3].RowSpan++;
                            e.Row.Cells[3].Visible = false;                        if (GridView1.Rows[row].Cells[4].RowSpan == 0) GridView1.Rows[row].Cells[4].RowSpan++;
                            GridView1.Rows[row].Cells[4].RowSpan++;
                            e.Row.Cells[4].Visible = false;
                            
                           
                            if (GridView1.Rows[row].Cells[5].RowSpan == 0) GridView1.Rows[row].Cells[5].RowSpan++;
                            GridView1.Rows[row].Cells[5].RowSpan++;
                            e.Row.Cells[5].Visible = false;
                                 
                            if (GridView1.Rows[row].Cells[6].RowSpan == 0) GridView1.Rows[row].Cells[6].RowSpan++;
                            GridView1.Rows[row].Cells[6].RowSpan++;
                            e.Row.Cells[6].Visible = false;
                            if (GridView1.Rows[row].Cells[7].RowSpan == 0) GridView1.Rows[row].Cells[7].RowSpan++;
                            GridView1.Rows[row].Cells[7].RowSpan++;
                            e.Row.Cells[7].Visible = false;
                            if (GridView1.Rows[row].Cells[8].RowSpan == 0) GridView1.Rows[row].Cells[8].RowSpan++;
                            GridView1.Rows[row].Cells[8].RowSpan++;
                            e.Row.Cells[8].Visible = false;
                            if (GridView1.Rows[row].Cells[9].RowSpan == 0) GridView1.Rows[row].Cells[9].RowSpan++;
                            GridView1.Rows[row].Cells[9].RowSpan++;
                            e.Row.Cells[9].Visible = false;                       
                        }
                        else
                        {
                            row = rowindex;
                            order = order + 1;
                            e.Row.Cells[0].Text = order.ToString();
                        }
                    
                    }
                    else
                    {
                        if (RadioButtonList1.SelectedIndex == 8)
                        {                        int rowindex = e.Row.RowIndex;
                            int rowindex_1 = e.Row.RowIndex;
                            if (rowindex - 1 < 0 && rowindex_1 - 1 < 0)
                            {
                                order = order + 1;
                                e.Row.Cells[0].Text = order.ToString();
                                return;
                            }
                            /*if (GridView1.Rows[rowindex].Cells[1].RowSpan == 0) GridView1.Rows[rowindex].Cells[1].RowSpan++;
                            GridView1.Rows[rowindex].Cells[1].RowSpan++;
                            e.Row.Cells[1].Visible = false;*/
                            if (e.Row.Cells[2].Text == GridView1.Rows[rowindex - 1].Cells[2].Text)
                            {
                                if (GridView1.Rows[row].Cells[2].RowSpan == 0) GridView1.Rows[row].Cells[2].RowSpan++;
                                GridView1.Rows[row].Cells[2].RowSpan++;
                                e.Row.Cells[2].Visible = false;
                                
                            }
                            else
                            {
                                
                                row = rowindex;
                            }
                            if (e.Row.Cells[1].Text == GridView1.Rows[rowindex_1 - 1].Cells[1].Text)
                            {
                               
                                if (GridView1.Rows[row_1].Cells[1].RowSpan == 0) GridView1.Rows[row_1].Cells[1].RowSpan++;
                                GridView1.Rows[row_1].Cells[1].RowSpan++;
                                e.Row.Cells[1].Visible = false;
                            }
                            else
                            {                            row_1 = rowindex_1;
                            }
                            
                                                   
                           
                        }
                        
                        
                        order = order + 1;
                        e.Row.Cells[0].Text = order.ToString();
                    }
                    
                }
                if (e.Row.RowType == DataControlRowType.DataRow)
                {                e.Row.Cells[1].Attributes.Add("class", "text");            }
                
                        }
    这是我去年用C#写的,也是网上看来的

    RadioButtonList1.SelectedIndex == 5
    和RadioButtonList1.SelectedIndex == 8
    这两段,分别是两张不同的报表的单元格合并
      

  2.   

    微软的DataGrid不是很好用,网上有第三方的替代控件,很好用,只要设置一下就可以了
    你可以从网上搜索一下!
    例如:ComponentOne0404
      

  3.   

    2楼的还是看不懂你怎么让他们合并的,好像是GridView1.Rows[row].Cells[5].RowSpan++起的作用,但是能不能先得到“星期一”有几个,然后直接把值赋给GridView1.Rows[row].Cells[5].RowSpan阿,还有GridView1_RowDataBound这个是事件还是自己定义的函数阿?小弟笨 不解,如是事件,何时发生,如果是函数 ,是在从数据库得到datagrid1后调用这个函数呢还是怎么做呢?有没有简单点的办法或将详细点 谢谢各位了。
      

  4.   

    这段代码的意思在dategrid(我用的提gridview)绑定时发生,在每行绑定时,与保存的上一行同一列的数据比较,如果一样就合并