Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > ComboBoxColumn edited value - how to access?
 

ComboBoxColumn edited value - how to access?

Hi,

I have a datagrid with 6 columns, first column is a combo box column binded to a different datatableand updates the keys in the datagridview datatable like normal. I also have two unbound columns in the datagridview which have their values set programatically - problem is, I need to update these values when the user selects a item from the combo box but I cant see a way of getting at the edited key value. I can get to the EditedFormatValue but not the 'EditedValue' (doesn't exist) - has anyone got a solution for this. I don't really want to have to commit changes to the database so I can calculate values.

Marlon
marlon_tucker  Friday, September 11, 2009 10:37 AM
If your requirement is to update the grid for every change in combobox selection then write code to update the grid in combobox_selectedchange event,so it will fill the grid for every change in comboxboxselection.

Saving the data to database is optional depend on your requirement


Hope this will help you...:)
Rohini Chavakula  Saturday, September 12, 2009 5:13 AM

Hi,

The value will not save to the database unless you update the changes to the database. If you bind a datagridview control to a datatable which data is got from database, you have got the datagridview bind to the datatable instance. Any Changes of the datatable will not impact the database.

If currency ComboBoxColumn is databound and set both DisplayMember and ValueMember property, you can get the display text by DataGridViewCurrentCell.EditedFormattedValue property, valuemember by DataGridViewCurrentCell.Value property.

Set the cell value by changing the cell.value property. If this combobxcolumn is databound, you need to make sure the value is contained in the datasource, otherwise, there will throw an error.

If you need implement other action base on user selection on the comboboxcell, you need to handle ComboboxCell.SelectedValueChanged Event. Please refer to the following FAQ.

17. How do I handle the SelectedIndexChanged event?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq17

I also suggest you to check the currentCell.ColumnIndex and remove the selectedIndexChanged event handler at cellEndEdit event. This will avoid cells in other datagridviewComboBoxColumn invoke the selectedIndexChanged event.

ComboBox cb;

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

cb = e.Control as ComboBox;

if (dataGridView1.CurrentCell.ColumnIndex == 0)

{

if (cb != null)

{

// first remove event handler to keep from attaching multiple:

cb.SelectedIndexChanged -= new

EventHandler(cb_SelectedIndexChanged);

// now attach the event handler

cb.SelectedIndexChanged += new

EventHandler(cb_SelectedIndexChanged);

}

}

}

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

cb.SelectedIndexChanged -= new

EventHandler(cb_SelectedIndexChanged);

}

More information:

DataGridViewCell.Value Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.value.aspx

DataGridViewCell.EditedFormattedValue Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.editedformattedvalue.aspx

27. How do I have a combo box column display a sub set of data based upon the value of a different combo box column?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq27

If anything unclear, please feel free to tell me.

Best regards,

Ling Wang


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.
Ling Wang  Monday, September 14, 2009 9:27 AM
Sample code to fill the grid for every change in combobox selection:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim cn As New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MDB file path")
        cn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand("Select * from salesentries where id = " & Val(ComboBox1.Text) & "", cn)
        sda = New OleDbDataAdapter(cmd)
        Dim ds As New DataSet
        sda.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)

    End Sub
http://www.codeproject.com/KB/vb/DataGridViewEditForm.aspx
Rohini Chavakula  Monday, September 14, 2009 2:54 PM
If your requirement is to update the grid for every change in combobox selection then write code to update the grid in combobox_selectedchange event,so it will fill the grid for every change in comboxboxselection.

Saving the data to database is optional depend on your requirement


Hope this will help you...:)
Rohini Chavakula  Saturday, September 12, 2009 5:13 AM

Hi,

The value will not save to the database unless you update the changes to the database. If you bind a datagridview control to a datatable which data is got from database, you have got the datagridview bind to the datatable instance. Any Changes of the datatable will not impact the database.

If currency ComboBoxColumn is databound and set both DisplayMember and ValueMember property, you can get the display text by DataGridViewCurrentCell.EditedFormattedValue property, valuemember by DataGridViewCurrentCell.Value property.

Set the cell value by changing the cell.value property. If this combobxcolumn is databound, you need to make sure the value is contained in the datasource, otherwise, there will throw an error.

If you need implement other action base on user selection on the comboboxcell, you need to handle ComboboxCell.SelectedValueChanged Event. Please refer to the following FAQ.

17. How do I handle the SelectedIndexChanged event?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq17

I also suggest you to check the currentCell.ColumnIndex and remove the selectedIndexChanged event handler at cellEndEdit event. This will avoid cells in other datagridviewComboBoxColumn invoke the selectedIndexChanged event.

ComboBox cb;

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

{

cb = e.Control as ComboBox;

if (dataGridView1.CurrentCell.ColumnIndex == 0)

{

if (cb != null)

{

// first remove event handler to keep from attaching multiple:

cb.SelectedIndexChanged -= new

EventHandler(cb_SelectedIndexChanged);

// now attach the event handler

cb.SelectedIndexChanged += new

EventHandler(cb_SelectedIndexChanged);

}

}

}

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)

{

cb.SelectedIndexChanged -= new

EventHandler(cb_SelectedIndexChanged);

}

More information:

DataGridViewCell.Value Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.value.aspx

DataGridViewCell.EditedFormattedValue Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.editedformattedvalue.aspx

27. How do I have a combo box column display a sub set of data based upon the value of a different combo box column?

http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq27

If anything unclear, please feel free to tell me.

Best regards,

Ling Wang


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.
Ling Wang  Monday, September 14, 2009 9:27 AM
Sample code to fill the grid for every change in combobox selection:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim cn As New SqlConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=MDB file path")
        cn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand("Select * from salesentries where id = " & Val(ComboBox1.Text) & "", cn)
        sda = New OleDbDataAdapter(cmd)
        Dim ds As New DataSet
        sda.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)

    End Sub
http://www.codeproject.com/KB/vb/DataGridViewEditForm.aspx
Rohini Chavakula  Monday, September 14, 2009 2:54 PM

You can use google to search for other answers

Custom Search

More Threads

• DataSourceUpdateMode.OnPropertyChanged - How is it supposed to work?
• Navigators addnewitem
• DataGridView and Lookup ComboBox
• Using a dataview in a child table loses the parent navigation links
• Vb 2005 databinding
• db Update problem.
• N00b here, How to get SQL2005 row data
• Binding to DataRow / DataRowView problem
• Gridview problem and Question
• How to move rows in a DataGridView bounded to a datatable?