none
how to get the selected value from DataGridViewComboBoxColumn in c#? RRS feed

  • Question

  • hi

    I've added a DataGridViewComboBoxColumn and i want to get the value selected in the combobox. How to get this?

    Tuesday, July 6, 2010 12:15 PM

Answers

  • Hi AristocratP,

    According to your need: the value of row2 of this column( say cell[1][0] for example ) is determined by row1 in the same column (cell[0][0]), row3 (cell[2][0]) is determined by row2 ( cell[1][0] ),etc. right? If I'm off base, please correct me.

    If so, why do you use combobox column? You just need combobox cell for the first row of the column, other cells in the same column can be replaced by textbox cell. To do the determinations , you can use "switch...case" for the cells of this column.

    What I mean is your requirement is doing automatic assignment on column, not on row! If on row, you can bind them to a datatable to perform the automation, but on columns :) seems not so convenient. 

    I think the following links will help you more on DataGridView:

    http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.displaymember(VS.80).aspx

    Bind DatagridviewComboboxcolumn

    http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database

    Best regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    • Marked as answer by Helen Zhou Tuesday, July 13, 2010 3:14 AM
    Friday, July 9, 2010 8:55 AM

All replies

  • if you want to get an integer:

    int myInt = int.Parse(dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["YourColumnNameOrNumber"].Value.ToString();

    if its a string:

    string myString = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells["YourColumnNameOrNumber"].FormattedValue.ToString();

    if you want to define the columns number, you can write the column name in the inverted commas (["3rdColumnName"]), or simply enter the column`s number ([2] - if this is 3rd column).

    Hope it helps,

    Mitja

    Tuesday, July 6, 2010 1:08 PM
  • if DataGridViewComboBoxColumn is databount, then get value member of cell. Like this

    DataGridViewComboBoxColumn dgvc;
    dgvc = (DataGridViewComboBoxColumn)dataGridView1.Columns["Column1"];
    string strText = dgvc.DisplayMember.ToString();
    
    Tuesday, July 6, 2010 1:14 PM
  • hi

    I have added a DataGridViewComboBoxColumn in a datagridview. When the user selects the value from comboboxNames in First row, based on the value seleted the comboboxNames in second row should be populated. and based on the second row comboboxNames selected item,  comboboxNames in third row should be populated and finally all the rows and its values  present in datagridview should be saved into database. how?

    Thanks in advence

    Tuesday, July 6, 2010 1:32 PM
  • Is your datagridview already connected to a data source?  , if  yes you cannot do it, because if you add a new element to a cell, that value should present in original data source which is bound to datagridview, else you will end up with series of ‘Formatting error’ .

     

    To avoid this you can perform updates like this.

    When new value added to comboboxcolumn1, add that value to datagridview’s data source using an insert command, do it for other comboboxcolumns.

    • Proposed as answer by Helen Zhou Friday, July 9, 2010 8:24 AM
    Wednesday, July 7, 2010 6:36 AM
  • Hi AristocratP,

    According to your need: the value of row2 of this column( say cell[1][0] for example ) is determined by row1 in the same column (cell[0][0]), row3 (cell[2][0]) is determined by row2 ( cell[1][0] ),etc. right? If I'm off base, please correct me.

    If so, why do you use combobox column? You just need combobox cell for the first row of the column, other cells in the same column can be replaced by textbox cell. To do the determinations , you can use "switch...case" for the cells of this column.

    What I mean is your requirement is doing automatic assignment on column, not on row! If on row, you can bind them to a datatable to perform the automation, but on columns :) seems not so convenient. 

    I think the following links will help you more on DataGridView:

    http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

    http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.displaymember(VS.80).aspx

    Bind DatagridviewComboboxcolumn

    http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database

    Best regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    • Marked as answer by Helen Zhou Tuesday, July 13, 2010 3:14 AM
    Friday, July 9, 2010 8:55 AM