C#:拆分字符串并将其放入 DataGridView 的第一列

发布于 2024-11-25 20:32:06 字数 1336 浏览 0 评论 0原文

我需要获取文件的第一行并将字符串的单词放入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙温凉少女 2024-12-02 20:32:06

下面的代码采用分隔字符串,将其拆分,然后将值添加到 datagridview 列。尽管我仍然很难理解你为什么要这样做。

string csv = "Name,Surname,Telephone,Address";
string[] split = csv.Split(',');

DataGridViewTextBoxColumn subject = new DataGridViewTextBoxColumn();
subject.HeaderText = "Subject Type";
subject.Name = "Subject";

dataGridView1.Columns.Add(subject);

foreach (string item in split)
{
    dataGridView1.Rows.Add(item);
}

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.

string csv = "Name,Surname,Telephone,Address";
string[] split = csv.Split(',');

DataGridViewTextBoxColumn subject = new DataGridViewTextBoxColumn();
subject.HeaderText = "Subject Type";
subject.Name = "Subject";

dataGridView1.Columns.Add(subject);

foreach (string item in split)
{
    dataGridView1.Rows.Add(item);
}
恏ㄋ傷疤忘ㄋ疼 2024-12-02 20:32:06

我提供这个答案是基于您想要将分隔文件的每一行放入 datagridview 行的假设。如果这实际上不是您想要的,请添加评论。

string csv = "John,Doe,21";
string[] split = csv.Split(',');

DataGridViewTextBoxColumn firstName = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn lastName = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn age = new DataGridViewTextBoxColumn();

dataGridView1.Columns.Add(firstName);
dataGridView1.Columns.Add(lastName);
dataGridView1.Columns.Add(age);

dataGridView1.Rows.Add(split);

上面的代码显然只在一行上运行,但您可以很容易地在循环中调用它。如果这样做,请小心不要在该循环内添加列!

这是在网格中显示分隔文件的快速方法,但如果我编写此代码,我会将文件解析为对象模型,然后将这些对象的列表绑定到 datagridview 数据源 - 这将为您提供两个方式数据绑定以及更易于管理的数据处理方式。

即使像我下面展示的一些基本的东西也会更干净:

var users = (from line in File.ReadAllLines(@"C:\mycsv.txt")
let columns = line.Split(',')
select new User()
{
    FirstName = columns[0],
    Surname = columns[1],
    Age = columns[2]
}).ToList();

dataGridView1.DataSource = users;

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.

string csv = "John,Doe,21";
string[] split = csv.Split(',');

DataGridViewTextBoxColumn firstName = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn lastName = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn age = new DataGridViewTextBoxColumn();

dataGridView1.Columns.Add(firstName);
dataGridView1.Columns.Add(lastName);
dataGridView1.Columns.Add(age);

dataGridView1.Rows.Add(split);

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:

var users = (from line in File.ReadAllLines(@"C:\mycsv.txt")
let columns = line.Split(',')
select new User()
{
    FirstName = columns[0],
    Surname = columns[1],
    Age = columns[2]
}).ToList();

dataGridView1.DataSource = users;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文