0

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!

5
  • You likely had the downvote due to "but its not working". What precisely isn;t working? is there an exception etc. This is the detail thats missing. Commented Nov 11, 2016 at 15:39
  • As it happens, you're setting your datasource to a string - not your actual data.... Commented Nov 11, 2016 at 15:40
  • ok, I made a mistake, but please don't give me a downvote, just for this time. I deleted the wrong question... Commented Nov 11, 2016 at 15:49
  • I didn;t downvote - not my style :) just giving reasons why you might have been. Commented Nov 11, 2016 at 15:50
  • Thank you, I will be careful next time :-) Commented Nov 11, 2016 at 15:52

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

0

you can manipulate the values in rowdatabound event of gridview

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.110).aspx

And your datasource must be a List or DataTable in above scenario.

dataGridView2.DataSource = mystring;//Wrong

should be

dataGridView2.DataSource = matchList;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.