I'm currently about to design a project that uses a DataGridView. As one of the events I would like it to perform a task upon double-clicking on a row.
Looking around I can find examples of creating your own event, however I notice that DataGridView has no property or definition for double-clicking a row, and was wondering how I could specify this myself?
There's a CellMouseDoubleClick event that, in contrast to the "normal" DataGridView's mouse double click event, fires only when the user actually double-clicks the cell of a row.
About the question on how to do it yourself: You could try to derive a new class from DataGridView, attach the CellMouseDoubleClick event internally and just fire a new event, passing the clicked row. For example:
private void DataGridView1_CellMouseDoubleClick(Object sender, DataGridViewCellMouseEventArgs e)
{
// Determine the row the clicked cell belongs to
...
// Fire a new event for that row
...
}
Related
My project is a Windows Forms project and I have a DataGridView.
DataGridView has a column that is an editable CheckBoxColumn.
I'm using CellBeginEdit event to make decision the CheckBox is checked or unchecked.
There is no problem when I clicked first, but when I clicked second, third or more than once, CellBeginEdit event not firing.
From the comments you stated that you are not navigating to another cell after that first click.
But after the first click, if I focus another editable cell and click again the combobox cell, event is firing
This is by design. The ComboBoxCell enters Edit mode on focus. While the cell maintains focus, CellBeginEdit will not trigger. You can bypass this behavior by calling EndEdit(), like so:
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
this.dataGridView1.EndEdit();
}
}
So I have a datagridview that auto generates columns based on the data I pull from a database. Now I also have a cell click event for the datagridview, however this event only fires if the column header is click. It will not fire if I click any of the cells populated inside the datagridview, why?
// This is in the designer code
this.dgvScheduled.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvScheduled_CellClick);
// This is in my main forms application
private void dgvScheduled_CellClick(object sender, DataGridViewCellEventArgs e)
{
ChangeIsNoShow(e);
}
With the event subscribed in the designer code and the event in the application, it does not make sense why it does not execute when I click on cell that is not the header cell.
There are 2 distinct UI concept: CellValueChanging vs CellValueChanged.
Quoted from DevExpress Documentation:
The CellValueChanging event is raised each time the edited value is being changed (the user types or deletes a character, chooses a value from the dropdown list, etc.).
On the other hand, CellValueChanged is raised when user had done cell editing by hitting enter or clicking outside of the active cell.
Now my problem is, I have a combobox type column, and I want to always skip the CellValueChanging and make the change final. The current behavior is when user select an item from the combobox, the change doesn't take effect immediately(e.g, the view will not be resorted as per the change). The change is not accepted until user hit enter or click outside of the cell.
---------------07/26/2013 2:25PM update---------------
Sorry that my previous question description misled everybody, so I'll rephrase it.
Here is the current behavior:
Pic1: beginning state. Rows are sorted alphabetically by Target.
Pic2: Change the 2nd row value from B to D
Pic3: After a single mouse left click on item D, the dropdown disappears and the cell value changes to D. However, the rows are not resorted
Pic4: After clicking outside the cell or hitting enter, the rows are resorted.
What I want to achieve is to skip the step in Pic3. In other word, I want any changes committed immediately, without having to make an extra kepboard or mouse click.
What I am showing here is a simplified example of my application. I can't move my CellValueChanged event handler logic to CellValueChanging or EditValueChanged because it would cause some errors.
You want to make some UI change once user edit the cell value, right? I think generally you have to give up handling the CellValueChanged event, but use the CellValueChanging Event instead:
pseudocode:
HandleCellValueChanging(object sender, CellValueChangingEventArgs e)
{
// Get underlying object
// and write the value direct into the object
var rowObj = (YourType)gridview.GetRow(gridView.FocusedRowHandle);
rowObj.TargetField = e.NewValue; // e.Value or e.NewValue, not sure
// Then Do your UI effect
}
Another option is to use RepositoryItemEditor and handle the key-up and/or Mouse-up event instead.
If you want to to catch the moment when an end-user changes some value in gridview's cell editor (e.g. select item in combobox), please handle the EditValueChanged event of a corresponding RepositoryItem. To immediately post this value (make the changes final), you need to call the PostEditor method of a corresponding container:
repositoryItemComboBox1.EditValueChanged += repositoryItemComboBox1_EditValueChanged;
//...
void repositoryItemComboBox1_EditValueChanged(object sender, EventArgs e) {
gridView1.PostEditor();
// do something else if it needed
}
Related example: E3234 - How to update a row's style immediately after a an inplace editor's value is changed
I have follwoing feew questions on XtraGrid (Dev Express).
How to enable editing the cell by double clicking on it? by defalut XtraGrid permits cell editing if we just click on it. I dont want this to be happen.
How do I get the column/Row information which is edited?. Is there any event like AfterRowEdit() or AfterCellEdit()?
Thanks,
Omkar
1 You could capture the click event and enable the editor if its clicked twice in a short timespan.
2 To get column/row information I would add a special editor to the column and capture its events.
Try setting the OptionsBehaviour.EditorShowMode property of your view to MouseDownFocused. That way the user has to focus the cell first, and the editor will show up only on second click.
Have a look at view's ValidateRow event, or if you need any processing BEFORE editing a row, you could use the view's ShowingEditor event, and grab the actual row by the view's FocusedRowHandle property.
Disable the gridview editor.
capture DoubleClick event on the gridcontrol.
And in this event enable the gridview editor
===========
Bind each column to a repository item
Go to a column and find the columnedit property.
Set a repository item to that column.
Then assign the validating event to the repository item.
Code:
private void your_gridcontrol_double_click(object sender, EventArgs e)
{
GridHitInfo hit = your_gridview.CalcHitInfo((e as MouseEventArgs).Location);
if (hit.InRow)
{
}
}
I'm using a DGV to show a list of images with text captions as a picklist. Their must always be a one and only one selection made in the list. I can't find a way to prevent the user from clearing the selection with a control-click on the selected row.
Is there a property in the designer I'm missing that could do this?
If I have to override the behavior in the mouse click events are there other ways the user could clear the current selection that need covered as well?
Is there a third approach I could take that's less cumbersome than my second idea?
The easiest way is to catch the SelectionChanged event and check to see if the user has unselected all rows. If so, reselect the previously selected row. Essentially, you're intercepting their action and switching the selection back. Something like this (code untested but you will get the idea):
DataGridViewRow last_selected_row;
private void dgv_SelectionChanged(object sender, EventArgs e)
{
if (dgv.SelectedRows.Count == 0)
last_selected_row.Selected = true;
else
last_selected_row = dgv.SelectedRows[0];
}
Depending on your application, it might be better to store the row index rather than a reference to the row itself. Also be sure to initialize last_selected_row and update it if you delete any rows.
Any other controls that hook the SelectionChanged event will need to safely handle the case that no rows are selected, in case they fire before the event that switches it back. They can just return immediately though, safe in the knowledge that SelectionChanged will fire again momentarily.
You could also subclass DataGridView and override the OnSelectionChanged method. Then you could reselect the last selected row before the event fires (it will fire when you call base.OnSelectionChanged).
A DGV got a property called multiselect, if you set it to false only one cell/row can be selected at a time.
Just handle the DataBindingComplete event of the datagridview like this:
private void datagridview1_DataBindingComplete(System.Object sender, System.Windows.Forms.DataGridViewBindingCompleteEventArgs e)
{
datagridview1.ClearSelection();
}