I have a DataGridView, with several ComboBoxColumns in it. Is there a way to create an event, so that each time a ComboBoxColumncell is entered and an item selected, the event fires?
All I can figure out so far is this:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
MessageBox.Show("Amanda");
}
}
Which is not doing anything.
You're probably looking for the EditingControlShowing event.
See here for a similar question:
"SelectedIndexChanged" event in ComboBoxColumn on Datagridview
try this one.
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox nameComboBox = e.Control as ComboBox;
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
if (nameComboBox != null)
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
nameComboBox .SelectedIndexChanged -= (nameComboBox _SelectedIndexChanged);
nameComboBox .SelectedIndexChanged += (nameComboBox _SelectedIndexChanged);
}
}
}
private void nameComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
var rowindex = dataGridView1.CurrentCell.RowIndex;
if (dataGridView1[1, rowindex].EditedFormattedValue != null)
{
Consol.WriteLine(dataGridView1[1, rowindex].EditedFormattedValue.ToString());
}
else
{
//No value in cell
}
}
}
Related
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(polNumDataGridViewTextBoxColumn_KeyPress);
if (dataGridView1.CurrentCell.ColumnIndex == 3) //Desired Column
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.KeyPress += new KeyPressEventHandler(polNumDataGridViewTextBoxColumn_KeyPress);
}
}
}
private void polNumDataGridViewTextBoxColumn_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
The control in the gridview is not a TextBox. It is a DataGridViewTextBoxEditingControl
I made this working test (my control names and column indexes are different):
private void DataGridView2_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewTextBoxEditingControl tb) {
tb.KeyPress -= Tb_KeyPress;
tb.KeyPress += Tb_KeyPress;
}
}
private void Tb_KeyPress(object sender, KeyPressEventArgs e)
{
if (dataGridView2.CurrentCell.ColumnIndex == 1 &&
!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
But using data binding is usually the better approach and it fixes the problem automatically. I.e. when a cell is bound to numeric property or DateTime property is behaves accordingly.
I have a datagrid with a combobox, i want to get my value, i can get it but i dont know why, i get it 4 times ???
could someone help me ?
here my code :
private void dgvLocataire_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dgvLocataire.BeginEdit(false);
var ec = dgvLocataire.EditingControl as DataGridViewComboBoxEditingControl;
if (ec != null && ec.Width - e.X < SystemInformation.VerticalScrollBarWidth)
ec.DroppedDown = true;
if ((e.ColumnIndex != 3) && (e.ColumnIndex != 4))
{
dgvLocataire.Columns[e.ColumnIndex].ReadOnly = true;
}
dgvLocataire.CellValueChanged +=
new DataGridViewCellEventHandler(dgvLocataire_CellValueChanged);
//dgvLocataire.CurrentCellDirtyStateChanged +=
//new EventHandler(dgvLocataire_CurrentCellDirtyStateChanged);
}
private void dgvLocataire_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dgvLocataire.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dgvLocataire_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
string comboboxSelectedValue = string.Empty;
if (dgvLocataire.Columns[e.ColumnIndex].GetType() == typeof(DataGridViewComboBoxColumn))
comboboxSelectedValue = dgvLocataire.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
MessageBox.Show(comboboxSelectedValue);
}
The messagebox appears 4 times when i choose a value in combobox.
Thanks for your help
In dgvLocataire_CellMouseClick method, you are subscribing to dgvLocataire_CellValueChanged every time you click. Which means it can be called multiple times => MessageBox.Show(comboboxSelectedValue) is called multiple times.
You should subscribe to this event only once at the initialization of this form.
I change like this
private void dgvLocataire_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dgvLocataire.BeginEdit(false);
var ec = dgvLocataire.EditingControl as DataGridViewComboBoxEditingControl;
if (ec != null && ec.Width - e.X < SystemInformation.VerticalScrollBarWidth)
ec.DroppedDown = true;
if ((e.ColumnIndex != 3) && (e.ColumnIndex != 4))
{
dgvLocataire.Columns[e.ColumnIndex].ReadOnly = true;
}
}
private void dgvLocataire_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cb = e.Control as ComboBox;
if (cb != null)
{
cb.SelectionChangeCommitted -= new EventHandler(ComboBox_SelectedIndexChanged);
// now attach the event handler
cb.SelectionChangeCommitted += new EventHandler(ComboBox_SelectedIndexChanged);
}
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
string item = cb.Text;
if (item != null)
MessageBox.Show(item);
}
`
but now i dont get the messagebox to show ???
any help ?
Thanks
I tried to set value for DataGridViewButtonColumn cell in Datagridview.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dgv.Columns[e.ColumnIndex].Name.Equals("id")) {
e.Value = 14;
}
}
Then I try to get value after click event:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex.Equals(1) && e.RowIndex != -1)
{
// GET HERE
}
}
So, how to get value e.Value = 14 in event dataGridView1_CellClick?
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var dg = (DataGridView)sender;
dg[e.ColumnIndex, e.RowIndex].Value = 123;
}
I've a form with a DataGridView and a BindingNavigator with toolStripButton.
So I need to disable a BindingNavigator's ToolStripButton when any cell is selected and enable it when at least one cell is selected.
I've tried with CellClick event and SelectionChanged event but i can't solve this issue.
private void myDataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (myGridView.Columns[e.ColumnIndex].Name == "findValueDataGridViewTextBoxColumn"
|| myDataGridView.Columns[e.ColumnIndex].Name == "replaceValueDataGridViewTextBoxColumn")
{
myDropDownButton.Enabled = true;
}
else
{
myDropDownButton.Enabled = false;
}
}
Any idea how to do this?
Use index instead of name and DataGridViewCellCancelEventArgs instead of DataGridViewCellEventArgs.
Example
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
string msg = String.Format("Editing Cell at ({0}, {1})",
e.ColumnIndex, e.RowIndex);
//You go to your datagrid and identify the index of the replaceValueDataGridViewTextBoxColumn
//e.g Int ColIndex1 = replaceValueDataGridViewTextBoxColumn index 0 fro the first , 1 for second etc.
if(e.ColumnIndex = ColIndex1 || e.ColumnIndex = ColIndex2)
{
myDropDownButton.Enabled = true;
}
// this.Text = msg;
}
In your case
private void myDataGridView_CellEnter(object sender, DataGridViewCellCancelEventArgs e)
{
//get the column index of your columns
//Here you dont need to call grid name just e is enough
//Note you can try e.Name may be , but i didn't
if (e.ColumnIndex == colIndex1 || e.ColumnIndex == colIndex2)
{
myDropDownButton.Enabled = true;
}
else
{
myDropDownButton.Enabled = false;
}
}
or Try like below, put a break point and see the value of colName.
private void dataGridView1_CellMouseEnter(object sender,
DataGridViewCellEventArgs e)
{
string colName= (string)dataGridView1.Columns[e.ColumnIndex].Name
}
Please check Here and Here for full explanation.
You can use SelectedCells property of DataGridView:
if (myDataGridView.SelectedCells.Count > 1) // at least one cell selected
{
//do somethings
}
How to check if DataGridView cell contain number between 0 and 3 while entering value?
This is how I check if specific cell constains int number:
private void dgUpitnik_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.KeyPress -= new KeyPressEventHandler(ColumnOcjena_KeyPress);
if (dgUpitnik.CurrentCell.ColumnIndex == 2) //Desired Column
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.KeyPress += new KeyPressEventHandler(ColumnOcjena_KeyPress);
}
}
}
private void ColumnOcjena_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
But how to check inside KeyPress event that the number entered in cell is 0, 1, 2 or 3?
I've found solution, here it is:
private void dgUpitnik_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
dgUpitnik.Rows[e.RowIndex].ErrorText = "";
int newInteger;
if (e.ColumnIndex == 2)
{
if (dgUpitnik.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
if (!int.TryParse(e.FormattedValue.ToString(), out newInteger) || newInteger > 3)
{
e.Cancel = true;
dgUpitnik.Rows[e.RowIndex].ErrorText = "Ocjena mora biti u rasponu od 0 do 3!";
}
}
}
}
Maybe it will be usable for someone someday.