Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 值作为组合框从DataTable输入gridview_C#_Mysql - Fatal编程技术网

C# 值作为组合框从DataTable输入gridview

C# 值作为组合框从DataTable输入gridview,c#,mysql,C#,Mysql,我试图从我的datatable中获取一列,以便在我的网格视图中显示为组合框。还需要有能力填写一个小集合组合选择。当我从Gui绑定它时,它不会显示值,所以我尝试编程,但似乎找不到正确的代码 connection2 = new MySqlConnection(ConnectionString2); try { string proid = txtbxProjId.Text; //prepare query to

我试图从我的datatable中获取一列,以便在我的网格视图中显示为组合框。还需要有能力填写一个小集合组合选择。当我从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)
        {


        }
    }
这是可行的,但将示例_类型显示为一个文本框,我希望它是一个带有F、pq、B选项的组合

多谢各位
Brent

您必须将组合框放入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();

只需根据您的情况调整代码即可

谢谢Martin,但我想我可能无法理解,因为我不太明白它是如何工作的。基本上,我只想从我的数据表中获取示例类型行,并将其显示为组合框而不是文本框,然后添加的每一行都将是一个组合框,其默认值可以更改为listOk中的一个。你必须明白,你不能用这种方式填充你的组合框。因为gridview的行还不存在!首先需要填充gridview,并调用yourGridView.DataBind。然后,在RowDataBound事件中,填充组合框,如我所示。下面是一个完整的示例: