Need to create new row in DataGridView

G

Guest

I have a DataGridView on my Windows form with one ComboBox column. I have set
the ComboBox to use the DropDown style, through the EditingControlShowing
event handler:

private void dgvSentences_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cbo = e.Control as ComboBox;
if (cbo != null) cbo.DropDownStyle = ComboBoxStyle.DropDown;
}

And I have setup whatever the user has typed to be stored in the dropdown
list through the CellValidating event handler:

private void dgvSentences_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
object eFV = e.FormattedValue;
DataGridViewComboBoxCell cbc = dgvSentences.CurrentCell as
DataGridViewComboBoxCell;
if (cbc != null && !cbc.Items.Contains(eFV))
{
cbc.Items.Insert(0, eFV);
if (dgvSentences.IsCurrentCellDirty)
dgvSentences.CommitEdit(DataGridViewDataErrorContexts.Commit);
cbc.Value = cbc.Items[0];
}
}

This parts works fine. What I would like to do is have a new row added at
the end of the list as soon as a user starts typing text into the cell, like
a DataGridViewTextBoxCell would do. If I choose an item from the DropDown
list, it will automatically create the new row. If I start typing in the
ComboBox cell, it does not create a new row for me to start a new entry. How
would I handle this?
 
R

Richard Lewis Haggard

The first thing that occurred to me was to intercept the event from the
combo's edit that is triggered when the edit's data changed. Propagate this
back to your 'control has changed' data member. I believe that the combo
will fire a TextChanged event every time anything changes within the combo's
edit field.

When the DataGridView is notified that something has changed in the new row,
it will automatically generate another row. This event is DataGridView's
EditingControlDataGirdView.NotifyCurrentCellDirty( true );
 
O

office

Hi David,
You must tell the datagridview about changing some cell values by
calling NotifyCurrentCellDirty(true). Then your datagridview should
have created a new row. Hope this helps.
 
G

Guest

Thanks for the reply. That did the trick. I was going over every event and
method in the DataGridView class documentation (and web searching), but just
couldn't find the answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top