Results 1 to 4 of 4

Thread: Alright im stumped, Need help with loading CSV (a bit tricky)

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    5

    Alright im stumped, Need help with loading CSV (a bit tricky)

    Alright, so I'm having a tough time trying to accomplish this, and I've spent well over 48 hours trying to get it right and I just cannot. I'll start with saying I am a bit of a newbie. So THANK YOU 100000% in advance for any help.

    OKAY HERE GOES:

    I need to load ONLY one line from my CSV file in to my datagridview. Here is the tricky part. I want to use a filter (search lines by index number) and when the index number is found, randomize which line (based on the index number) will be pulled.

    I have a CSV File that looks like this:
    ' #Index / #Text


    001; Value is 10;
    001; Value is 11;
    001; Value is 12;
    002; Value is 21;
    002; Value is 22;
    002; Value is 22;
    003; Value is 02;
    003; Value is 03;
    003; Value is 04;

    Where as 001-003 is the index number.

    So essentially, If I need to pull from the 001 index, I'll pull one of the three possible lines (Value is 10, Value is 11, Value is 12).


  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Alright im stumped, Need help with loading CSV (a bit tricky)

    There are numerous ways that this could be done. Here's one of them.

    Use a TextFieldParser to read the data from the file into a DataTable. The MSDN documentation for the TextFieldParser class has a code example for reading delimited files and there are examples all over of creating a DataTable. Bind the DataTable to a BindingSource and bind that to the DataGridView.

    Call the Select method of the DataTable to get all the DataRows with a particular Index value into an array. Use a Random object to generate a random index into that array. Get the Text value of the row at that index. Set the Filter of the BindingSource using that Text value, and the Index value too if there may be duplicate Text values, such that all other rows are excluded and only that one row will be displayed in the grid.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    5

    Re: Alright im stumped, Need help with loading CSV (a bit tricky)

    Quote Originally Posted by jmcilhinney View Post
    There are numerous ways that this could be done. Here's one of them.

    Use a TextFieldParser to read the data from the file into a DataTable. The MSDN documentation for the TextFieldParser class has a code example for reading delimited files and there are examples all over of creating a DataTable. Bind the DataTable to a BindingSource and bind that to the DataGridView.

    Call the Select method of the DataTable to get all the DataRows with a particular Index value into an array. Use a Random object to generate a random index into that array. Get the Text value of the row at that index. Set the Filter of the BindingSource using that Text value, and the Index value too if there may be duplicate Text values, such that all other rows are excluded and only that one row will be displayed in the grid.
    Question, do I need a data table or will a data grid work? I have the CSV file loaded in to the DGV but now I need to find out how to filter and randomize. Or I guess take your suggestion (not very familiar with Data Tables)...

    Thanks a ton

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Alright im stumped, Need help with loading CSV (a bit tricky)

    A DataTable is about data storage while a DataGridView is about data display and input. You don't have to use both but they work well together in this context. The DataTable allows you to store all the data in one place at the same time while the DataGridView allows you to display just what you want to right now. Like I said, there are a number of ways to skin this carry but I'd suggest that they is one of the best.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width