现在是我的代码。任何建议,评论或示例代码都非常感谢。谢谢。
DataSet result = excelReader.AsDataSet();
excelReader.Close();
if (result != null)
{
DataTable dataTable = result.Tables[0];
List<string> headers = new List<string>();
foreach (DataColumn col in dataTable.Columns)
{
headers.Add(col.ColumnName);
}
dataGridView1.DataSource = dataTable;
}
答案 0 :(得分:5)
尝试
string[] columnNames = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
dataGridView1.DataSource = columnNames;
或
dataGridView1.DataSource = dt.Columns.Cast<DataColumn>()
.Select(x => x.ColumnName)
.ToArray();
答案 1 :(得分:0)
只需将Header设置为可见:
DataGridView.ColumnHeaderVisible = true;
这对我有用。
答案 2 :(得分:0)
System.Data.DataTable dt = GridDataSource.Tables[0];
foreach (System.Data.DataColumn col in dt.Columns)
{
cmbSelectSearchBy.Items.Add(col.ColumnName);
}
cmbSelectSearchBy.Text = "Select Column";