6
Answers

Merge Same Data Column in DataGridView windows C#

Photo of ali salamat

ali salamat

12y
17k
1
Hi
I have some data and i want to Merge Same Data Column in grid Like bellow
From :
Id Name ItemToBuy
1 mike Toshiba portage
1 mike Dell Latitude
2 nancy Toshiba portage
2 nancy Sony vioa
3 rosa Asus
3 rosa Hp
4 farid Sony vioa
 //------------------------------------------------------Show in grid like bellow                                                     
Id Name ItemToBuy
1 mike Toshiba portage
Dell Latitude
2 nancy Toshiba portage
Sony vioa
3 rosa Asus
Hp
4 farid Sony vioa

Answers (6)

1
Photo of Vulpes
NA 98.3k 1.5m 12y
Try something like this:

            // set up dgv
            dataGridView1.ColumnCount = 3;
            dataGridView1.Columns[0].HeaderText = "Id";
            dataGridView1.Columns[1].HeaderText = "Name"; 
            dataGridView1.Columns[2].HeaderText = "ItemToBuy";
            dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            dataGridView1.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
           
            string[] data = 
            {
                "1,mike,Toshiba portage",
                "1,mike,Dell latitude",
                "2,nancy,Toshiba portage",
                "2,nancy,Sony vaio",
                "3,rosa,Asus",
                "3,rosa,Hp",
                "4,farid,Sony vaio",
                "4,farid,Hp"
           };

           // analyze data and create rows
           Dictionary<string, int> dict = new Dictionary<string, int>(); 
           int rowNum = 0;
           foreach(string line in data)
           {
               string[] items = line.Split(',');
               
               if (dict.ContainsKey(items[0]))
               {
                   dataGridView1.Rows[dict[items[0]]].Cells[2].Value += "\r\n" + items[2];
               }
               else
               {
                   dict.Add(items[0], rowNum);
                   dataGridView1.Rows.Add(items[0], items[1], items[2]);
                   rowNum++;
               }
           }
0
Photo of ali salamat
NA 23 0 12y
Hi Vulpes thank you for replay but i want really separated row
0
Photo of Vulpes
NA 98.3k 1.5m 12y
You have to set the column's WrapMode property to true to get it to accept multiple lines of text in the first place.

However, if you try the code, you'll see that the text is not actually wrapping because I'm using "\r\n" to force a new line before the next computer name is added for a particular ID.

0
Photo of ali salamat
NA 23 0 12y
0
Photo of ali salamat
NA 23 0 12y
Hi Santhosh Kumar
Thank you but my Problem not solved I dont want grouping plz see
bellow image and link
I want that my same data in two column Id and Name shows as Merge Like Excel
and i dont want to use Hierarchical in my grid
your sample merge data of ItemToBuy in one row
thank you

0
Photo of Santhosh Kumar Jayaraman
NA 9.9k 2.3m 12y
try this

 DataTable dt = new DataTable();
            DataRow dr = dt.NewRow();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("ItemToBuy", typeof(string));
            dr["Id"] = 1 ;
            dr["Name"] = "Mike";
            dr["ItemToBuy"] = "Toshiba portage";
            dt.Rows.Add(dr);
            DataRow dr1 = dt.NewRow();
            dr1["Id"] = 1;
            dr1["Name"] = "Mike";
            dr1["ItemToBuy"] = "Dell Latitude";
            dt.Rows.Add(dr1);
            DataRow dr2 = dt.NewRow();
            dr2["Id"] = 2;
            dr2["Name"] = "nacny";
            dr2["ItemToBuy"] = "Dell Latitude";
            dt.Rows.Add(dr2);
            DataTable dt1 = new DataTable();


            dt1.Columns.Add("Id", typeof(int));
            dt1.Columns.Add("Name", typeof(string));
            dt1.Columns.Add("ItemToBuy", typeof(string));
         
           
            var query = from row in dt.AsEnumerable()
                        group row by row.Field<int>("Id") into grp
                        select new
                        {
                            Id = grp.Key,
                            name=grp.Select(m=>m.Field<String> ("Name")).First(),
                            Joined = string.Join(" ", grp.OrderBy(s => s.Field<int>("Id"))
                                          .Select(s => s.Field<string>("ItemToBuy")))
                        };
            foreach (var grp in query)
            {
                DataRow row = dt1.NewRow();
                row["ID"] = grp.Id;
                row["Name"] = grp.name;
                row["ItemToBuy"] = grp.Joined;
                dt1.Rows.Add(row);
            }
           


Now in your datatable Dt1 you wil have the format as you wanted.. Check the attached solution