2

I would like to select a specific column from a Dataset once it's been populated (e.g. Column Grades) and put the values into a list

string excelFile = @"C:\Scores.xlsx";
if (File.Exists(excelFile))
{
    string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+excelFile+";Extended Properties=Excel 12.0;";
    var dataAdapter = new OleDbDataAdapter();
    var objConn = new OleDbConnection(connString);

    //SELECT [Name],[Grade],[Location] ect...
    const string query = "SELECT * FROM [TeamScores$]"; 
    var objCmd = new OleDbCommand(query, objConn);

    var table = new DataSet();
    dataAdapter.SelectCommand = objCmd;
    dataAdapter.Fill(table);               

    //I would like to filter the DataSet to select only [Name] and populate the values into a List<string>

    dataGridView1.DataSource = table.Tables[0]; //Will show all results                

}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
sw2020
  • 195
  • 1
  • 2
  • 10
  • I am not sure how to filter the Values within the DataSet – sw2020 Nov 17 '14 at 01:54
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 17 '14 at 03:14

4 Answers4

5

Your variable naming is confusing you.

var table = new DataSet();  // not good at all

A DataSet is not a table. A DataSet contains DataTables.

Try:

DataSet ScoresDataSet = new DataSet();

Then you can use the Select method on the table (something like...):

DataTable ScoresTable = ScoresDataSet.Tables[0]; 

dataGridView1.DataSource = ScoresTable.Select("Your criteria");
shA.t
  • 16,580
  • 5
  • 54
  • 111
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
2

I have now solved the issue I can change "Grade" to which ever column I wish and it'll show the values associated.

DataTable scoresTable = ScoresDataSet.Tables[0];                
var result = scoresTable.AsEnumerable()
    .Select(r => r.Field<string>("Grade")).Where(r => r != null);

var listOfGrades = result.ToList();
John Saunders
  • 160,644
  • 26
  • 247
  • 397
sw2020
  • 195
  • 1
  • 2
  • 10
1

You need adjust this line:

dataGridView1.DataSource = table.Tables[0]; //Will show all results   

for example this one:

dataGridView1.DataSource = table.Tables[0].Select("yourField=5")); // you filter the datarows where yourField is 5. 

or

dataGridView1.DataSource = table.Tables[0].Select("yourField>5 and yourField<21")); // is another example
A. Senna
  • 131
  • 1
  • 6
0

Hopefully this can help you :

System.Data.DataSet dsTemp2 = new System.Data.DataSet();


if (dsTemp2.Tables[0].Rows.Count <= 0)
    MessageBox.Show("Records not found");
else
{
    foreach (DataRow dRow in dsTemp2.Tables[0].Rows)
    {
        yourtextbox1.Text = dsTemp2.Tables[0].Rows[0][4].ToString();
        yourtextbox2.Text = dsTemp2.Tables[0].Rows[0][5].ToString();
        yourtextbox3.Text = dsTemp2.Tables[0].Rows[0][7].ToString();
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397