datagridview单元格拆分

时间:2021-02-10 16:20:35
不想用第三方控件

c# winform下如何实现datagridview单元格拆分,并指定单元格绑定数据,最好有实例代码  

注:c# winform

11 个解决方案

#1


拆分很麻烦的,最好自己绘制单元格,

#2


重写这个方法自己画,

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)

#3


http://download.csdn.net/detail/ireenter/3407133

#4


有人知道怎么弄吗??

#6


如上图的效果代码没有的   

#8


不是上图效果显示的代码

#9


运用 

 
int[] 列索引集合 
 

将第1列的单元格格一样的数据合并..

GroupRows(GridView1,new int[]{0});  


全部代码

 
/// <summary>
        /// GridView合并行 zgke@sina.com qq:116149
        /// </summary>
        /// <param name="p_GridView">Grid控件</param>
        /// <param name="p_ColumnsIndex">列索引</param>
        public static void GroupRows(GridView p_GridView, int[] p_ColumnsIndex)
        {
            int _Count = p_GridView.Rows.Count;
            string[] _TempText = new string[p_ColumnsIndex.Length];
            int[] _RowIndex = new int[p_ColumnsIndex.Length];
            for (int i = 0; i != _Count; i++)
            {
                string _CellText = "";
                for (int z = 0; z != p_ColumnsIndex.Length; z++)

{
                    _CellText += p_GridView.Rows[i].Cells[p_ColumnsIndex[z]].Text;
                    if (_TempText[z] == _CellText)
                    {
                        p_GridView.Rows[i].Cells[p_ColumnsIndex[z]].Visible = false;
                        p_GridView.Rows[_RowIndex[z]].Cells[p_ColumnsIndex[z]].RowSpan++;
                    }
                    else
                    {
                        _RowIndex[z] = i;
                        _TempText[z] = _CellText;
p_GridView.Rows[_RowIndex[z]].Cells[p_ColumnsIndex[z]].RowSpan = 1;
                    }
                }
            }
        }



-------------------------------------------------------另一种
///      
    ///   合并GridView列中相同的行   
    ///      
    ///   GridView对象   
    ///   需要合并的列   
    public static void GroupRows(GridView GridView1, int cellNum)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridView1.Rows.Count - 1)
        {
            GridViewRow gvr = GridView1.Rows[i];
            for (++i; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridView1.Rows[i];
                if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                } 
                if (i == GridView1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    } 

    ///      
    ///   根据条件列合并GridView列中相同的行   
    ///      
    ///   GridView对象   
    ///   需要合并的列
    ///   ///   条件列(根据某条件列还合并)
    public static void GroupRows(GridView GridView1, int cellNum, int cellNum2)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridView1.Rows.Count - 1)
        {
            GridViewRow gvr = GridView1.Rows[i];
            for (++i; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridView1.Rows[i];
                if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                } 

                if (i == GridView1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    } 

  

第二个方法只是在第一个方法的修改了一点点, 在判断单元格的时候,加上了一个条件列:

if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)。当然,第二个方法还可以扩展,根据多个列的条件来合并。

-----------------------------------------------------------------
/// <summary>
    /// 合并单元格
    /// </summary>
    /// <param name="gridView">要合并单元格的表单</param>
    /// <param name="ArryJudgeCell">判断的列</param>
    /// <param name="ArryCellNum">要合并的列</param>
    public static void DoUniteGroupRowsGrid(GridView gridView, int[] ArryJudgeCell, int[] ArryCellNum)
    {
        int i = 0, rowSpanNum = 1;

        bool flagSame = true;

        while (i < gridView.Rows.Count - 1)
        {
            // 得到当前行
            GridViewRow gvrThis = gridView.Rows[i];

            for (++i; i < gridView.Rows.Count; i++)
            {
                GridViewRow gvrNext = gridView.Rows[i];

                for (int j = 0; j < ArryJudgeCell.Length; j++)
                {
                    flagSame = true;

                    if (gvrThis.Cells[ArryJudgeCell[j]].Text != gvrNext.Cells[ArryJudgeCell[j]].Text)
                    {
                        flagSame = false;

                        break;
                    }
                }

                if (flagSame)
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrNext.Cells[ArryCellNum[k]].Visible = false;
                    }

                    rowSpanNum++;
                }
                else
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrThis.Cells[ArryCellNum[k]].RowSpan = rowSpanNum;
                    }
                    rowSpanNum = 1;
                    break;
                }

                if (i == gridView.Rows.Count - 1)
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrThis.Cells[ArryCellNum[k]].RowSpan = rowSpanNum;
                    }
                }
            }
        }
    }

#10


什么没有RowSpan属性

#11


为什么没有RowSpan属性??

#1


拆分很麻烦的,最好自己绘制单元格,

#2


重写这个方法自己画,

protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)

#3


http://download.csdn.net/detail/ireenter/3407133

#4


有人知道怎么弄吗??

#5


