英文:
There are more columns in the DataGrid than there are in the csv file
问题
在WPF和C#中,我创建了一个DataGrid,它需要显示一个CSV文件中的8列和190行。但问题是,它显示了5列多余的列,并在最后重复显示了一次最初的8列。为什么会显示13列多余的列?
在这5列中,它插入了以下内容:
Campionato_calcio_2022_23.Campionato_calcio_2022_23_serie_A
以下是代码:
class Campionato_calcio_2022_23_serie_A
{
public String ID { get; set; }
public string Squadra_casa { get; set; }
public string Squadra_fuoric { get; set; }
public string Ris_cas { get; set; }
public string Ris_fuorc { get; set; }
public string segni { get; set; }
public string Data { get; set; }
public string Giornate { get; set; }
public Campionato_calcio_2022_23_serie_A() { }
}
void lFnGenerateData(StreamReader aReader)
{
try
{
bool lBlnIsColumns = true;
string[] lArrCols = null;
List<Campionato_calcio_2022_23_serie_A> lstPersonalList = new List<Campionato_calcio_2022_23_serie_A>();
DataGrid1.Columns.Clear();
while (aReader.Peek() > 0)
{
string lStrLine = aReader.ReadLine();
if (lStrLine == null)
break;
if (lStrLine.Trim() == "")
continue;
string[] lArrStrCells = null;
lArrStrCells = lStrLine.Split(';');
if (lArrStrCells == null)
continue;
if (lBlnIsColumns)
{
lArrCols = lArrStrCells;
foreach (string lStrCell in lArrStrCells)
{
DataGridTextColumn lDGCol = new DataGridTextColumn();
lDGCol.Header = lStrCell;
lDGCol.Binding = new System.Windows.Data.Binding(lStrCell);
DataGrid1.Columns.Add(lDGCol);
}
lBlnIsColumns = false;
continue;
}
if (lArrCols == null)
continue;
int lIntColID = 0;
Campionato_calcio_2022_23_serie_A objCampionato_calcio_2022_23_serie_A = new Campionato_calcio_2022_23_serie_A();
objCampionato_calcio_2022_23_serie_A.ID = lArrStrCells[0];
objCampionato_calcio_2022_23_serie_A.Squadra_casa = lArrStrCells[1];
objCampionato_calcio_2022_23_serie_A.Squadra_fuoric = lArrStrCells[2];
objCampionato_calcio_2022_23_serie_A.Ris_cas = lArrStrCells[3];
objCampionato_calcio_2022_23_serie_A.Ris_fuorc = lArrStrCells[4];
objCampionato_calcio_2022_23_serie_A.segni = lArrStrCells[5];
objCampionato_calcio_2022_23_serie_A.Data = lArrStrCells[6];
objCampionato_calcio_2022_23_serie_A.Giornate = lArrStrCells[7];
lstPersonalList.Add(objCampionato_calcio_2022_23_serie_A);
}
aReader.Close();
DataGrid1.ItemsSource = lstPersonalList;
}
catch (Exception)
{
throw;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
lFnLoadFileData();
}
catch (Exception)
{
throw;
}
}
void lFnLoadFileData()
{
try
{
Microsoft.Win32.OpenFileDialog lObjFileDlge = new Microsoft.Win32.OpenFileDialog();
lObjFileDlge.Filter = "CSV Files|*.csv";
lObjFileDlge.FilterIndex = 1;
lObjFileDlge.Multiselect = false;
string fName = "";
bool? lBlnUserclicked = lObjFileDlge.ShowDialog();
if (lBlnUserclicked != null || lBlnUserclicked == true)
{
fName = lObjFileDlge.FileName;
}
if (System.IO.File.Exists(fName) == true)
{
// FileStream lObjFileStream = lObjFileDlge.File.OpenRead();
StreamReader lObjStreamReader = new StreamReader(fName);
System.Windows.MessageBox.Show(lObjStreamReader.ToString());
lFnGenerateData(lObjStreamReader);
lObjStreamReader.Close();
}
}
catch (Exception)
{
throw;
}
}
我希望它只显示CSV文件中的8列。
英文:
In wpf, c# I created a DataGrid where it has to display 8 columns and 190 rows which are in the csv file. My problem displays me 5 more columns and then repeating at the end the 8 columns that are at the beginning. Why is it showing me 13 more columns?
In the 5 columns he inserts the entry:
Campionato_calcio_2022_23.Campionato_calcio_2022_23_serie_A
This is the code:
class Campionato_calcio_2022_23_serie_A
{
public String ID { get; set; }
public string Squadra_casa { get; set; }
public string Squadra_fuoric { get; set; }
public string Ris_cas { get; set; }
public string Ris_fuorc { get; set; }
public string segni { get; set; }
public string Data { get; set; }
public string Giornate { get; set; }
public Campionato_calcio_2022_23_serie_A() { }
}
void lFnGenerateData(StreamReader aReader)
{
try
{
bool lBlnIsColumns = true;
string[] lArrCols = null;
List<Campionato_calcio_2022_23_serie_A> lstPersonalList = new List<Campionato_calcio_2022_23_serie_A>();
DataGrid1.Columns.Clear();
while (aReader.Peek() > 0)
{
string lStrLine = aReader.ReadLine();
if (lStrLine == null)
break;
if (lStrLine.Trim() == "")
continue;
string[] lArrStrCells = null;
lArrStrCells = lStrLine.Split(';');
if (lArrStrCells == null)
continue;
if (lBlnIsColumns)
{
lArrCols = lArrStrCells;
foreach (string lStrCell in lArrStrCells)
{
DataGridTextColumn lDGCol = new DataGridTextColumn();
lDGCol.Header = lStrCell;
lDGCol.Binding = new System.Windows.Data.Binding(lStrCell);
DataGrid1.Columns.Add(lDGCol);
}
lBlnIsColumns = false;
continue;
}
if (lArrCols == null)
continue;
int lIntColID = 0;
Campionato_calcio_2022_23_serie_A objCampionato_calcio_2022_23_serie_A = new Campionato_calcio_2022_23_serie_A();
objCampionato_calcio_2022_23_serie_A.ID = lArrStrCells[0];
objCampionato_calcio_2022_23_serie_A.Squadra_casa = lArrStrCells[1];
objCampionato_calcio_2022_23_serie_A.Squadra_fuoric = lArrStrCells[2];
objCampionato_calcio_2022_23_serie_A.Ris_cas = lArrStrCells[3];
objCampionato_calcio_2022_23_serie_A.Ris_fuorc = lArrStrCells[4];
objCampionato_calcio_2022_23_serie_A.segni = lArrStrCells[5];
objCampionato_calcio_2022_23_serie_A.Data = lArrStrCells[6];
objCampionato_calcio_2022_23_serie_A.Giornate = lArrStrCells[7];
lstPersonalList.Add(objCampionato_calcio_2022_23_serie_A);
}
aReader.Close();
DataGrid1.ItemsSource = lstPersonalList;
}
catch (Exception)
{
throw;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
{
try
{
lFnLoadFileData();
}
catch (Exception)
{
throw;
}
}
void lFnLoadFileData()
{
try
{
Microsoft.Win32.OpenFileDialog lObjFileDlge = new Microsoft.Win32.OpenFileDialog();
lObjFileDlge.Filter = "CSV Files|*.csv";
lObjFileDlge.FilterIndex = 1;
lObjFileDlge.Multiselect = false;
string fName = "";
bool? lBlnUserclicked = lObjFileDlge.ShowDialog();
if (lBlnUserclicked != null || lBlnUserclicked == true)
{
fName = lObjFileDlge.FileName;
}
if (System.IO.File.Exists(fName) == true)
{
// FileStream lObjFileStream = lObjFileDlge.File.OpenRead();
StreamReader lObjStreamReader = new StreamReader(fName);
System.Windows.MessageBox.Show(lObjStreamReader.ToString());
lFnGenerateData(lObjStreamReader);
lObjStreamReader.Close();
}
}
catch (Exception)
{
throw;
}
}
}
I want it to display me only 8 columns which are in the csv file.
答案1
得分: 1
Here is the translated code:
已解决此代码:
使用 CsvHelper.Configuration;
使用 CsvHelper.TypeConversion;
private void Button_Click(object sender, RoutedEventArgs e)
{
var fileName = @"D:\C_Sharp\WPF\Progetti\Campionati-calcio-2022-23\bin\Debug\net6.0-windows\Camp-calcio-2022-23.csv";
var configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Encoding = Encoding.UTF8,
Delimiter = ",";
};
using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var textReader = new StreamReader(fs, Encoding.UTF8))
using (var csv = new CsvReader(textReader, configuration))
{
var data = csv.GetRecords<OttoColonne>();
foreach (var OttoColonne in data)
{
// 在每行中处理数值
DataGrid1.ItemsSource = data;
}
}
}
}
class OttoColonne
{
public int ID { get; set; }
public string Squadra_casa { get; set; }
public string Squadra_fuoric { get; set; }
public string Ris_cas { get; set; }
public string Ris_fuorc { get; set; }
public string segni { get; set; }
public string Data { get; set; }
public string Giornate { get; set; }
}
Please note that I've retained the code structure and comments in the translation.
英文:
SOLVED with this code:
using CsvHelper.Configuration;
using CsvHelper.TypeConversion;
private void Button_Click(object sender, RoutedEventArgs e)
{
var fileName = @"D:\C_Sharp\WPF\Progetti\Campionati-calcio-2022-23\bin\Debug\net6.0-windows\Camp-calcio-2022-23.csv";
var configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Encoding = Encoding.UTF8,
Delimiter = ","
};
using (var fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (var textReader = new StreamReader(fs, Encoding.UTF8))
using (var csv = new CsvReader(textReader, configuration))
{
var data = csv.GetRecords<OttoColonne>();
foreach (var OttoColonne in data)
{
// Do something with values in each row
DataGrid1.ItemsSource = data;
}
}
}
}
class OttoColonne
{
public int ID { get; set; }
public string Squadra_casa { get; set; }
public string Squadra_fuoric { get; set; }
public string Ris_cas { get; set; }
public string Ris_fuorc { get; set; }
public string segni { get; set; }
public string Data { get; set; }
public string Giornate { get; set; }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论