数据表格中的列数比CSV文件中的列数多。

huangapple go评论136阅读模式
英文:

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&lt;Campionato_calcio_2022_23_serie_A&gt; lstPersonalList = new List&lt;Campionato_calcio_2022_23_serie_A&gt;();
DataGrid1.Columns.Clear();
while (aReader.Peek() &gt; 0)
{
string lStrLine = aReader.ReadLine();
if (lStrLine == null)
break;
if (lStrLine.Trim() == &quot;&quot;)
continue;
string[] lArrStrCells = null;
lArrStrCells = lStrLine.Split(&#39;;&#39;);
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 = &quot;CSV Files|*.csv&quot;;
lObjFileDlge.FilterIndex = 1;
lObjFileDlge.Multiselect = false;
string fName = &quot;&quot;;
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 = @&quot;D:\C_Sharp\WPF\Progetti\Campionati-calcio-2022-23\bin\Debug\net6.0-windows\Camp-calcio-2022-23.csv&quot;;
var configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
{
Encoding = Encoding.UTF8,
Delimiter = &quot;,&quot;
};
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&lt;OttoColonne&gt;();
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; } 
}

huangapple
  • 本文由 发表于 2023年6月12日 21:54:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定