#6


如上图的效果代码没有的   

#7


#8


不是上图效果显示的代码

#9


运用 

 
int[] 列索引集合 
 

将第1列的单元格格一样的数据合并..

GroupRows(GridView1,new int[]{0});  


全部代码

 
/// <summary>
        /// GridView合并行 zgke@sina.com qq:116149
        /// </summary>
        /// <param name="p_GridView">Grid控件</param>
        /// <param name="p_ColumnsIndex">列索引</param>
        public static void GroupRows(GridView p_GridView, int[] p_ColumnsIndex)
        {
            int _Count = p_GridView.Rows.Count;
            string[] _TempText = new string[p_ColumnsIndex.Length];
            int[] _RowIndex = new int[p_ColumnsIndex.Length];
            for (int i = 0; i != _Count; i++)
            {
                string _CellText = "";
                for (int z = 0; z != p_ColumnsIndex.Length; z++)

{
                    _CellText += p_GridView.Rows[i].Cells[p_ColumnsIndex[z]].Text;
                    if (_TempText[z] == _CellText)
                    {
                        p_GridView.Rows[i].Cells[p_ColumnsIndex[z]].Visible = false;
                        p_GridView.Rows[_RowIndex[z]].Cells[p_ColumnsIndex[z]].RowSpan++;
                    }
                    else
                    {
                        _RowIndex[z] = i;
                        _TempText[z] = _CellText;
p_GridView.Rows[_RowIndex[z]].Cells[p_ColumnsIndex[z]].RowSpan = 1;
                    }
                }
            }
        }



-------------------------------------------------------另一种
///      
    ///   合并GridView列中相同的行   
    ///      
    ///   GridView对象   
    ///   需要合并的列   
    public static void GroupRows(GridView GridView1, int cellNum)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridView1.Rows.Count - 1)
        {
            GridViewRow gvr = GridView1.Rows[i];
            for (++i; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridView1.Rows[i];
                if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                } 
                if (i == GridView1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    } 

    ///      
    ///   根据条件列合并GridView列中相同的行   
    ///      
    ///   GridView对象   
    ///   需要合并的列
    ///   ///   条件列(根据某条件列还合并)
    public static void GroupRows(GridView GridView1, int cellNum, int cellNum2)
    {
        int i = 0, rowSpanNum = 1;
        while (i < GridView1.Rows.Count - 1)
        {
            GridViewRow gvr = GridView1.Rows[i];
            for (++i; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gvrNext = GridView1.Rows[i];
                if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)
                {
                    gvrNext.Cells[cellNum].Visible = false;
                    rowSpanNum++;
                }
                else
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                    rowSpanNum = 1;
                    break;
                } 

                if (i == GridView1.Rows.Count - 1)
                {
                    gvr.Cells[cellNum].RowSpan = rowSpanNum;
                }
            }
        }
    } 

  

第二个方法只是在第一个方法的修改了一点点, 在判断单元格的时候,加上了一个条件列:

if (gvr.Cells[cellNum].Text + gvr.Cells[cellNum2].Text == gvrNext.Cells[cellNum].Text + gvrNext.Cells[cellNum2].Text)。当然,第二个方法还可以扩展,根据多个列的条件来合并。

-----------------------------------------------------------------
/// <summary>
    /// 合并单元格
    /// </summary>
    /// <param name="gridView">要合并单元格的表单</param>
    /// <param name="ArryJudgeCell">判断的列</param>
    /// <param name="ArryCellNum">要合并的列</param>
    public static void DoUniteGroupRowsGrid(GridView gridView, int[] ArryJudgeCell, int[] ArryCellNum)
    {
        int i = 0, rowSpanNum = 1;

        bool flagSame = true;

        while (i < gridView.Rows.Count - 1)
        {
            // 得到当前行
            GridViewRow gvrThis = gridView.Rows[i];

            for (++i; i < gridView.Rows.Count; i++)
            {
                GridViewRow gvrNext = gridView.Rows[i];

                for (int j = 0; j < ArryJudgeCell.Length; j++)
                {
                    flagSame = true;

                    if (gvrThis.Cells[ArryJudgeCell[j]].Text != gvrNext.Cells[ArryJudgeCell[j]].Text)
                    {
                        flagSame = false;

                        break;
                    }
                }

                if (flagSame)
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrNext.Cells[ArryCellNum[k]].Visible = false;
                    }

                    rowSpanNum++;
                }
                else
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrThis.Cells[ArryCellNum[k]].RowSpan = rowSpanNum;
                    }
                    rowSpanNum = 1;
                    break;
                }

                if (i == gridView.Rows.Count - 1)
                {
                    for (int k = 0; k < ArryCellNum.Length; k++)
                    {
                        gvrThis.Cells[ArryCellNum[k]].RowSpan = rowSpanNum;
                    }
                }
            }
        }
    }

#10


什么没有RowSpan属性

#11


为什么没有RowSpan属性??