简体   繁体   English

如何在c#中将表数据显示到datagridview中?

[英]How to display table data into datagridview in c#?

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中显示整个登录表数据,但我得到一个空的小部件?有人可以吗?

在分配DataSource之后调用DataBind()

Try using DataTable instead of using DataSet to fill data. 尝试使用DataTable而不是使用DataSet来填充数据。 Or check your sql connection. 或者检查你的sql连接。

Where you are using this code. 你在哪里使用这个代码。 On which you are using it? 你正在使用它? Try it on on_load event. 在on_load事件上尝试一下。 Use Default view and then bind the data using DataBind() method. 使用默认视图,然后使用DataBind()方法绑定数据。

dataGridView1.DataSource =ds.Tables[0].DefaultView; dataGridView1.DataSource = ds.Tables [0] .DefaultView;

dataGridView1.DataBind(); dataGridView1.DataBind();

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. 虽然丑陋。

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;
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;

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

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