我想在填充数据集中后选择一个特定的列(例如列成绩)并将值放入列表中
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
}
最佳答案
您的变量命名让您感到困惑。
var table = new DataSet(); // not good at all
A DataSet is not a table. A DataSet contains DataTables.
尝试:
DataSet ScoresDataSet = new DataSet();
然后您可以在表格上使用 Select 方法(类似于...):
DataTable ScoresTable = ScoresDataSet.Tables[0];
dataGridView1.DataSource = ScoresTable.Select("Your criteria");
关于c# - 数据集选择列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26964357/