将datatable第一行设置为网格标题 [英] set datatable first row as grid header

查看:88
本文介绍了将datatable第一行设置为网格标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我正在使用以下连接字符串从xls文件中获取数据

I have a scenario where i am fetching data from xls file using following connection string

mCon.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;data source=" + mstrFilePath + ";Extended Properties=\"Excel 12.0;HDR=NO\";");

和以下代码

string strSelectQuery = "Select * from [sheet1$]";    
System.Data.OleDb.OleDbCommand= new System.Data.OleDb.OleDbCommand(strSelectQuery, mCon);
DataAdapter = new System.Data.OleDb.OleDbDataAdapter(strSelectQuery, mCon);
DataAdapter.Fill(mDTable);  

现在,当我将此数据表mDtable绑定到gridview时,将导致没有网格的标题行.

Now when i bind this datatable mDtable to gridview, It results in no header row for grid.

但是我有另一个gridview,它需要将标题列作为数据表mDTable中的第一行.

But i have another gridview which needs to have header columns as the first row in datatable mDTable.

我该如何使用相同的数据表来绑定既没有标题行又有标题行的gridview?

How can i use same datatable to bind both gridview's one without header row and another with header row ??

推荐答案

严厉的解决方案:

您可以使用DataTable.Columns [n] .ColumnName对列标题进行签名.

You can sign the column header, using DataTable.Columns[n].ColumnName.

示例(未经测试,请注意代码错误)

Example (not tested, beware of code error):

int index = 0;
foreach(DataTableColumn col in DataTable.Columns)
{
  col.ColumnName = DataTable.Rows[0][index].ToString();
  index++;
}

然后,您可以将DataTable绑定到gridview,并将AutoGenerateColumn设置为true.

Then you can bind the DataTable to gridview, and set AutoGenerateColumn to true.

这篇关于将datatable第一行设置为网格标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