开发者

C# : Split String and put it in the first column of a DataGridView

开发者 https://www.devze.com 2023-03-22 02:50 出处:网络
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 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();

    fo开发者_JS百科rData.Add(stringforData.Split(';'));

}

The method ToDataGrid must put the elements of the ArrayList named forData to the first column of the DataGridView.

Please Help!!


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);
}


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;
0

精彩评论

暂无评论...
验证码 换一张
取 消