首页 新闻 会员 周边

DataGrid数据控件,怎么合并数据啊

0
悬赏园豆:50 [已关闭问题] 关闭于 2013-05-09 10:43

前两列重复的数据合并成一行,后两列把与前两列对应的数据合并到一个单元格里边

问题补充:

这些数据时从存储过程里边直接读出来的,求高手帮忙解答!!!

行水流云的主页 行水流云 | 初学一级 | 园豆:147
提问于:2013-05-03 10:07
< >
分享
所有回答(7)
0

这个sql语句处理吧。

清海扬波 | 园豆:825 (小虾三级) | 2013-05-03 10:16
0

webform?. 如果是的话,需要在数据加载完成后,触发一个事件,处理table 特定td合并,网络上找个jquery 的 合并单元格的插件即可。

邢少 | 园豆:10926 (专家六级) | 2013-05-03 10:19

好像不行啊,,,

支持(0) 反对(0) 行水流云 | 园豆:147 (初学一级) | 2013-05-03 11:30

@行水流云: 不行?.看来实际处理出现了问题。你的这个情况之前实际应用过,方式肯定是可行的。包括楼下的同学的方式。

仔细琢磨一下吧

支持(0) 反对(0) 邢少 | 园豆:10926 (专家六级) | 2013-05-03 11:35
0

我以前写的一个GridView合并看看:

        protected override void AfterDataBound()
        {
            //MergeGroupRow();        // 合并行
            //MergeTableHeader();     // 合并头部两列
      
        }
        /// <summary>
        /// 合并第一列
        /// </summary>
        public void MergeGroupRow()
        {
            //int COL_TYPE_INDEX = 0;
            //int COL_NAME_INDEX = 1;
            //int COL_SHORTNAME_INDEX = 2;
            //int COL_FEATURE_INDEX = 3;

            int rowCount = this.grAnnualTarget.Rows.Count;

            MergeRowsByCol(0, 0, rowCount);    // 从第一行第一列开始合并
        }

        public void MergeRowsByCol(int colIndex, int startRowNum, int rowCount)
        {
            if (rowCount > 1)
            {
                TableCell firstCell = grAnnualTarget.Rows[startRowNum].Cells[colIndex] as TableCell;       // 第一行第一列
                string firstCellText = firstCell.Text;

                for (int i = 1; i < rowCount; i++)
                {
                    TableCell currentCell = grAnnualTarget.Rows[i].Cells[colIndex] as TableCell;

                    if (currentCell.Text.Equals(firstCellText))
                    {
                        if (firstCell.RowSpan == 0) firstCell.RowSpan = 1;
                        firstCell.RowSpan++;
                        firstCell.VerticalAlign = VerticalAlign.Middle;
                        grAnnualTarget.Rows[i].Cells.Remove(currentCell);

                        if (colIndex < 4)
                        {
                            //MergeRowsByCol(colIndex + 1, startRowNum, i + 1);
                        }
                    }
                    else
                    {
                        firstCell = currentCell;
                        firstCellText = firstCell.Text;
                    }

                    startRowNum++;
                }
            }

        }

        private void MergeTableHeader()
        {
            int CELL_TYPE_INDEX = 7;
            int CELL_VALUE_INDEX = 8;

            int totalRowCount = this.grAnnualTarget.Rows.Count;
            if (totalRowCount > 1)
            {
                TableCell typeCell = grAnnualTarget.HeaderRow.Cells[CELL_TYPE_INDEX] as TableCell;       // 第 7列
                TableCell valueCell = grAnnualTarget.HeaderRow.Cells[CELL_VALUE_INDEX] as TableCell;       // 第 8列

                typeCell.ColumnSpan = 2;
                grAnnualTarget.HeaderRow.Cells.Remove(valueCell);
            }
        }
胖子哥哥 | 园豆:341 (菜鸟二级) | 2013-05-03 10:27

貌似也不行。。。

支持(0) 反对(0) 行水流云 | 园豆:147 (初学一级) | 2013-05-03 11:30

@行水流云: 自己修改试试   我这里都可以用的 别直接扔进去

支持(0) 反对(0) 胖子哥哥 | 园豆:341 (菜鸟二级) | 2013-05-03 11:31
0

查查横纵表转换!

鹰击长空123 | 园豆:320 (菜鸟二级) | 2013-05-03 16:07

这是在程序中,,,在程序这边改,,

支持(0) 反对(0) 行水流云 | 园豆:147 (初学一级) | 2013-05-03 16:18
0

