我想在填充后从数据集中选择一个特定的列(例如列等级)并将值放入列表中
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
}
Run Code Online (Sandbox Code Playgroud)
您的变量命名让您感到困惑。
var table = new DataSet(); // not good at all
Run Code Online (Sandbox Code Playgroud)
数据集不是表。数据集包含数据表。
尝试:
DataSet ScoresDataSet = new DataSet();
Run Code Online (Sandbox Code Playgroud)
然后你就可以在表上使用 Select 方法(类似于...):
DataTable ScoresTable = ScoresDataSet.Tables[0];
dataGridView1.DataSource = ScoresTable.Select("Your criteria");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11737 次 |
| 最近记录: |