如何在c#中将表格数据显示到datagridview中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12341475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display table data into datagridview in c#?
提问by user1610299
mySqlCnnection.Open();
string list = "select * from login";
MySqlDataAdapter dataadapter = new MySqlDataAdapter(list, mySqlCnnection);
DataSet ds = new DataSet();
dataadapter.Fill(ds, "login");
dataGridView1.DataSource =ds.Tables[0];
I want to display the entire login table data in a datagridview but am getting an empty widget?some one could please?
我想在 datagridview 中显示整个登录表数据,但我得到了一个空的小部件?有人可以吗?
回答by driis
Call DataBind()after assigning the DataSource.
DataBind()分配数据源后调用。
回答by hienvd
Try using DataTable instead of using DataSet to fill data. Or check your sql connection.
尝试使用 DataTable 而不是使用 DataSet 来填充数据。或者检查你的sql连接。
回答by sohail.hussain.dyn
Where you are using this code. On which you are using it? Try it on on_load event. Use Default view and then bind the data using DataBind() method.
您在哪里使用此代码。你在哪个上使用它?在 on_load 事件上试试。使用默认视图,然后使用 DataBind() 方法绑定数据。
dataGridView1.DataSource =ds.Tables[0].DefaultView;
dataGridView1.DataSource =ds.Tables[0].DefaultView;
dataGridView1.DataBind();
dataGridView1.DataBind();
回答by War
Your code:
您的代码:
mySqlCnnection.Open();
string list = "select * from login";
MySqlDataAdapter dataadapter = new MySqlDataAdapter(list, mySqlCnnection);
DataSet ds = new DataSet();
dataadapter.Fill(ds, "login");
dataGridView1.DataSource =ds.Tables[0];
And on the next line ...
在下一行...
dataGridView1.Databind();
Ugly though.
虽然丑。
回答by Dmytro
Try to Use binding source as datasource of your datagrid.
尝试使用绑定源作为数据网格的数据源。
var bindingSource = new System.Windows.Forms.BindingSource();
bindingSource.DataSource = ds.Tables[0];
dataGridView1.DataSource = bindingSource;
回答by ??? ?????
string list = "select * from login";
SqlCommand command = new SqlCommand(list , db.connect());
SqlDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
dataGridView1.DataSource=dt;

