繁体   English   中英

DataTable 中的值变成 gridview 为 combobox

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM