I've added a handler for the CellFormatting event on a DataGridView to modify the background color based on the content of the row.
It doesn't seem to be firing even as data gets inserted into the table. I added the event handler by doubleclicking in the IDE on the CellFormatting event which seemed to create the code properly.
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// this never gets called
MessageBox.Show("Event fired");
}
What could I be doing wrong?
I think you cannot use CellFormating event for your case. It occurs when the contents of a cell need to be formatted for display.
Try CellValueChanged event instead (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx)
Or
Select other appropriate event from http://msdn.microsoft.com/en-us/library/x4dwfh7x.aspx
You could try the RowValidated event:
private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
}
NOTE: This event will fire when you click on rows and when you close the form.
Related
How can I add an event handler for when a Devexpress TreeList selection changes? Here's what I have that isn't working:
window.nList.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(nList_SelectedItemChanged);
private void nList_SelectedItemChanged(object sender, DevExpress.Xpf.Grid.SelectedItemChangedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(nList.CurrentCellValue);
}
Are you using the multi-select mode? The SelectedItemChanged and SelectionChanged events aren't fired if the SelectionMode property is set to MultiSelectMode.None(default value).
Please use the CurrentItemChanged event instead when single-selection mode is active.
This event occurs after the focused row has been changed (e.g. row focus moves to another data row).
Just use TreeListControl.SelectionChanged event.
In XAML:
<dxg:TreeListControl x:Name="treeListControl1" SelectedItemChanged="treeListControl1_SelectedItemChanged" />
Or in c#:
treeListControl1.SelectedItemChanged += treeListControl1_SelectedItemChanged;
Event handler method:
void treeListControl1_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
{
MessageBox.Show(((YourClass)e.NewItem).SomeValue.ToString());
}
I have a Gridview. I and populating two dynamic text box for each cell inside it. User will enter the arriving time in first textbox and the arriving + 9 hours will be added and display in second textbox. I have written event handler where i am calculating exit time. the event handler is working fine but I need to event handler will fire for first Cell only. How to prevent event handler for rest of the textbox.
You didn't provide code but this is a general example.
private bool _isFirst = true;
private void CellEventHandler(object sender, EventArgs e)
{
if (!_isFirst) return;
// code
_isFirst = false;
}
You could also unbind the event handler
private void CellEventHandler(object sender, EventArgs e)
{
// your code here
textBox.Click -= CellEventHandler;
}
In the code below the SelectionChanged event is fired before the end of RowsAdded,how can I make the Event atomic?
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1];
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].Selected = true;
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow != null)
{
//Something
}
}
what should i do?
SelectionChanged is fired in the middle of handling RowsAdded because you are causing a SelectionChanged by changing the current cell within dataGridView1_RowsAdded. Adding a row doesn't cause both events to be fired -- you're causing the second event while handling the first one. (In fact, you're probably causing SelectionChanged twice, because both lines in the handler seem to change the selection).
If you don't want dataGridView1_SelectionChanged running while in the RowsAdded handler, you need to either temporarily unsubscribe from the event:
dataGridView1.SelectionChanged -= dataGridView1_SelectionChanged;
// change the selection...
dataGridView1.SelectionChanged += dataGridView1_SelectionChanged;
Or even better, re-design what you're doing inside the SelectionChanged handler so that it is appropriate for all instances of the event.
You can override the event you want to conrol and then put an if condition in the overriden event and control when it fires and when it does not.
mahdi
What is the difference between the CellClick event and the SelectionChanged event in the Windows Froms DataGridView control?
And when exactly is the selection changed event run: before the form load event or after?
The best reference for this sort of question is the MSDN DataGridView documentation.
For the CellClick event they say:
This event occurs when any part of a cell is clicked, including
borders and padding. It also occurs when the user presses and releases
the SPACE key while a button cell or check box cell has focus, and
will occur twice for these cell types if the cell is clicked while
pressing the SPACE key.
For the SelectionChanged event:
This event occurs whenever cells are selected or the selection is
canceled, whether programmatically or by user action. For example,
this event is useful when you want display the sum of the currently
selected cells.
The obvious difference is that the CellClick can fire even when the DataGridView selection does not change, for example with a right click or when clicking on the currently selected cell. Also the selection can change without a cell being clicked, for example when you change the selection programatically.
As for when exactly the selection changed event is run in relation to the form load event, when attached in the form constructor it is before (and several times at that!).
I just proved that to myself with the following code:
public Form1()
{
InitializeComponent();
MyBindingList<BackingObject> backing_objects = new MyBindingList<BackingObject>();
backing_objects.Add(new BackingObject{ PrimaryKey = 1, Name = "Fred", Hidden = "Fred 1"});
dataGridView1.DataSource = backing_objects;
this.Load += new EventHandler(Form1_Load);
dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
}
void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Load");
}
void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
Console.WriteLine("Selection Changed");
}
The output window reads:
Selection Changed
Selection Changed
Selection Changed
Load
Note that you can make the selection changed fire after the load event by attaching it during the DataBindingComplete event handler.
dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
}
Now in the output window you only see:
Load
And there is no selection changed output until the grid selection is changed (by for example a cell click)
I'm developed an application for the datagridview filtering . And i used the datagridview's dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
event for filtering.
But i want to handle it on the key press event for the datagridview cell. But i'm not getting that type of event.
the datagridview event should occure
on the each keypress..
So can anybody tell me that which event should i use for the datagridview?
please help me...
thanx
The DataGridView.KeyPress event will not be raised when the user types in a particular cell. If you want to be notified each time they press a key while editing content in a cell, you have two options:
Handle the KeyPress event that is raised directly by the editing control itself (which you can access using the EditingControlShowing event).
For example, you might use the following code:
public class Form1 : Form
{
public Form1()
{
// Add a handler for the EditingControlShowing event
myDGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(myDGV_EditingControlShowing);
}
private void myDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
// Ensure that the editing control is a TextBox
TextBox txt = e.Control as TextBox;
if (txt != null)
{
// Remove an existing event handler, if present, to avoid adding
// multiple handler when the editing control is reused
txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress);
// Add a handler for the TextBox's KeyPress event
txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
}
}
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
// Write your validation code here
// ...
MessageBox.Show(e.KeyChar.ToString());
}
}
Create a custom class that inherits from the standard DataGridView control and override its ProcessDialogKey method. This method is designed to process each key event, even those
that occur on the editing control. You can either handle the key presses inside of that overridden method, or raise an event of your own to which you can attach a separate handler method.