Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I have a datagridview with three columns where, everytime I only have one value, I want to merge my columns, similarly to the picture below : enter image description here

I have made sure to have my three column values for each row split by '-'. In that case, the datagridview takes those values from my database and splits them before adding them to their respective columns. Here's a chunk of my code :

C#
String[] splitter = new string[3];
try
{
string a = datagridview2. Rows[row. Index]. Cells[$"{combobox1. Text + combobox2. Text}datagridviewtextboxcolumn"]. Value == null ? String. Empty : datagridview2. Rows[row. Index]. Cells[$"{combobox1. Text + combobox2. Text}datagridviewtextboxcolumn"]. Value. Tostring();

if (a. Contains('-'))
{
splitter = a. Split('-');
row. Cells["tolerancemindatagridviewtextboxcolumn"]. Value = splitter[0];
row. Cells["tolerancenomdatagridviewtextboxcolumn"]. Value = splitter[1];
row. Cells["tolerancemaxdatagridviewtextboxcolumn"]. Value = splitter[2];

}
else
{
row. Cells["tolerancemindatagridviewtextboxcolumn"]. Value = a;

//code pour merge
}

}
catch { exception exception; }


What I have tried:

I've tried playing around with the style methods but none of them seem to work. Does anyone have any idea on how to do it?
Posted
Updated 20-Jun-23 6:08am

1 solution

See here: How to Merge Cells in DataGridView | 10Tec[^]

And do yourself two favours:
1) Indent your code! It's a whole load more readable for everybody if you do, and VS will auto indent it to your preferred style for you.
2) Don't swallow exceptions. At the very least, log them to the debug console so you know they have occurred and get some info to help you fix them:
C#
catch (Exception ex)
    {
    Debug.WriteLine(ex.ToString());
    }
Swallowing exceptions just means you have no idea what is goin on in your code, or even that it is wrong - like turning up the volume in a car to cover the noise when a wheel falls off doesn't mean that the car is fixed.
 
Share this answer
 
Comments
LiterallyGutsFromBerserk 21-Jun-23 3:11am    
Thank you for the link and the tips. I'm still new to sites like codeproject and stack overflow, so I'm still trying to figure out the text formatting. As for the link, it's the solution to my problem, so thank you for saving me so much time.Finally, I did indeed use the catch exception the way one would turn up the volume of a car when wheels fall off. So thank you for explaining that part(I wasn't sure what the "try catch " were used for exactly).
OriginalGriff 21-Jun-23 3:45am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900