惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
C
Cisco Blogs
博客园 - 叶小钗
P
Privacy International News Feed
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
IT之家
IT之家
博客园 - 聂微东
Know Your Adversary
Know Your Adversary
Help Net Security
Help Net Security
罗磊的独立博客
I
Intezer
S
Schneier on Security
博客园_首页
C
CERT Recently Published Vulnerability Notes
雷峰网
雷峰网
Cisco Talos Blog
Cisco Talos Blog
宝玉的分享
宝玉的分享
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Webroot Blog
Webroot Blog
TaoSecurity Blog
TaoSecurity Blog
MyScale Blog
MyScale Blog
P
Privacy & Cybersecurity Law Blog
T
The Exploit Database - CXSecurity.com
PCI Perspectives
PCI Perspectives
Security Latest
Security Latest
H
Heimdal Security Blog
S
Secure Thoughts
Hacker News: Ask HN
Hacker News: Ask HN
Y
Y Combinator Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Microsoft Security Blog
Microsoft Security Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
SecWiki News
SecWiki News
The GitHub Blog
The GitHub Blog
A
Arctic Wolf
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
T
Threat Research - Cisco Blogs
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 小光_520

这个博客18年了.. C#基础概念之延迟加载(转) C# ASP.NET CSV文件导入数据库(转) GridView 使用方法总结二(转) GridView 使用方法总结一(转) 单条SQL语句实现复杂逻辑几例(转) Statspack之十四-"log file sync" 等待事件(转贴) Statspack之十三-Enqueue(转贴) Statspack之十二-db file scattered read-DB文件分散读取(转帖) Statspack之十一-Statspack报告各部分简要说明(转帖) Statspack之十-调整STATSPACK的收集门限(转帖) Statspack之九-其它重要脚本(转帖) Statspack之八-删除历史数据(转帖) Statspack之七-移除定时任务(转贴) Statspack之六-生成分析报告(转贴) Statspack之五-规划自动任务(转贴) Statspack之四-测试安装好的Statspack(转贴) Statspack之三-安装statspack(转贴) Statspack之二-需要更改的系统参数(转贴)
把EXCEL文件导入到GridView,GridView根据要求动态的增加列(转)
小光_520 · 2012-11-20 · via 博客园 - 小光_520

做一个小网页,要把EXCEL中的内容导入到GridView中,而起还并不是单纯的显示出来就OK了,还要根据要求动态的在GridView显示

因为GridView的每一列绑定于哪一个数据字段,根本不清楚!还好有前人留下的点点滴滴,终于完成了!!

此段代码是吧EXCEL的数据导入到一个数据集中!

这点相对简单,各个地方都有代码可COPY

string mystring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + savePath + "';Extended properties=Excel 8.0";

                OleDbConnection conn = new OleDbConnection(mystring);

                conn.Open();

                OleDbDataAdapter myDa = new OleDbDataAdapter("select   *   from   [Sheet1$]", conn);

                DataSet myDs = new DataSet();

                myDa.Fill(myDs, "xls");

                DataTable dt_test = myDs.Tables[0];

  如果是原封不动的单纯的显示excel文件的内容,绑定数据源就OK了!

GridView1.DataSource = dt;

            GridView1.DataBind();

 但是我们的目标不是这个,是要动态的显示,动态的绑定

int icols = dt_test.Columns.Count;

gv_content.DataSource = dt_test;

BoundField f;

for (int i = 0; i < icols; i++)

{

    f = new BoundField();

    f.HeaderText = "列标题";

    f.DataField = dt_test.Columns[i].ColumnName;

    gv_content.Columns.Add(f);

}

gv_content.DataBind();

  这样能修改的幅度就比较大了!

转自:http://www.cnblogs.com/allan5204/archive/2012/08/09/2629869.html