为什么列表框中什么都没有显示? C#

时间:2018-11-14 23:04:52

标签: c# listbox

我的程序应该从XML文件中获取球队名称,并将其显示在列表框中,当从列表框中选择球队时,该团队中具有较高击球率的球员将显示在datagridview中。由于某种原因,列表框中没有任何显示,因此我无法选择任何内容并测试程序是否正常运行。

public partial class Form1 : Form
{
    DataSet resultset = new DataSet();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        resultset.ReadXml("Baseball.xml");

        var myQuery = resultset.Tables[0].AsEnumerable().Select(row => new
        {
            teamname = row.Field<string>("Team")
        })
        .Distinct();

        foreach (var rowname in myQuery)
        {
            listBox1.Items.Add(rowname.teamname.ToString());
        }
    }
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        double TotalhitsAverage, TotalatBatAverage, TeamAverage;
        DataTable playerInformation = new DataTable();
        DataRow[] tableName = resultset.Tables[0].Select("Team = '" + listBox1.SelectedItem + "'");

        playerInformation = tableName.CopyToDataTable();
        DataTable clonedcolumns = playerInformation.Clone();

        clonedcolumns.Columns[3].DataType = typeof(double);
        clonedcolumns.Columns[2].DataType = typeof(double);

        foreach (DataRow row in playerInformation.Rows)
        {
            clonedcolumns.ImportRow(row);
        }

        TotalhitsAverage = clonedcolumns.AsEnumerable().Average(r => r.Field<double>("hits"));

        TotalatBatAverage = clonedcolumns.AsEnumerable().Average(r => r.Field<double>("atBats"));

        TeamAverage = TotalhitsAverage / TotalatBatAverage;

        DataTable playerAverage = new DataTable();
        DataRow rowPlayer = playerAverage.NewRow();

        playerAverage.Columns.Add("Player", typeof(String));
        playerAverage.Columns.Add("Batting Avg", typeof(double));

        for (int i = 0; i < clonedcolumns.Rows.Count; i++)
        {
            double averageplayer = Convert.ToDouble(clonedcolumns.Rows[i][3]) / Convert.ToDouble(clonedcolumns.Rows[i][2]);

            if (TeamAverage <= averageplayer)
            {
                playerAverage.Rows.Add(clonedcolumns.Rows[i][0].ToString(), Math.Round(averageplayer, 4));
            }
        }
        DataView view = playerAverage.DefaultView;
        view.Sort = "Batting Avg DESC";
        DataTable sortedDate = view.ToTable();
        dgvBaseball.DataSource = sortedDate;
        dgvBaseball.AutoSize = true;

    }
}

0 个答案:

没有答案