2016-10-26 14 views
0

テーブルデータをDataGridViewに入れたC#でMySQL接続をしていますが、問題があり、データベース内のユーザーテーブルのすべてのアカウントの行が作成されますが、 tは実際のデータを示します。誰も助けることができますか?DataGridViewが私の列を表示していません

マイコード:

public Form1() 
{ 
    InitializeComponent(); 

    string connectionString = "Server=mysql.dunkycart.com; Database=dunkycart; Uid=dunkycart; Pwd=rooB-dnK-sqL;"; //Set your MySQL connection string here. 
    string query = "SELECT name, username, email FROM users"; // set query to fetch data "Select * from tabelname"; 
    using (MySqlConnection conn = new MySqlConnection(connectionString)) 
    { 
     using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn)) 
     { 
      DataSet ds = new DataSet(); 
      adapter.Fill(ds); 
      dataGridView1.DataSource = ds.Tables[0]; 
     } 
    } 
} 

問題: This is the output of the form. I entered the column names manually, btw, so it's not doing that either.

+0

この投稿は役に立ちましたか? [バインディングソースからフォームのコントロールを自動生成する方法はありますか?](http://stackoverflow.com/questions/38165043/is-there-a-way-to-auto-generate-controls-on-a -form-from-a-binding-source) –

答えて

0

あなたはDataSet代わりのDataTableを使用しているためので、あなたは、この変更する必要があります。これに

adapter.Fill(ds); 

adapter.Fill(ds,"tbl"); 

それとも、代わりにDataSetDataTableを使用することができます。

DataTable dt = new DataTable(); 
adapter.Fill(dt); 
+0

これは表示されますが、テーブルにない余分な空白の行が表示されます。どのようにこれを修正するための任意のアイデア? –

+0

@Wioooはい。 'AllowUserToAddRows'を' DataGridView'のプロパティで 'False'に設定してください。 –

0

ネヴァーマインド、私は修正を見つけました。私がしなければならなかったのは、DataGridViewの各列のDataPropertyNameをdbテーブルの対応する列に設定することです。

関連する問題