GridView-合并header与datarow单元格

实现效果:

Id Full Name Depart
1 fname1 lname1 Sales
2 fname2 lname2
3 fname3 lname3
4 fname4 lname4
5 fname5 lname5
6 fname6 lname6 IT
7 fname7 lname7
8 fname8 lname8
9 fname9 lname9
10 fname10 lname10

代码:

using  System;
using  System.Data;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Collections.Generic;

public   partial   class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack) bindPersons();
    }



    
//绑定GridView
    protected void bindPersons()
    
{
        
this.GridView1.DataSource = getSource();
        
this.GridView1.DataBind();
    }


    
//创建数据源
    protected DataTable getSource()
    
{
        
/**-----------------------------------------
         *      Id  FisrtName   LastName    Depart
         * -----------------------------------------
         *       1   fname1       lname1     Sales
         *       1   fname2       lname2     IT
         * -----------------------------------------
         * 
*/

        DataTable persons 
= new DataTable();
        persons.Columns.Add(
"Id"typeof(System.Int32));
        persons.Columns.Add(
"FirstName"typeof(System.String));
        persons.Columns.Add(
"LastName"typeof(System.String));
        persons.Columns.Add(
"Depart"typeof(System.String));

        
for (int i = 0; i < 10; i++)
        
{
            DataRow row 
= persons.NewRow();
            row[
"Id"= i+1;
            row[
"FirstName"= "fname" + (i + 1);
            row[
"LastName"= "lname" + (i + 1);
            
if (i < 5)
                row[
"Depart"= "Sales";
            
else
                row[
"Depart"= "IT";
            persons.Rows.Add(row);
        }

        
return persons;
    }

    
    
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    
{//合并header的firstName和lastName列
        
        
if (e.Row.RowType == DataControlRowType.Header)
        
{
            
//删除lastName列
            e.Row.Cells.RemoveAt(2);
            
//合并列
            e.Row.Cells[1].Attributes["colspan"= "2";
            
//设置列标签
            e.Row.Cells[1].Text = "<font color=\"red\">Full Name</font>";
        }


    }


    
    
protected void GridView1_PreRender(object sender, EventArgs e)
    
{//合并datarow
        
        Dictionary
<string,List<GridViewRow>> rowList = new Dictionary<string,List<GridViewRow>>();
        
foreach (GridViewRow row in GridView1.Rows)
        
{
            
string departName = row.Cells[3].Text;
            
if (!rowList.ContainsKey(departName))
            
{
                rowList[departName] 
= new List<GridViewRow>();
            }

            rowList[departName].Add(row);
        }


        
foreach (KeyValuePair<string, List<GridViewRow>> departList in rowList)
        
{
            
int i = 0;
            
foreach (GridViewRow row in departList.Value)
            
{

                
if (i == 0)
                
{
                    row.Cells[
3].Attributes["rowspan"= departList.Value.Count.ToString();
                    row.Cells[
3].Attributes["valign"= "center";
                    i
++;
                }

                
else
                
{
                    row.Cells.RemoveAt(
3);
                }

            }

        }


    }

}


下载: 文件

你可能感兴趣的:(GridView)