Why is nothing displaying in my listbox? C#










0















My program is supposed to take team names from an XML file and display them in the listbox, and when a team is selected from the listbox, players with a higher batting average on the team are displayed in a datagridview. For some reason nothing is showing up in my listbox, and therefore I cannot select anything and test if my program works.



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;












share|improve this question






















  • Does your dataset get populated?

    – Crowcoder
    Nov 14 '18 at 23:20






  • 5





    This is a classic case of writing a bunch of code, pressing the big go button and wondering why it doesn’t work. You ARE now a programmer, we don’t do this. You NEED to debug your code so YOU can identify at each point in code what is going on (after all you wrote it), YOU need inspect each line while it is running to determine if it has YOUR expected values, and if you can’t figure it out then you come to stack overflow with the results of those tests, the line that is not working, the expected results and the reasoning. You have none of this

    – Michael Randall
    Nov 14 '18 at 23:41







  • 1





    This comment isn't sarcastic - it's for real. Click on the margin to the left of the line of code after resultset.ReadXml. You'll see a dot appear. It's called a breakpoint. Run the program again. When the program gets to that line it will stop. Move your mouse over the word resultset and Visual Studio will let you see what's in resultset, or more specifically, resultset.Tables[0]. My guess is nothing. So this really has nothing to do with a ListBox.

    – Scott Hannen
    Nov 15 '18 at 0:28











  • @TheGeneral Okay, I will make sure to gather all this information before asking here. My apologies.

    – dw0802
    Nov 15 '18 at 0:52






  • 1





    Navigating through Code using the Step Debugger

    – None of the Above
    Nov 15 '18 at 1:15















0















My program is supposed to take team names from an XML file and display them in the listbox, and when a team is selected from the listbox, players with a higher batting average on the team are displayed in a datagridview. For some reason nothing is showing up in my listbox, and therefore I cannot select anything and test if my program works.



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;












share|improve this question






















  • Does your dataset get populated?

    – Crowcoder
    Nov 14 '18 at 23:20






  • 5





    This is a classic case of writing a bunch of code, pressing the big go button and wondering why it doesn’t work. You ARE now a programmer, we don’t do this. You NEED to debug your code so YOU can identify at each point in code what is going on (after all you wrote it), YOU need inspect each line while it is running to determine if it has YOUR expected values, and if you can’t figure it out then you come to stack overflow with the results of those tests, the line that is not working, the expected results and the reasoning. You have none of this

    – Michael Randall
    Nov 14 '18 at 23:41







  • 1





    This comment isn't sarcastic - it's for real. Click on the margin to the left of the line of code after resultset.ReadXml. You'll see a dot appear. It's called a breakpoint. Run the program again. When the program gets to that line it will stop. Move your mouse over the word resultset and Visual Studio will let you see what's in resultset, or more specifically, resultset.Tables[0]. My guess is nothing. So this really has nothing to do with a ListBox.

    – Scott Hannen
    Nov 15 '18 at 0:28











  • @TheGeneral Okay, I will make sure to gather all this information before asking here. My apologies.

    – dw0802
    Nov 15 '18 at 0:52






  • 1





    Navigating through Code using the Step Debugger

    – None of the Above
    Nov 15 '18 at 1:15













0












0








0








My program is supposed to take team names from an XML file and display them in the listbox, and when a team is selected from the listbox, players with a higher batting average on the team are displayed in a datagridview. For some reason nothing is showing up in my listbox, and therefore I cannot select anything and test if my program works.



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;












share|improve this question














My program is supposed to take team names from an XML file and display them in the listbox, and when a team is selected from the listbox, players with a higher batting average on the team are displayed in a datagridview. For some reason nothing is showing up in my listbox, and therefore I cannot select anything and test if my program works.



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;









c# listbox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 23:04









dw0802dw0802

32




