I'm having some trouble with VisualStudio 2010 C# Winforms.
I have created a DataGridView with an unbound column that is of type DataGridViewComboBoxColumn. The column works fine, except unlike a normal ComboBox, I can't seem to type in just any value. I am forced to pick a value from the list.
Is there a property I need to set or another type I can use that will allow me to enter any value in the cell in addition to providing a list to pick a value from?
Thanks!
I don't think there is a property that will allow this, but I found an answer here that worked with a small modification.
Try adding the following 2 event handlers, here assuming a column named comboBoxColumn:
private void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
ComboBox c = e.Control as ComboBox;
if (c != null) c.DropDownStyle = ComboBoxStyle.DropDown;
}
private void dataGridView1_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == comboBoxColumn.Index)
{
object eFV = e.FormattedValue;
if (!comboBoxColumn.Items.Contains(eFV))
{
comboBoxColumn.Items.Add(eFV);
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = eFV;
}
}
}
DataGridViewComboBoxColumn is designed rather for select from possible values - not for typing data. If you want to add any data, you should do it programmatically for desired DataGridViewComboBoxCell:
((DataGridViewComboBoxCell)dataGridView1[0,0]).Items.AddRange(new string [] {"A","B","C"});
Related
I have a View Model in which my data is stored to be used on the presenter and window. However, I now need to get (when pressing a button) the current row's results on the grid into my view model. I first used the mouse double click event, but the requirements changed from that to a button on the form:
private void dgvResults_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (this.colOrderNo.Index != e.ColumnIndex || e.RowIndex < 0) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) this.dgvResults.Rows[e.RowIndex].DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
}
That's my old code. Now I need that here:
private void btnAuditLog_Click(object sender, EventArgs e)
Not sure how to refer to the RowIndex without the DataGridViewCellMouseEventArges event. I tried the CurrentRow property, but it doesn't do the job.
Just another question on the fly, if someone could assist with that as well, how do I get a value from one form to another, spesifically a properties value. It's not on the grid, in a control. It's basically just public string order that's all. I set the value on one form, then need it for query filtering on another. Let me know if you need anything else.
You want to use the SelectedRows property of the DataGridView. It tells which row is selected. In this case, you seem to want the first selected row (since you have a single property called this.AuditOrderKey).
The column order no to longer matters in this context.
if (dgvResults.SelectedRows.Count == 0) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) dgvResults.SelectedRows[0].DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
If you don't have selected rows, CurrentRow is an option:
if (dgvResults.CurrentRow == null) return;
var auditOrder = (PurchaseOrdersTrackingViewModel) dgvResults.CurrentRow.DataBoundItem;
this.AuditOrderKey = $"{auditOrder.OrderNo}|{auditOrder.ItemNo}|{auditOrder.WarehouseId}|{auditOrder.ItemSequence}";
Use the SelectedRows property of the DataGridView. Assuming that you set your DataGridView as MultiSelect = false; and SelectionMode = FullRowSelect;
if (dgvResults.SelectedRows.Count > 0)
{
dgvResults.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
}
In your button click event, it will look like this. (Assuming that your button name is btnEdit)
private void btnEdit_Click(object sender, EventArgs e)
{
if (dgvResults.SelectedRows.Count > 0)
{
string selectedValue = dgvResults.SelectedRows[0].Cells["yourColumnName"].Value.ToString();
}
}
i have an combobox cell and i wanna convert to textbox cell after select item from the same combobox cell and convert to textbox but the problem when using
[1, DataGridView1.CurrentRow.Index] = new DataGridViewTextBoxCell()`
the cell not change until click on another cell although i'm using Datagridview1.refresh()
the first picture (when select from combobox then it should be changed )
the second picture ( after click an another cell )
DataGridViewComboBoxCells are notoriously obtuse when it comes to such a simple thing as simply and fully accepting a selected value..
This seems to work for me:
ComboBox editComboBox = null;
private void DataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox && e.ColumnIndex == 1)
{
editComboBox = (ComboBox)e.Control;
editComboBox.SelectionChangeCommitted -= editComboBox_SelectionChangeCommitted;
editComboBox.SelectionChangeCommitted += editComboBox_SelectionChangeCommitted;
}
}
void editComboBox_SelectionChangeCommitted(object sender, EventArgs e)
{
DataGridView1[1, dataGridView5.CurrentRow.Index] = new DataGridViewTextBoxCell();
DataGridView1[1, dataGridView5.CurrentRow.Index].Value =
editComboBox.SelectedItem.ToString();
DataGridView1.EndEdit();
DataGridView1[1, dataGridView5.CurrentRow.Index]cell1.Selected = false;
editComboBox = null;
}
Note that calling EndEdit allegedly can raise an exception if no error handler is found. I don't have one and I still don't get an error. Not sure what the docs mean; maybe that it may raise an error if the data are in fact invalid..
Also note that with this design the user can not easily correct an edit, unless you provide a way to restore the dropdown..
I have added a DataGridViewComboBox to a bound DataGridView (grdBOOK), the DataGridViewComboBox will replace column 3 to allow for user selection. I'm struggling to set the default of the DataGridViewComboBox equal to the value of column 3 so user selection is not required if the value is correct.
I pulled the code below from the net, but I get an error:
DataGridViewComboBoxCell value is not valid.
I thought a ComboBox cell could be treated as a normal DataGridView cell, but (see code below) an error is generated when a string is added to the ComboBox column? I've trawled the net and SO for a few days but nothing works, any suggestions please?
public void BOOK_COMBO2()
{
DataGridViewComboBoxCell cb_cell = new DataGridViewComboBoxCell();
DataGridViewComboBoxColumn cb_col = new DataGridViewComboBoxColumn();
// Contract field
cb_col.Items.AddRange("YEARLY", "MONTHLY", "");
cb_col.FlatStyle = FlatStyle.Flat;
cb_col.HeaderText = "newCONTRACT";
cb_col.Width = 50;
cb_col.ValueType = typeof(string);
// Add ComboBox and test
grdBOOK.Columns.Insert(5, cb_col);
grdBOOK.Rows[14].Cells[4].Value = "zzz"; // No error adding string to normal dgv column
grdBOOK.Rows[14].Cells[5].Value = "xxx"; // Error adding string to dgvcombobx column
//copy old values to new combobox and set as default
foreach (DataGridViewRow item in grdBOOK.Rows)
{
item.Cells[5].Value = item.Cells[3].Value;
}
//hide original column
grdBOOK.Columns[3].Visible = false;
}
After more research on the net, IMHO using a ContextMenuStrip is a better method of achieving this. Link here. A ContextMenuStrip has better methods, events, properties etc. I hope this helps others looking for a solution.
private void dataGridView1_DataError(object sender,
DataGridViewDataErrorEventArgs e)
{
// If the data source raises an exception when a cell value is
// commited, display an error message.
if (e.Exception != null &&
e.Context == DataGridViewDataErrorContexts.Commit)
{
MessageBox.Show("");
}
}
private void Form1_Load(object sender, EventArgs e)
{ dataGridView1.DataError +=
dataGridView1_DataError;}
I am trying to create a datagridComboBoxcolumn in winforms with Suggestions Based on Loose Character Search similar to Easycomplete combobox. but I want this as Datagridview combobox.
I have created a grid with Datagridviewcombobox column and used autocomplete but it will search only from first characters. I want loose search. I used
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewComboBoxEditingControl)
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
}
}
Please provide me a solution to create this type of datagridviewcombobox.
For that you have to create a custom DataGridiVew control. This is not a one line code or not a single class code. you have to make several classes for that.
public class MyDgv : DataGridView
{
....
}
And also create some classes like DataGridViewComboBoxColumn, DataGridViewComboBoxCell, DataGridViewEditingComboBoxControl
There is a tutorial how to create custom column in datagridview on msdn
I am very new to C#, and I really am stuck. I will try to be concise.
I have some ComboBox (in the same column) in DataGridView, and I want users to be able to type in the ComboBox. I got it to semi-work...
*Problem 1:
When I type in it, even though what I typed is added to the list of selections in the drop down, the ComboBox goes blank and I have to re-select the value I just entered. How can I make it so the value I type stays as the selection and not go blank.
*Problem 2:
Is there a way I can make certain ComboBox not editable in the same column? My code seems to make all of my ComboBox user editable. How can I make some ComboBox exceptions?
Thanks in advance, so much, if you could help!
Here are the codes:
private void dm_dgview_add_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control.GetType() == typeof(DataGridViewComboBoxEditingControl))
{
DataGridViewComboBoxEditingControl combo = e.Control as DataGridViewComboBoxEditingControl;
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.TextChanged += new EventHandler(combo_TextChanged);
}
}
void combo_TextChanged(object sender, EventArgs e)
{
dm_dgview_add.NotifyCurrentCellDirty(true);
}
private void dm_dgview_add_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridViewComboBoxCell cell = dm_dgview_add.CurrentCell as DataGridViewComboBoxCell;
if (cell != null && e.ColumnIndex == dm_dgview_add.Columns[1].Index)
{
if (!cell.Items.Contains(e.FormattedValue) && e.FormattedValue != null)
{
cell.Items.Add(e.FormattedValue);
}
}
}
Please help, I greatly appreciate this!
Do you need to set cell.Items.SelectedItem = e.FormattedValue in the cell validating event?