views:

43

answers:

2

Hi, I have text file with log information from another program and it has very bad formatting but I can´t edit original program and I want create a new program with dataGridView which read data from .txt file and add it to datagridview. It´s not so hard and I made it with dataGridView1.Rows.Add and array of strings (and prepared columns before). But I don´t use datasource and dataset. I want to add filtering (combobox in header of datagridview, like here but I can´t add it without dataset. So how can I create dataset with just array of strings which has just data? I am trying this code:

DataSet ds = new DataSet();
ds.Tables.Add(TableName);
ds.Tables[TableName].Columns.Add("Datum");
ds.Tables[TableName].Columns.Add("Cas");
ds.Tables[TableName].Columns.Add("Moduly");

Then I read everyline in file and create array with strings and try add that array to row:

ds.Tables[TableName].Rows.Add(PoleRetezcu);

And then just add DataSet do datagridview but it wont work:

dataGridView1.DataSource = ds;

Thank you for help

A: 

I believe this article answers your question with a very detailed explanation. Essentially, the problem you're running into is that when you're passing PoleRetezcu into the table, the table doesn't know what data belongs in which column, so the information in discarded. By matching each of the columns' DataPropertyName to the properties in your custom class, you are loosely binding them, ensuring that the table knows how to store the information coming in.

Ari Patrick
Thank you, I read it and created my class and maybe it could help but first I tried article from Pat and it helps so I don´t need nothing else but thank you :)
Bibo
+1  A: 

You can probably skip the DataSet creation altogether and use the DataGridView's own methods to add the data, as is done in this MSDN example. Particularly, look at step 4, which has the following code:

private void PopulateDataGridView()
{

    string[] row0 = { "11/22/1968", "29", "Revolution 9", 
        "Beatles", "The Beatles [White Album]" };
    string[] row1 = { "1960", "6", "Fools Rush In", 
        "Frank Sinatra", "Nice 'N' Easy" };
    string[] row2 = { "11/11/1971", "1", "One of These Days", 
        "Pink Floyd", "Meddle" };
    string[] row3 = { "1988", "7", "Where Is My Mind?", 
        "Pixies", "Surfer Rosa" };
    string[] row4 = { "5/1981", "9", "Can't Find My Mind", 
        "Cramps", "Psychedelic Jungle" };
    string[] row5 = { "6/10/2003", "13", 
        "Scatterbrain. (As Dead As Leaves.)", 
        "Radiohead", "Hail to the Thief" };
    string[] row6 = { "6/30/1992", "3", "Dress", "P J Harvey", "Dry" };

    songsDataGridView.Rows.Add(row0);
    songsDataGridView.Rows.Add(row1);
    songsDataGridView.Rows.Add(row2);
    songsDataGridView.Rows.Add(row3);
    songsDataGridView.Rows.Add(row4);
    songsDataGridView.Rows.Add(row5);
    songsDataGridView.Rows.Add(row6);

    songsDataGridView.Columns[0].DisplayIndex = 3;
    songsDataGridView.Columns[1].DisplayIndex = 4;
    songsDataGridView.Columns[2].DisplayIndex = 0;
    songsDataGridView.Columns[3].DisplayIndex = 1;
    songsDataGridView.Columns[4].DisplayIndex = 2;
}

(By the way, you can bind a DGV to a DataTable directly - you don't need a DataSet to track it. This article basically shows how to do that.)

Hope that helps. Post your final answer ;-)

Pat
First time I done it like this but for library DataGridViewAutoFilter I need to use DataTable so it was my problem. How to bind DataTable (or DataSet) from text file and then bind it to datagridview. Article which you link helps me. Only problem then was with generating columns so for others dont forget add "dataGridView1.AutoGenerateColumns = true;". Thank you for help
Bibo