C#:拆分字符串并将其放入 DataGridView 的第一列
我需要获取文件的第一行并将字符串的单词放入 DataGridView 的第一列中。
我编写了这段代码,其中 csv 文件转换为数组列表:
ArrayList fileList = new ArrayList();
private void button2_Click(object sender, EventArgs e)
{
string line;
// Read the file and display it line by line.
//Read the path from the TextBox
System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text);
stringforData=file.ReadLine(); //this line is because I didn't need //the first
//line of the file
while ((line = file.ReadLine()) != null)
{
// puts elements into array
fileList.Add(line.Split(';'));
}
file.Close();
}
我的文件是这样的:
Name;Surname;Telephone;Address;
george;parado;3434;lili_2;
jennifer;macin;3333;powel_34;
nick;lukas;3322;manchester_44;
我希望 DataGridView 是这样的:
**Subject Type**
Name
Surname
Telephone
Address
所以我需要获取文件的第一行并将其放入第一列一个 DataGridView。
到目前为止我已经做到了这个方法。
ArrayList forData = new ArrayList();
string stringforData;
public void ToDataGrid()
{
DataGridViewColumn newCol = new DataGridViewTextBoxColumn();
forData.Add(stringforData.Split(';'));
}
ToDataGrid 方法必须将名为 forData 的 ArrayList 的元素放入 DataGridView 的第一列。
请帮助!
I need to take the first line of a file and put the words of the string into the first column of a DataGridView.
I have written this code where a csv file is converted to a array list:
ArrayList fileList = new ArrayList();
private void button2_Click(object sender, EventArgs e)
{
string line;
// Read the file and display it line by line.
//Read the path from the TextBox
System.IO.StreamReader file = new System.IO.StreamReader(textBox1.Text);
stringforData=file.ReadLine(); //this line is because I didn't need //the first
//line of the file
while ((line = file.ReadLine()) != null)
{
// puts elements into array
fileList.Add(line.Split(';'));
}
file.Close();
}
My file is like this :
Name;Surname;Telephone;Address;
george;parado;3434;lili_2;
jennifer;macin;3333;powel_34;
nick;lukas;3322;manchester_44;
I want the DataGridView to be like this :
**Subject Type**
Name
Surname
Telephone
Address
So I need to take the first line of the file and put it to the first column of a DataGridView.
As far as now I have made this method.
ArrayList forData = new ArrayList();
string stringforData;
public void ToDataGrid()
{
DataGridViewColumn newCol = new DataGridViewTextBoxColumn();
forData.Add(stringforData.Split(';'));
}
The method ToDataGrid must put the elements of the ArrayList named forData to the first column of the DataGridView.
Please Help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的代码采用分隔字符串,将其拆分,然后将值添加到 datagridview 列。尽管我仍然很难理解你为什么要这样做。
The code below takes a delimited string, splits it and then adds the values to a datagridview column. Though I'm still struggling to see why you want to do this.
我提供这个答案是基于您想要将分隔文件的每一行放入 datagridview 行的假设。如果这实际上不是您想要的,请添加评论。
上面的代码显然只在一行上运行,但您可以很容易地在循环中调用它。如果这样做,请小心不要在该循环内添加列!
这是在网格中显示分隔文件的快速方法,但如果我编写此代码,我会将文件解析为对象模型,然后将这些对象的列表绑定到 datagridview 数据源 - 这将为您提供两个方式数据绑定以及更易于管理的数据处理方式。
即使像我下面展示的一些基本的东西也会更干净:
I'm providing this answer based on the assumption that you want to put each line of the delimited file into a datagridview row. Please add a comment if this isn't actually what you want.
The code above is obviously only working on a single line but you can call this in a loop easily enough. If you do that, be careful to not add the columns inside that loop!
This works as a quick way of displaying a delimited file in a grid but if I was writing this code I'd instead parse the file into an object model and then bind a list of those objects to the datagridview datasource - this will give you two way databinding and a much more managable way of working with this data.
Even something rudimentary like I show below would be cleaner: