简体   繁体   English

绑定到datagridview

[英]Bind to datagridview

How can i bind data to datagridview? 如何将数据绑定到datagridview?

DataTable table = new DataTable();
string pot = "Provider=Microsoft.Ace.OLEDB.12.0; Data Source = " + textbox_path.Text + ";Extended Properties=\"Excel 8.0; HDR=Yes;\";";
OleDbConnection pove = new OleDbConnection(pot);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + textbox_sheet.Text + "$]", pove);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt); /* napaka | oldedb driver ? :O*/
dataGridView1.DataSource = dt;

在末尾添加:-

dataGridView1.DataBind();

If you are working in web application you have to add dataGridView1.DataBind(); 如果您正在使用Web应用程序,则必须添加dataGridView1.DataBind(); after dataGridView1.DataSource = dt; dataGridView1.DataSource = dt; .

Else tell us what is the problem in your above code? 否则,告诉我们您上面的代码有什么问题? Is it throwing any error? 它抛出任何错误吗?

Check the below MSDN link. 检查下面的MSDN链接。 It has working examples. 它具有有效的示例。

http://msdn.microsoft.com/en-us/library/vstudio/fbk67b6z(v=vs.100).aspx http://msdn.microsoft.com/zh-CN/library/vstudio/fbk67b6z(v=vs.100).aspx

Although you did not mention it, this question was tagged as winforms. 尽管您没有提及,但该问题被标记为winforms。 Winforms does not need Winforms不需要
dataGridView1.DataBind();
You have not given us any errors or hints as to where it fails. 您尚未向我们提供任何错误或失败提示。 I suspect it is in the formation of this connection string or select statement. 我怀疑它是在此连接字符串或select语句的形成中。 The code to bind the datatable to the DGV is correct 将数据表绑定到DGV的代码正确

string pot = "Provider=Microsoft.Ace.OLEDB.12.0; Data Source = " + textbox_path.Text + ";Extended Properties=\"Excel 8.0; HDR=Yes;\";";
OleDbConnection pove = new OleDbConnection(pot);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from [" + textbox_sheet.Text + "$]", pove);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM