Value from DataTable into gridview as combobox

Go To StackoverFlow.com

0

I am trying to get one column from my datatable to be displayed as a combo box in my grid view. Also need to have the ability to fill the combo with a small collection to chose from. It will not show the values when i bind it from the Gui so I am trying it programmatic but cant seem to find the right code.

 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)
        {


        }
    }

This works but shows the Sample_Type as a text box where i want it to be a combo with F,P Q,B as options.

Thank you Brent

2012-04-04 17:13
by bmorrison1982


0

You have to put your ComboBox in a itemTemplate, and you have to fill it during rowDataBound event.

Quick exemple from my current project:

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

Just adapt the code to your situation

2012-04-04 17:18
by Bestter
Thank you Martin but i guess i may be in over what i can understand as i dont really understand how that works. Basically i just want to take the sample type row from my data table and make it show as a combo box instead of a text box then each new line added would be a combo box with a default value that could be changed to one in the lis - bmorrison1982 2012-04-04 18:44
Ok. You have to understand that you CANNOT fill your ComboBox the way you did it. Because the rows of your gridview do not exist yet! You first need to fill your gridview, and call yourGridView.DataBind() - Bestter 2012-04-05 15:54
After that, in your RowDataBound event, you fill your combobox like I show you. Here's a complete exemple: http://msdn.microsoft.com/en-us/library/aa479353.aspxBestter 2012-04-05 17:08
Ads