0
私のグリッドビューにコンボボックスとして表示されるように、私のデータテーブルから1つの列を取得しようとしています。また、選択する小さなコレクションでコンボを満たす能力も必要です。私はGuiからそれをバインドするときに値を表示しないので、プログラムで試していますが、正しいコードを見つけることができません。DataTableからコンボボックスとしてのgridviewへの値
connection2 = new MySqlConnection(ConnectionString2);
try
{
string proid = txtbxProjId.Text;
//prepare query to get all records from items table
string query2 = "select sl.sample_id As sample_id, sl.sample_number As Sample_Number, sl.sample_date As Sample_Date, sl.sample_time As Sample_Time, sl.sample_comments As Sample_Comments FROM spt_sample_login sl Where project_id = '"+proid+"'";
//prepare adapter to run query
adapter2 = new MySqlDataAdapter(query2, connection2);
adapter3 = new MySqlDataAdapter(query2, connection2);
//create a DataTable to hold the query results
DataTable dTable2 = new DataTable();
DataSet DS2 = new DataSet();
//fill the DataTable
//get query results in dataset
adapter2.Fill(dTable2);
adapter3.Fill(DS2);
//return datatable with all records
//BindingSource to sync DataTable and DataGridView
//set the BindingSource DataSource
GridView1.DataSource = dTable2;
this.GridView1.Columns["sample_id"].Visible = false;
this.GridView1.Columns["Sample_Type"].DisplayIndex = 4;
this.GridView1.Columns["Sample_Type"].Visible = true;
//set the DataGridView DataSource
}
catch (MySqlException ex)
{
}
}
これは動作しますが、私はそれはF、P Q、オプションとしてBとコンボになりたいテキストボックスとしてSAMPLE_TYPEを示しています。
あなたはItemTemplateににあなたのコンボボックスを配置する必要があり、そしてあなたがrowDataBoundイベントの間に、それを埋めるために持っているあなたに ブレント
はあなたにマーティンありがとうしかし、私は私が実際にどのようにそれを理解しないと私は理解できるものを上にあってもよいと思います自分の状況にコードを適応させます働く基本的には、私のデータテーブルからサンプルタイプの行を取り出し、テキストボックスの代わりにコンボボックスとして表示させ、新しい行が追加されると、デフォルト値のコンボボックスがリストの1つに変更されます – bmorrison1982
あなたはComboBoxをあなたのやり方で埋めることができないことを理解しなければなりません。あなたのgridviewの行はまだ存在しないので!まず、gridviewを塗り潰して、yourGridView.DataBind()を呼び出します。 – Bestter
その後、RowDataBoundイベントで、私はあなたのようにコンボボックスを塗りつぶします。ここに完全な例があります:[http://msdn.microsoft.com/en-us/library/aa479353.aspx](http://msdn.microsoft.com/en-us/library/aa479353.aspx) – Bestter