Datagridview列到字符串 [英] Datagridview column to string
问题描述
嗨我对vb.net很新,说出我想做的事情是:
我得到的这个datagridview有一个名为Creditor Details的列看起来像这样
--------------------
|债权人详情|
--------------------
| Absa |
| Absa |
| Capitec |
| FNB |
|标准银行|
----- ---------------
我想要做的是声明1个字符串变量并让列成为字符串,但我想删除重复项例如对于Absa有重复,也在每个债权人详细信息之间我想添加一个\来将它们分隔在字符串变量中我该怎么做?
< b>我尝试过的事情:
我不知道怎么做,任何帮助都会非常感激
Hi I am pretty new to vb.net to word what I want to do is:
I got this datagridview that has a column called "Creditor Details" that look like this
--------------------
| Creditor Details |
--------------------
|Absa |
|Absa |
|Capitec |
|FNB |
|Standard Bank |
--------------------
what i want to do is declare 1 string variable and let the column be the string, but i want to delete the duplicates for instance there's a duplicate for Absa, also between each Creditor Details i want to add a "\" to seperate them in the string variable how could i do this?
What I have tried:
I'm not sure how to do this, any help will be much appreciated
推荐答案
使用 StringBuilder
[ ^ ],并添加依次检查每个单元格的内容。记住要检查重复项。
Use a StringBuilder
[^], and add the contents of each cell in turn, remembering to check for duplicates.
你不应该根据GUI控件的数据工作,而应该使用来自GUI的数据容器中的数据。 。
假设你的网格数据源是一个DataTable,你可以用这样的东西来获取你要求的数据:
You should not working based on data from GUI controls, but rather with the data from the "data containers" which feed the GUI.
Assuming your grid's data source is a DataTable, you can use something like this to get your requested data:
private void buttonTest_Click(object sender, EventArgs e)
{
// Demo Data
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Creditor_Details", typeof(string)));
dt.Rows.Add(new object[] { "Absa"});
dt.Rows.Add(new object[] { "Absa"});
dt.Rows.Add(new object[] { "Capitec"});
dt.Rows.Add(new object[] { "FNB" });
dt.Rows.Add(new object[] { "FNB" });
dt.Rows.Add(new object[] { "Standard Bank" });
dataGridView.DataSource = dt;
// Get Distinct Columns
var result = dt.AsEnumerable()
.GroupBy(gb => gb.Field<string>("Creditor_Details"))
.Select(grp => grp.First()["Creditor_Details"]);
// Join Columns to a single string
var singleString = string.Join("/", result.ToArray());
}</string>
不建议:糟糕的解决方案,万一你真的喜欢/需要从DataGridView获取信息:
NOT SUGGESTED: The bad solution, in case you really like/need to get the info from the DataGridView:
private void buttonTest_Click(object sender, EventArgs e)
{
// NOT SUGGESTED
IEnumerable<datagridviewrow> dgvRows = dataGridView.Rows.Cast<datagridviewrow>();
var resultDGV = dgvRows
.GroupBy(row => row.Cells["Creditor_Details"])
.Select(grp => grp.First().Cells["Creditor_Details"]);
var singleStringDGV = string.Join("/", result.ToArray());
}
我希望它能解决。
I hope it hepls.
这篇关于Datagridview列到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!