0
我想从我的数据表中获取一列以在我的网格视图中显示为组合框。还需要有能力用一个小集合填充组合来选择。它不会显示值时,我把它从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)
{
}
}
这工作,但显示了Sample_Type,那是我希望它是用F,P Q,B中选择一个组合文本框。
谢谢 布伦特
谢谢马丁,但我想我可能是在什么我能理解,因为我真的不知道怎么说作品。基本上我只想从我的数据表中取出样本类型的行,并将其显示为组合框而不是文本框,然后每个添加的新行将是一个组合框,其默认值可以更改为列表中的一个 – bmorrison1982 2012-04-04 18:44:31
好的。你必须明白你不能像你那样填充你的ComboBox。因为你的gridview的行还不存在!你首先需要填写你的gridview,然后调用你的GridView.DataBind()。 – Bestter 2012-04-05 15:54:30
之后,在你的RowDataBound事件中,你会像我向你展示的那样填充你的组合框。以下是一个完整的示例:[http://msdn.microsoft.com/en-us/library/aa479353.aspx](http://msdn.microsoft.com/en-us/library/aa479353.aspx) – Bestter 2012-04-05 17:08:40