2012-09-09 56 views
3
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]; 

我想顯示一個DataGridView整個登錄表的數據,但我得到一個空的窗口小部件?一些人能討好?如何在c#中將表格數據顯示到datagridview中?

回答

1

分配數據源後,呼叫DataBind()

+0

什麼目的,我們將調用DataBind()? – user1610299

+0

在'Page'對象上調用它是最佳實踐。 – driis

+0

如何在我的代碼中調用dataBind()方法? – user1610299

1

嘗試使用,而不是使用DataSet來填充數據的DataTable。或者檢查你的sql連接。

+0

數據表,我也試過相同的結果.. – user1610299

+0

再次檢查您的連接字符串...是您的代碼連接數據庫?.. – hienvd

1

你在哪裏使用這種代碼。你正在使用它?試試on_load事件。 使用默認視圖,然後使用DataBind()方法綁定數據。

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

dataGridView1.DataBind();

0

您的代碼:

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

並在下一行...

dataGridView1.Databind(); 

醜陋雖然。

0

儘量使用綁定來源作爲數據網格的數據源。

var bindingSource = new System.Windows.Forms.BindingSource(); 
bindingSource.DataSource = ds.Tables[0]; 
dataGridView1.DataSource = bindingSource; 
-1
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; 
相關問題