2016-03-17 34 views
1

我建立了一個到我的數據庫的連接,但是當我嘗試從一個表中查看某些數據時,並沒有在dataGridView中顯示。當我按下按鈕,幾秒鐘似乎會顯示的值,之後沒有發佈在dataGridView在DataGridView中顯示MySQL數據庫值C#

任何人都可以解釋,如果我在我的代碼中犯了錯誤? 我在Visual Studio 2013的工作

我的代碼:

private void button1_Click(object sender, EventArgs e) 
{ 
    string connectionString = "datasource=localhost;database=microweb;port=3306;username=root;password=pass"; 

    MySqlConnection conn = new MySqlConnection(connectionString); 
    MySqlCommand cmd = new MySqlCommand("select * from reservations", conn); 

    try 
    { 
     MySqlDataAdapter sda = new MySqlDataAdapter(); 
     sda.SelectCommand = cmd; 
     DataTable dta = new DataTable(); 
     sda.Fill(dta); 
     BindingSource bdsour = new BindingSource(); 

     bdsour.DataSource = dta; 
     dataGridView1.DataSource = bdsour; 
     sda.Update(dta); 

    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    }  
} 
+0

是您的數據源真名叫 「localhost」 的?如果是這樣,在填充後dta中包含多少(任何?)記錄? –

+0

是的,被命名爲localhost。我在connectionString中做了一些更改,就像另一個數據庫名稱,傳遞,數據源,並顯示我一些錯誤,如沒有數據庫命名...或密碼不正確... –

+0

我試圖使用沒有BindingSource,並且沒有更改.... –

回答

2

您應該設置AutoGenerateColumns爲true

bdsour.DataSource = dta; 
dataGridView1.AutoGenerateColumns = true; 
dataGridView1.DataSource = bdsour; 
+0

的值謝謝!!!!正在工作 –

+0

@AlexV很高興有幫助 – Marusyk

相關問題