2012-10-15 95 views
3

我試了好幾次,使其工作,但它只是不填充與MySQL數據DataGridView的,這裏是我的代碼:填充的DataGridView與MySQL數據

string connectionString = "SERVER=localhost;DATABASE=shootsource;UID=root;PASSWORD=;"; 
     string sql = "SELECT * FROM characters"; 
     MySqlConnection connection = new MySqlConnection(connectionString); 
     connection.Open(); 
     sCommand = new MySqlCommand(sql, connection); 
     sAdapter = new MySqlDataAdapter(sCommand); 
     sBuilder = new MySqlCommandBuilder(sAdapter); 
     sDs = new DataSet(); 
     sAdapter.Fill(sDs, "characters"); 
     sTable = sDs.Tables["characters"]; 
     connection.Close(); 
     dataGridView1.DataSource = sDs.Tables["characters"]; 
     dataGridView1.ReadOnly = true; 
     dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 

它只顯示從mysql表中的列'字符':身份證,名稱,時間等...我怎麼能從數據庫中填充它?

這是問題的一個形象:

enter image description here

+0

只顯示列**頭* *?你是這個意思嗎?你確定桌子上有數據嗎? – climbage

+0

是啊有..是啊..這裏有一張圖片:http://www.picz.ro/index.php?id=1dc2d2d989590d263a979c7a097124a0 –

+0

我看不出有什麼問題 – climbage

回答

0

您設置數據源後,您需要做的DataBind

dataGridView1.DataBind(); 
7
 private void MySQL_ToDatagridview() 
    { 
     //VarribleKeeper.MySQLConnectionString = your connection string 
     //info being your table name 
     MySqlConnection mysqlCon = new 

     MySqlConnection(VarribleKeeper.MySQLConnectionString); 
     mysqlCon.Open(); 

     MySqlDataAdapter MyDA = new MySqlDataAdapter(); 
     string sqlSelectAll = "SELECT * from info"; 
     MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon); 

     DataTable table = new DataTable(); 
     MyDA.Fill(table); 

     BindingSource bSource = new BindingSource(); 
     bSource.DataSource = table; 


     dataGridView1.DataSource = bSource; 
    }