Click here to Skip to main content
15,915,603 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
DataSet ds = new DataSet();
           DataRow[] result = ds.Tables[0].Select("Team = '" + listBox1.SelectedItem + "'"); // filter result set
           DataTable dtPlayerInfo = new DataTable();
           dtPlayerInfo = result.CopyToDataTable();



           DataTable dtCloned = dtPlayerInfo.Clone();
           dtCloned.Columns[3].DataType = typeof(double);
           dtCloned.Columns[2].DataType = typeof(double);

           foreach (DataRow row in dtPlayerInfo.Rows)
           {
               dtCloned.ImportRow(row);

           }
           // Avg hit of team
           double hitsAvg = dtCloned.AsEnumerable()
               .Average(r =>
               r.Field<double>("hits"));

           // Avg bat of team
           double atBatAvg = dtCloned.AsEnumerable()
               .Average(r =>
               r.Field<double>("atBats"));

           // Actual Avg of team
           double teamAvg = hitsAvg / atBatAvg;
           DataTable dtPlayerAvg = new DataTable();
           DataRow rowPlayer = dtPlayerAvg.NewRow();
           dtPlayerAvg.Columns.Add("Player", typeof(string));
           dtPlayerAvg.Columns.Add("Batteing Avg", typeof(double));

           for (int i = 0; i < dtCloned.Rows.Count; i++) ;
           {
               double AvgPayer = Convert.ToDouble(dtCloned.Rows[3]) / Convert.ToDouble(dtCloned.Rows[2]);
               if (teamAvg <= AvgPayer) ; // checked palyer Avg to Team Avg
               {
                   dtPlayerAvg.Rows.Add(dtCloned.Rows[0].ToString(), Math.Round(AvgPayer, 4));

               }
           }

           DataView view = dtPlayerAvg.DefaultView;
           view.Sort = "Batteing Avg DESC ";
           //This cannot be done with the original data table. so created a new, sorted one:
           DataTable sortedDate = view.ToTable();
           dataGridView1.DataSource = sortedDate;


What I have tried:

I tried everything but still doesnt work
Posted
Updated 16-Nov-20 20:09pm
Comments
PIEBALDconsult 16-Nov-20 20:45pm    
DataSet ds = new DataSet();
DataRow[] result = ds.Tables[0] <==== There are no tables in a new DataSet.

1 solution

Look at your code:
C#
DataSet ds = new DataSet();
DataRow[] result = ds.Tables[0].Select("Team = '" + listBox1.SelectedItem + "'");

ds is a new DataSet: you have added no data to it at all. As a result, it has no tables, and you cannot access them.
No tables, nothing to filter, error message because the index is out of range.

Find the DataSet you want in your code, or load the DataSet with data - your choice!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900