我从.csv文件获取值并将它们放入datagridview,但我想在填充datagridview之前替换一些值。
这是我的.csv文件的屏幕:
这是我尝试这样做的代码:
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;
请问,如果我的问题不明确,请在告诉我之前加上“-1”,我会尝试更好地解释我的问题。非常感谢你!
答案 0 :(得分:2)
您可以在构建列表时替换文本
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;
答案 1 :(得分:0)
您可以操纵gridview <{1}}事件中的值
您的rowdatabound
必须是上述情景中的列表或datasource
。
DataTable
应该是
dataGridView2.DataSource = mystring;//Wrong