在C#窗口应用程序中合并datagridview的单元格 [英] Merge cells of datagridview in C# window application
问题描述
嗨..
我有三个文本框,分别输入名字,中间名和姓氏,然后单击保存和编辑按钮.将其保存到db后,我从数据库中检索并绑定到datagridview.我想将这三列合并为一个.当我单击编辑"按钮...时,应在相应的文本框中填写名字,中间名和姓氏.
我该如何实现.
Hi..
I have three textboxes where i enter first name, middle name and last name and save and edit button. After i save that into db,i retrive from the database and bind to datagridview. I want to merge those three columns as one. And when i click edit button....first,middle and last names should be filled in the corresponding textboxes.
How can i achieve this.
推荐答案
我不知道您使用的是哪种数据库,但是您不能做类似的事情
I don''t know what kind of database you use, but can''t you do something like
"SELECT (Firstname + Middlename + Lastname) AS fullname from [... etc]"
这样一来,您将直接从数据源中将所有3列接收到一列中.
然后,当您单击编辑按钮时,只需再次选择单独的数据,进入文本框即可.
That way you''ll receive all 3 columns into one column directly from the datasource.
Then when you click the edit button just select the data separate again, into the textboxes
实际上,我正在尝试学习如何合并datagridview列. 我知道您提出的方法,谢谢.
Actually i am trying to learn how we can merge datagridview columns.
I know your proposed method, thank you.
好的,如果您真的想合并"它们,可以这样做
Oke, if you really want to "merge them" you could do
private void DataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (DataGridViewRow row in DataGridView.Rows)
{
row.Cells["Firstname"].value += row.Cells["Middlename"].value.ToString() + row.Cells["Lastname"].value.ToString();
}
DataGridViewRow.Columns["Middlename"].Visible = false;
DataGridViewRow.Columns["Lastname"].Visible = false;
}
这篇关于在C#窗口应用程序中合并datagridview的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!