[英]Value from DataTable into gridview as combobox
我試圖從我的數據表中獲取一列以在我的網格視圖中顯示為組合框。 還需要有能力用一個小集合來填充組合以供選擇。 當我從 Gui 綁定它時它不會顯示值,所以我正在嘗試編程但似乎找不到正確的代碼。
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、PQ、B 作為選項組合。
謝謝布倫特
您必須將 ComboBox 放入 itemTemplate 中,並且必須在 rowDataBound 事件期間填充它。
我當前項目的快速示例:
DropDownList ddlRole = (DropDownList)e.Row.FindControl("ddlRole");
ddlRole.DataSource = GetTruckersRoles();
string[] rolesList = Roles.GetRolesForUser((string)gvTruckers.DataKeys[e.Row.RowIndex][0]);
if (rolesList.Length > 0)
{
//If user is not in any roles, dont' do this
ddlRole.SelectedValue = rolesList[0];
}
ddlRole.DataBind();
只需根據您的情況調整代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.