可以这样尝试一下,在得到存储过程返回的DataTable之后,先复制它的结构命名为table2,然后遍历table1(存储过程返回),如果id和name在table2里存在,则更新相应的数据,如果不存在,则添加。

祝你好运。

冲动 | 园豆:394 (菜鸟二级) | 2013-05-04 22:03

谢谢你 呵呵 我让高手帮我弄了一下,弄好了,

支持(0) 反对(0) 行水流云 | 园豆:147 (初学一级) | 2013-05-05 09:37
0

在后台 把表拆了,一行一行的加起来实现的 呵呵

行水流云 | 园豆:147 (初学一级) | 2013-05-05 09:42
0

类代码,请做适当修改:

    /// <summary>
    /// DataGrid列的合并(普通列,不包含模板列)
    /// 注意:1.DataGrid在绑定的时候进行分组和排序,才能让相同的行放在一起
    ///       2.方法应用的时机,应该在DataGrid的DataBound事件中使用
    /// </summary>
    /// <param name="dg">需要合并的DataGrid对象</param>
    /// <param name="columnIndex">所要合并列的索引</param>
    public static void UnitCell_T(DataGrid dg, int columnIndex)
    {
        int i = 0;                  //当前行数
        string lastType = string.Empty;        //当前判断是否合并行对应列的值
        int lastCell = 0;           //判断最后一个相同值的行的索引
        if (dg.Items.Count> 0)
        {
            lastType = dg.Items[0].Cells[columnIndex].Text.ToString();
            dg.Items[0].Cells[columnIndex].RowSpan = 1;
            lastCell = 0;
        }
        for (i = 1; i < dg.Items.Count; i++)
        {
            if (dg.Items[i].Cells[columnIndex].Text == lastType)
            {
                dg.Items[i].Cells[columnIndex].Visible = false;
                dg.Items[lastCell].Cells[columnIndex].RowSpan++;
            }
            else
            {
                lastType = dg.Items[i].Cells[columnIndex].Text.ToString();
                lastCell = i;
                dg.Items[i].Cells[columnIndex].RowSpan = 1;
            }
        }
        dg.Columns[columnIndex].ItemStyle.BackColor = System.Drawing.Color.Snow;
        dg.Columns[columnIndex].ItemStyle.ForeColor = System.Drawing.Color.DimGray;

    }

    /// <summary>
    /// DataGrid列的合并(模板列)
    /// </summary>
    /// <param name="gv">需要合并的GridView对象</param>
    /// <param name="columnIndex">所要合并列的索引</param>
    /// <param name="lblName">模板列对象的ToolTip</param>
    public static void UnitCell_T(DataGrid dg, int columnIndex, string lblName)
    {
        int i = 0;                  //当前行数
        string lastType = string.Empty;        //当前判断是否合并行对应列的值
        int lastCell = 0;           //判断最后一个相同值的行的索引
        if (dg.Items.Count > 0)
        {
            lastType = (dg.Items[0].Cells[columnIndex].FindControl(lblName) as Label).ToolTip;
            dg.Items[0].Cells[columnIndex].RowSpan = 1;
            lastCell = 0;
        }
        for (i = 1; i < dg.Items.Count; i++)
        {
            if ((dg.Items[i].Cells[columnIndex].FindControl(lblName) as Label).ToolTip == lastType)
            {
                dg.Items[i].Cells[columnIndex].Visible = false;
                dg.Items[lastCell].Cells[columnIndex].RowSpan++;
            }
            else
            {
                lastType = (dg.Items[i].Cells[columnIndex].FindControl(lblName) as Label).ToolTip.ToString();
                lastCell = i;
                dg.Items[i].Cells[columnIndex].RowSpan = 1;
            }
        }
        dg.Columns[columnIndex].ItemStyle.BackColor = System.Drawing.Color.Snow;
        dg.Columns[columnIndex].ItemStyle.ForeColor = System.Drawing.Color.DimGray;
    }

引用代码

    protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        FN.UnitCell_T(DataGrid1, 3);
        FN.UnitCell_T(DataGrid1, 6);
        FN.UnitCell_T(DataGrid1, 7);
        FN.UnitCell_T(DataGrid1, 8);
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            e.Item.Attributes["onMouseOver"] = "javascript:c=this.style.backgroundColor;this.style.background='#B0E0E6';";  //current 粉蓝色 //--#6699ff 蓝色 #FFFF00 黄色 #FFFFE0 亮黄色
            e.Item.Attributes["onMouseOut"] = "javascript:this.style.background=c;";
        }
    }
爱智旮旯 | 园豆:204 (菜鸟二级) | 2013-05-06 13:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册