I use C#[2013] winform. I drag and drop a datagridview and design it in two cols then I select value from database to it. I now want to merge two headers into one only. The headers already displayed by designing in properties. I want to merge the columns.
Col1 | Col2
001 | John
I want to merge into one col as
Title
001 | John
How to merge it?
What type of datasource are you binding to... You might just be better to add a new column to a datatable, or property to a class/structure that is a combined value of the two in question. Then just display that one.
If editing is done to any individual field, then update the JOINED column/property as needed.
In the class that you are using as the source for you DataGridView
put a new property:
public string Title { get { return this.Col1 + " | " + this.Col2; }}
then delete those two columns and add one column for Title
Assuming you're talking about a DataGridView, there doesn't appear to be an easy built in-solution for this. Perhaps you could simply copy the data from one column to the other and split them with some sort of delimiter (perhaps a comma).
Modify then run following code after you fill your grid with data:
string data = string.Empty;
int indexCol0 = 0;
int indexCol1 = 1;
foreach (DataGridViewRow row in dataGridView1.Rows)
row.Cells[indexCol0].Value = row.Cells[indexCol0].Value + ", " + row.Cells[indexCol1].Value;
dataGridView1.Columns.RemoveAt(indexCol1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.