How to replace strings in a List

Marci

I got values from a .csv file and put them into a datagridview, but I'd like to replace some values before to fill my datagridview.

This is the screen of my .csv file:

enter image description here

and this is my code to try to do it:

string FileName = @"C:\mydir\testcsv.csv";
    OleDbConnection conn = new OleDbConnection
           ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " +
             Path.GetDirectoryName(FileName) +
             "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");

    conn.Open();

    OleDbDataAdapter adapter = new OleDbDataAdapter
           ("SELECT * FROM " + Path.GetFileName(FileName), conn);

    DataSet ds = new DataSet("Temp");
    adapter.Fill(ds);

    conn.Close();

    DataTable dt = ds.Tables[0];

    List<HT> matchhtList = new List<HT>();
    matchhtList = (from DataRow dr in dt.Rows
                   select new HT()
                   {
                       Home = dr["Home"].ToString(),
                       Away = dr["Away"].ToString(),
                       ScoreHome = dr["ScoreHome"].ToString(),
                       ScoreAway = dr["ScoreAway"].ToString(),
                       Segno = dr["Segno"].ToString(),
                       odd1 = dr["odd1"].ToString(),
                       oddx = dr["oddx"].ToString(),
                       odd2 = dr["odd2"].ToString()

                   }).ToList();

    StringBuilder mystring = new StringBuilder("matchhtList");
    mystring = mystring.Replace("Rosenborg", "Rihanna")
           .Replace("Start", "Stop")
           .Replace("Brann", "Circus");

    dataGridView2.DataSource = mystring;

Please, if my question is not clear, tell me before to put "-1", I'll try to explain better my question. Thank you very much!

Damith

you can replace the the text when you build the list

matchhtList = (from DataRow dr in dt.Rows
                   select new HT()
                   {
                       Home = dr["Home"].ToString().Replace("Rosenborg", "Rihanna"),
                       Away = dr["Away"].ToString().Replace("Start", "Stop").Replace("Brann", "Circus"),
                       ScoreHome = dr["ScoreHome"].ToString(),
                       ScoreAway = dr["ScoreAway"].ToString(),
                       Segno = dr["Segno"].ToString(),
                       odd1 = dr["odd1"].ToString(),
                       oddx = dr["oddx"].ToString(),
                       odd2 = dr["odd2"].ToString()

                   }).ToList();

dataGridView2.DataSource = matchhtList;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to replace a list of strings of a string?

How can I replace parts of strings in a list?

How to replace strings in pandas column that are in a list?

How to replace strings from a list and replace them with integers?

How to replace list of strings in text with items from another list?

How to perform .replace() function on list, if list contains strings, integers and/or floats?

Replace in strings of list

Replace strings in one list with strings in another list

How do I replace a comma separated list of strings with the first occurrence?

How to replace group of strings in pandas series based on a dictionary with values as list?

How to replace string in python from a list of possible strings

How to search and replace strings matching a replacement list for multiple files

How to replace strings in list from a dictionary with all possible combinations

how can I replace elements in a pandas column by a list of strings

How to replace text strings by reading in list using sed

How to replace part of a string from a list of strings using Python 2.7

how do you replace multiple strings in a list using loop for python?

Replace strings using List Comprehensions

Replace strings in list from dictionary

list of strings using replace to modify strings

How to replace certain strings?

How to replace strings in a string

How to replace unknown strings?

How to replace IP Addresses as strings in a list to sort strings in python with checking out the repetitions between IP Addresses

Replace one string list with list of strings in python

How can I use list comprehension to replace a comma with an escaped comma in a list that contains strings and numbers

Replace strings in a file based on a list of strings and a list of corresponding replacements

How to match strings in two files and replace strings?

How to match incorrect strings and replace by correct strings