我如何合并DataGridView行(具有相等值的单元格) [英] How can i merge DataGridView Rows (Cells with Equal Values)

查看:294
本文介绍了我如何合并DataGridView行(具有相等值的单元格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有像这张照片一样的实体?



Is embodiment available like that picture?

推荐答案

我认为,如果先合并表格然后将Gridview绑定到该表格会更好。
I think, it will be better if you merge tables first and then bind Gridview to that table.


我修改了它的工作原理

(设置DataGridViewCellStyle {WrapMode = True})



I modified and it works
( set DataGridViewCellStyle { WrapMode=True })

void Compare()
       {
           DataTable dt = GetDataTable();
           DataView dv = new DataView(dt);
           String strAtual = String.Empty;
           foreach (DataRowView row in dv)
           {
               if (strAtual.Equals(row.Row["OrderId"].ToString()))
               {
                   foreach (DataColumn dataColumn in dv.Table.Columns)
                   {
                       String columnName = (dataColumn.ColumnName);
                       if (columnName != "OrderId")
                       {
                           string strTagNumb = row[columnName].ToString();
                           CompareDelete(strTagNumb, strAtual, dv, columnName);
                       }
                   }
                   row.Delete();
                   continue;
               }
               if (!string.IsNullOrEmpty(row.Row["OrderId"].ToString()))
                   strAtual = row.Row["OrderId"].ToString();
           }

           dataGridView2.DataSource = dv;
       }

       private void CompareDelete(string strTagNumb, string strAtual, DataView dt, String columnName)
       {
           foreach (DataRowView row in dt)
           {
               if (row.Row["OrderId"].ToString().Equals(strAtual))
               {
                   string nl = Environment.NewLine;
                   row.Row[columnName] += string.Concat(nl, strTagNumb);
                   return;
               }
           }
       }


感谢您提出的好问题。我在上面的回答中为您提供了链接。但是我也试图自己解决这个问题。

以下代码演示了如何合并单元格并保持其不变,即使用户选择合并单元格也是如此。

点击合并按钮后,合并模式将会打开。

这只是 示例(10-15分钟后写) :))可能的解决方案 - 根据需要改进它。



Thanks for the good question. I provided you with links in my answer above. But I've also tried to solve this problem by myself.
The following code demonstrates how you can merge cells and keep it megred even if user selects merged cell.
Merging mode will be turned on after button "Merge" is clicked.
This is just the EXAMPLE(wrote by 10-15 minutes :) ) of possible solution - improve it as you wish.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        DataGridView dataGrid;
        DataSet dataSet;
        Button button;

        private List<string> MergedRowsInFirstColumn = new List<string>();
        private bool readyToMerge = false;


        public Form1()
        {
            InitializeComponent();
            CreateGridAndButton();
        }

        private void CreateGridAndButton()
        {
            dataGrid = new DataGridView();
            dataSet = new DataSet();

            dataGrid.Height = this.Height - 100;
            dataGrid.Dock = DockStyle.Top;
            dataGrid.ReadOnly = true;
            dataGrid.AllowUserToAddRows = false;
            dataGrid.AllowUserToResizeRows = false;
            dataGrid.RowHeadersVisible = false;
            this.dataGrid.Paint += new PaintEventHandler(dataGrid_Paint);

            this.Controls.Add(this.dataGrid);

            button = new Button();
            button.Text = "Merge";
            button.Dock = DockStyle.Bottom;
            button.Click += new System.EventHandler(this.button_Click);

            this.Controls.Add(this.button);

            DataTable tbl_main = new DataTable("tbl_main");

            tbl_main.Columns.Add("Manufacture");
            tbl_main.Columns.Add("Name");
            tbl_main.Columns.Add("CPU");
            tbl_main.Columns.Add("RAM");
            tbl_main.Columns.Add("Price");

            DataRow row;

            row = tbl_main.NewRow();
            row["Manufacture"] = "Dell";
            row["Name"] = "Inspiron 1525";
            row["CPU"] = "T7250";
            row["RAM"] = "2048 MB";
            row["Price"] = "


这篇关于我如何合并DataGridView行(具有相等值的单元格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