Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ComboBox in Datagrid view

ComboBox in Datagrid view

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cocoonwls
    wrote on last edited by
    #1

    Hi all, Basically i have 2 question in this message.I am using DataGridViewComboBoxColumn to insert a ComboBox into my datagridview.Code as below:

            dataGridView1.Columns.Remove("Seqence\_Number");  <----remove the original column first before add the combo box
            DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn();
            dgvComboBox.DataPropertyName = "Seqence\_Number";
            dgvComboBox.HeaderText = "Sequence (Stage)";
            dgvComboBox.DropDownWidth = 100;
            dgvComboBox.Width = 100;
            dgvComboBox.MaxDropDownItems = 10;  
            dgvComboBox.FlatStyle = FlatStyle.System;
    
            dsTemp = singleton.ProdetailsDataset.Copy();  <-----dataset will copy from my singleton class
            dgvComboBox.DataSource = dsTemp.Tables\[0\];
            dgvComboBox.ValueMember = "Stage\_Name";
            dgvComboBox.DisplayMember = "Stage\_Name";
         
            dataGridView1.Columns.Insert(1,dgvComboBox);
    

    The first question is, how can i display more than one value member in ComboBox? The second qeustion is, how can i do a selected event for that ComboBox in datagridview? what i wanna do is when user select the value in ComboBox, some event will fire to validate the value which selected by user. Any tips are welcome, Thanks in advance

    G 1 Reply Last reply
    0
    • C cocoonwls

      Hi all, Basically i have 2 question in this message.I am using DataGridViewComboBoxColumn to insert a ComboBox into my datagridview.Code as below:

              dataGridView1.Columns.Remove("Seqence\_Number");  <----remove the original column first before add the combo box
              DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn();
              dgvComboBox.DataPropertyName = "Seqence\_Number";
              dgvComboBox.HeaderText = "Sequence (Stage)";
              dgvComboBox.DropDownWidth = 100;
              dgvComboBox.Width = 100;
              dgvComboBox.MaxDropDownItems = 10;  
              dgvComboBox.FlatStyle = FlatStyle.System;
      
              dsTemp = singleton.ProdetailsDataset.Copy();  <-----dataset will copy from my singleton class
              dgvComboBox.DataSource = dsTemp.Tables\[0\];
              dgvComboBox.ValueMember = "Stage\_Name";
              dgvComboBox.DisplayMember = "Stage\_Name";
           
              dataGridView1.Columns.Insert(1,dgvComboBox);
      

      The first question is, how can i display more than one value member in ComboBox? The second qeustion is, how can i do a selected event for that ComboBox in datagridview? what i wanna do is when user select the value in ComboBox, some event will fire to validate the value which selected by user. Any tips are welcome, Thanks in advance

      G Offline
      G Offline
      Gopal S
      wrote on last edited by
      #2

      Hi, 1) Concatnate the two column values (in a query) and assign as a value member. 2) You can use "EditingControlShowing" event to access the "SelectedIndexChanged" event for combobox cell. Here is the Code Snipet: private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing); } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox ctl = e.Control as ComboBox; if(ctl!=null) ctl.SelectedIndexChanged+=new EventHandler(ctl_SelectedIndexChanged); } private void ctl_SelectedIndexChanged(object sender, EventArgs args) { MessageBox.Show("Selected Index Changed Event"); } Thanks,

      Gopal.S

      C 1 Reply Last reply
      0
      • G Gopal S

        Hi, 1) Concatnate the two column values (in a query) and assign as a value member. 2) You can use "EditingControlShowing" event to access the "SelectedIndexChanged" event for combobox cell. Here is the Code Snipet: private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing); } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox ctl = e.Control as ComboBox; if(ctl!=null) ctl.SelectedIndexChanged+=new EventHandler(ctl_SelectedIndexChanged); } private void ctl_SelectedIndexChanged(object sender, EventArgs args) { MessageBox.Show("Selected Index Changed Event"); } Thanks,

        Gopal.S

        C Offline
        C Offline
        cocoonwls
        wrote on last edited by
        #3

        Hi Gopal.S Thanks for your suggestion. I am just figure out how to do it, but seem you have a better suggestion :) 1) I just simple Concatnate the columns after i get the dataset, something like below: string s = dsTemp.Tables[0].Rows[1][2].ToString() + dsTemp.Tables[0].Rows[1][3].ToString(); 2) I am using CellEndEdit,as below;

        this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
        
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
         string cellvalue;
         if (e.ColumnIndex == 1)
         {
          cellvalue = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
          if (cellvalue != null && cellvalue != "")
          {
           //some coding here
          }
        }
        

        I will try you suggestion too, Thanks :laugh:

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups