0

データテーブルから 1 つの列を取得して、グリッド ビューにコンボ ボックスとして表示しようとしています。また、選択できる小さなコレクションでコンボを埋める機能も必要です。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 をオプションとして組み合わせて表示します。

ありがとうブレント

4

1 に答える 1

0

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();

コードを状況に合わせて調整するだけです

于 2012-04-04T17:18:59.377 に答える