将DataTable中的值作为组合框输入到gridview中

本文关键字:输入 组合 gridview DataTable | 更新日期: 2023-09-27 18:30:13

我正试图从数据表中获取一列,以便在网格视图中显示为组合框。还需要有能力用一个小集合来填充组合进行选择。当我从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、p、Q、B为选项的组合。

谢谢布伦特

将DataTable中的值作为组合框输入到gridview中

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

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