如何转换并将字符串/字符串值分配给Windows窗体中的列表? [英] How to convert and assign string/String value to list in Windows Forms?
问题描述
我想将字符串值转换为List.How转换为。
这是我的代码。
Hi,
I want to convert string value to List.How to Convert this.
Here is My Code.
private void TestGridData_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = new DataGridViewRow();
List<string> TestName = new List<string>();
int I = TestGridData.CurrentCell.ColumnIndex;
string Head = TestGridData.Columns[I].Name;
if (Head == "check")
{
row = TestGridData.CurrentRow;
int row2 = row.Index;
TestCost = Convert.ToInt32(TestGridData[2, row2].Value);
string Name = TestGridData[1, row2].Value.ToString();
}
// TestName=Name as List<string>;
sum = sum + TestCost;
txtcost.Text = sum.ToString();
}
存储在名称和列表中的字符串值存储在TestName中。如何将此名称转换为List.In这是我试过的注释行,但是没有得到正确的输出。
请帮我输出正确的输出
And the string value stored in Name and list stored in TestName.How to convert this Name to List.In this the commented line i tried but i didn't get correct output.
Please help me to get correct output
推荐答案
单个字符串不是列表(它是一个字符集,每个字符都可以通过索引器访问),因此将单个字符串转换为列表并不是大多数人所做的事情。
您可以通过将字符串添加到现有列表来实现:
A single string isn't a list (it's a Collection of characters, each of which you can access with an indexer) so converting a single string into a list isn't something most people do.
You can do it by adding the string to your existing list:
if (Head == "check")
{
row = TestGridData.CurrentRow;
int row2 = row.Index;
TestCost = Convert.ToInt32(TestGridData[2, row2].Value);
string Name = TestGridData[1, row2].Value.ToString();
TestName.Add(Name)
}
你好,
如果你想添加一个项目在列表然后
Hello ,
If you want to add an item in List then
TestName.Add(Name)
和一件事,你必须< b>将范围列为范围,以便它可以包含不同的项目。
有关List(t).Add方法的更多信息,请按照
列出< t> .Add方法
and one thing , you have to declare the List out side the scope ,so that it can contain different item .
For more information about List(t).Add method follow
List<t>.Add Method
这篇关于如何转换并将字符串/字符串值分配给Windows窗体中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!