none
Datagridview with combobox not mapping to SQL select statement RRS feed

  • Question

  • My datagridview fetches 4 columns from the database, and I would like the 3rd column to map to a combobox that I have loaded with descriptive data from the database. When I execute the form, I end up with 8 columns in my datagridview.

    Code:

     

    SqlDataAdapter da = new SqlDataAdapter("Select * from tblactivitycodes", clsConnectionDB.cnROMSdb);

     

    DataSet ds = new DataSet("CODES");

    da.Fill(ds,

    "CODES");

     

    cmbItem[] cmbItems = new cmbItem[ds.Tables[0].Rows.Count];

     

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

    {

    cmbItems[i] =

    new cmbItem(ds.Tables[0].Rows[i]["activity_description"].ToString(),ds.Tables[0].Rows[i]["activity_key"].ToString());

    }

    da =

    new SqlDataAdapter("Select workdate as Date,workdescription as Project,activity_key as Activity,hours as Hours from tbltime where employee_key=17 order by workdate desc", clsConnectionDB.cnROMSdb);

    ds =

    new DataSet("TIME");

    da.Fill(ds,

    "TIME");

    dg.Columns.Add(TextCol(

    "Date"));

    dg.Columns[0].Width = 90;

    dg.Columns.Add(TextCol(

    "Project"));

    dg.Columns[1].Width = 200;

    dg.Columns.Add(ComboCol(

    "Activity", cmbItems, "DisplayMember", "ValueMember", "Select Activity"));

    dg.Columns[2].Width = 140;

    dg.Columns.Add(TextCol(

    "Hours"));

    dg.Columns[3].Width = 80;

    dg.Columns[3].DefaultCellStyle.Alignment =

    DataGridViewContentAlignment.MiddleCenter;

    dg.DataSource = ds;

    dg.DataMember =

    "TIME";



    Is there anyway to force the "Activity" column to map to the entries in the combobox column of the datagridview?

    Thanks guys !!

    Monday, December 7, 2009 4:12 PM

Answers

  • Hi Terry,

    I am a little confused about your description:
    >>how to replace the numeric activity code with the description in a combobox.

    Can you elaborate on it?



    Best regards,
    Guang-Ming Bian - MSFT
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by tarne Monday, October 17, 2011 3:29 PM
    Wednesday, December 9, 2009 9:53 AM
    Moderator

All replies

  • Hi tarne,

     

    I don’t think you need extract data from dataset to cmbItem[], we can bind the dataset data to combbox directly. Just set field name as combobox displaymember and ValueMember. Like the code below:

     

    namespace WindowsApplication1

    {

     

        public partial class Form1 : Form

        {

     

            public Form1()

            {

                InitializeComponent();

            }

     

            DataTable gridDT = new DataTable();

            DataTable comDT = new DataTable();

     

            private void Form1_Load(object sender, EventArgs e)

            {

     

                gridDT.Columns.Add("order");

     

                gridDT.Columns.Add("productid");

     

                gridDT.Rows.Add("01");

     

                gridDT.Rows.Add("02");

     

                gridDT.Rows.Add("03");

     

                gridDT.Rows.Add("04");

     

                gridDT.Rows.Add("05");

     

                gridDT.AcceptChanges();

     

     

     

                comDT.Columns.Add("productid");

     

                comDT.Columns.Add("name");

     

                comDT.Columns.Add("date", typeof(DateTime));

     

                comDT.Columns.Add("weight", typeof(int));

     

                comDT.Rows.Add("001", "aaa", "2006-02-25", "15");

     

                comDT.Rows.Add("002", "bbb", "2006-01-30", "100");

     

                comDT.Rows.Add("003", "ccc", "2006-08-08", "5");

     

                comDT.Rows.Add("004", "ddd", "2007-01-15", "30");

     

                comDT.AcceptChanges();

     

     

     

                this.dataGridView1.DataSource = gridDT;

     

     

                this.dataGridView1.Columns.RemoveAt(1);

     

     

                DataGridViewComboBoxColumn dgvcob = new DataGridViewComboBoxColumn();

     

                dgvcob.HeaderText = "Product";

     

                dgvcob.DataPropertyName = "productid";

     

                dgvcob.DisplayMember = "name";

     

                dgvcob.ValueMember = "productid";

     

                dgvcob.DataSource = comDT;

     

                dgvcob.DefaultCellStyle.NullValue = "Choose a value";

     

                dgvcob.Width = 200;

     

                dgvcob.DisplayStyleForCurrentCellOnly = true;

     

                this.dataGridView1.Columns.Add(dgvcob);

     

            }

     

        }

     

    }

     

     

    Although in above sample, combobox is binding with datatable we create manually, we can query data into datatable/dataset, then set the same way.

     

    If you have any question, please let me know.


    Have a nice day.


    Best regards,

    Guang-Ming Bian - MSFT

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Tuesday, December 8, 2009 5:55 AM
    Moderator
  • Guang-Ming Bian,

    Thank you for responding. Please forgive my ignorance, but I can not follow your example. I'm having a hard time visualizing how to replace the numeric activity code with the description in a combobox. Can you point me to some other examples?

    Terry
    Tuesday, December 8, 2009 4:00 PM
  • Hi Terry,

    I am a little confused about your description:
    >>how to replace the numeric activity code with the description in a combobox.

    Can you elaborate on it?



    Best regards,
    Guang-Ming Bian - MSFT
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by tarne Monday, October 17, 2011 3:29 PM
    Wednesday, December 9, 2009 9:53 AM
    Moderator