32












  • Does your dataset get populated?

    – Crowcoder
    Nov 14 '18 at 23:20






  • 5





    This is a classic case of writing a bunch of code, pressing the big go button and wondering why it doesn’t work. You ARE now a programmer, we don’t do this. You NEED to debug your code so YOU can identify at each point in code what is going on (after all you wrote it), YOU need inspect each line while it is running to determine if it has YOUR expected values, and if you can’t figure it out then you come to stack overflow with the results of those tests, the line that is not working, the expected results and the reasoning. You have none of this

    – Michael Randall
    Nov 14 '18 at 23:41







  • 1





    This comment isn't sarcastic - it's for real. Click on the margin to the left of the line of code after resultset.ReadXml. You'll see a dot appear. It's called a breakpoint. Run the program again. When the program gets to that line it will stop. Move your mouse over the word resultset and Visual Studio will let you see what's in resultset, or more specifically, resultset.Tables[0]. My guess is nothing. So this really has nothing to do with a ListBox.

    – Scott Hannen
    Nov 15 '18 at 0:28











  • @TheGeneral Okay, I will make sure to gather all this information before asking here. My apologies.

    – dw0802
    Nov 15 '18 at 0:52






  • 1





    Navigating through Code using the Step Debugger

    – None of the Above
    Nov 15 '18 at 1:15

















  • Does your dataset get populated?

    – Crowcoder
    Nov 14 '18 at 23:20






  • 5





    This is a classic case of writing a bunch of code, pressing the big go button and wondering why it doesn’t work. You ARE now a programmer, we don’t do this. You NEED to debug your code so YOU can identify at each point in code what is going on (after all you wrote it), YOU need inspect each line while it is running to determine if it has YOUR expected values, and if you can’t figure it out then you come to stack overflow with the results of those tests, the line that is not working, the expected results and the reasoning. You have none of this

    – Michael Randall
    Nov 14 '18 at 23:41







  • 1





    This comment isn't sarcastic - it's for real. Click on the margin to the left of the line of code after resultset.ReadXml. You'll see a dot appear. It's called a breakpoint. Run the program again. When the program gets to that line it will stop. Move your mouse over the word resultset and Visual Studio will let you see what's in resultset, or more specifically, resultset.Tables[0]. My guess is nothing. So this really has nothing to do with a ListBox.

    – Scott Hannen
    Nov 15 '18 at 0:28











  • @TheGeneral Okay, I will make sure to gather all this information before asking here. My apologies.

    – dw0802
    Nov 15 '18 at 0:52






  • 1





    Navigating through Code using the Step Debugger

    – None of the Above
    Nov 15 '18 at 1:15
















Does your dataset get populated?

– Crowcoder
Nov 14 '18 at 23:20





Does your dataset get populated?

– Crowcoder
Nov 14 '18 at 23:20




5




5





This is a classic case of writing a bunch of code, pressing the big go button and wondering why it doesn’t work. You ARE now a programmer, we don’t do this. You NEED to debug your code so YOU can identify at each point in code what is going on (after all you wrote it), YOU need inspect each line while it is running to determine if it has YOUR expected values, and if you can’t figure it out then you come to stack overflow with the results of those tests, the line that is not working, the expected results and the reasoning. You have none of this

– Michael Randall
Nov 14 '18 at 23:41






This is a classic case of writing a bunch of code, pressing the big go button and wondering why it doesn’t work. You ARE now a programmer, we don’t do this. You NEED to debug your code so YOU can identify at each point in code what is going on (after all you wrote it), YOU need inspect each line while it is running to determine if it has YOUR expected values, and if you can’t figure it out then you come to stack overflow with the results of those tests, the line that is not working, the expected results and the reasoning. You have none of this

– Michael Randall
Nov 14 '18 at 23:41





1




1





This comment isn't sarcastic - it's for real. Click on the margin to the left of the line of code after resultset.ReadXml. You'll see a dot appear. It's called a breakpoint. Run the program again. When the program gets to that line it will stop. Move your mouse over the word resultset and Visual Studio will let you see what's in resultset, or more specifically, resultset.Tables[0]. My guess is nothing. So this really has nothing to do with a ListBox.

– Scott Hannen
Nov 15 '18 at 0:28





This comment isn't sarcastic - it's for real. Click on the margin to the left of the line of code after resultset.ReadXml. You'll see a dot appear. It's called a breakpoint. Run the program again. When the program gets to that line it will stop. Move your mouse over the word resultset and Visual Studio will let you see what's in resultset, or more specifically, resultset.Tables[0]. My guess is nothing. So this really has nothing to do with a ListBox.

– Scott Hannen
Nov 15 '18 at 0:28













@TheGeneral Okay, I will make sure to gather all this information before asking here. My apologies.

– dw0802
Nov 15 '18 at 0:52





@TheGeneral Okay, I will make sure to gather all this information before asking here. My apologies.

– dw0802
Nov 15 '18 at 0:52




1




1





Navigating through Code using the Step Debugger

– None of the Above
Nov 15 '18 at 1:15





Navigating through Code using the Step Debugger

– None of the Above
Nov 15 '18 at 1:15












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310056%2fwhy-is-nothing-displaying-in-my-listbox-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310056%2fwhy-is-nothing-displaying-in-my-listbox-c-sharp%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Museum of Modern and Contemporary Art of Trento and Rovereto

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully