I am trying to determine if the user has selected a row to get the currently selected row. But when the form is initialized it calls the event.
Is there a way to check if the data grid view is being initialized? I thought about setting a flag and using RowPostPaint to set the flag.
Any other ways to ignore the select row event during initialization?
Maybe it helps to check if the DataGridView has the focus:
if (this.dataGridView.Focused)
{
// handle selection ...
}
On form initialization the control shouldn't have the focus but when the user manually changes the row selection it should have the focus.
And maybe you are using the wrong event to determine the row selection. Are you using "RowEnter"? I suggest you use "SelectionChanged" and then access the "SelectedRow" property.
It depends to which event you subscribe, I suppose it's SelectionChanged ?
If so - define some boolean variable (like dgvIsInitialized), set it false by default, then load data into datagridview datasource and - after loading - set it true.
And in SelectionChanged event do similar to this:
if (dgvIsInitialized)
MessageBox.Show("Selection changed");
Related
I have a column in my DataGridView which has the DataGridViewCheckBoxColumn as column template. When user checks the checkbox, I need to uncheck it programmatically if it fails some conditions. I have done this, but this uncheck logic only takes effect after I change the focus from the cell manually. I tried changing the focus programmatically but didn't work. Is there any solution for this?
I found the answer with little code changes. I used the cancel edit function of datagridview. For checkbox value change to be fired previously i was using this code inside
CurrentCellDirtyStateChanged event.
CommitEdit(DataGridViewDataErrorContexts.Commit)
Now i changed it to.
CommitEdit(DataGridViewDataErrorContexts.CurrentCellChange)
And called the
CancelEdit()
to roll back the changes.
Just set value of your DataGridViewCell.Value property to false (or true):
((DataGridViewCheckBoxCell)row.Cells[CheckBoxColumn.Index]).value = false;
Recently I had similar problem. Setting Cell Value on false or working with TrueValue and FalseValue (for DataGridViewCheckBoxCell) failed in my case.
Finally i manage to achieve what I wanted (unchecking if some condition fails) with simple DataGridView method:
dataGridView1.CancelEdit()
Hope it helps.
I have a DataGridView which I need to run a CellValidating event to ensure that only valid values are selected from a ComboBox. This is needed as the ComboBox contains dummy rows used to display the category, with the fields the user can select listed underneath each category.
Whilst I have the validation code working fine, there is an unwelcome side-effect that all values are being wiped from the row being validated. I have stripped the code in the Event handler down to this, and the issue still occurs:
private void dgvInformation_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridView dgv = this.dgvInformation;
DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
}
If I remove the
DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
line then the issue does not occur.
The DGV is unbound which I believe is causing the issue. As a test I have made a simple form and populated the DGV values unbound, and each time the CellValidating event fires that row is wiped out, but when I create a List<> and use that as the DataSource the values are not wiped out. Could this be a bug with unbound DGVs?
Many thanks
I have problems with the sentense: "to ensure that only valid values are selected from a ComboBox." Using a combobox should actually prevent wrong values beeing tipped in controls like textbox, why dont you just show valid values in the combobox, or validate after all sellections have been made if you need to validate a combination of comboboxes sellection, therefor you may need somthing like a Submit button to run the validation routine.
If you still think you have to validate the combobox after each sellection then you ahve to run the validation somehow on a SelectionChanged event of the combobox.
I need to put a Checkbox against each Row in a DataGridView. I am making use of DataGridViewCheckBoxColumn for the purpose. But here user is able to select more than 1 row. How to restrict the user from selecting multiple rows in a DataGridView?
You will have to do it yourself when the checkbox is checked.
If you want to respond immediately
when users click a check box cell, you
can handle the
DataGridView.CellContentClick event,
but this event occurs before the cell
value is updated. If you need the new
value at the time of the click, one
option is to calculate what the
expected value will be based on the
current value. Another approach is to
commit the change immediately, and
handle the
DataGridView.CellValueChanged event to
respond to it. To commit the change
when the cell is clicked, you must
handle the
DataGridView.CurrentCellDirtyStateChanged
event. In the handler, if the current
cell is a check box cell, call the
DataGridView.CommitEdit method and
pass in the Commit value.
Whichever method you use, when you handle the checkbox being checked, you will need to mark all the other checkboxes as unchecked. How you will do that depends on whether (and how) your DataGridView is data-bound.
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();
}
I have a typical requirement.
I have a datagridview with a combobox column(items loaded at design time). When a user selects an item from combobox, remaining rows gets updated in database based on the selectedItem and dgv gets refreshed.
Problem is the combo box will lose its current selection and goes to unselected state.
I want to retain the selected item even after dgv refreshed.
Could anyone help me out
Thanks in advance
Do you mean that you're using an unbound comboboxcolumn? If so, the value can't automatically be persisted when refreshing the datasource. You need to store the selected value before updating and setting it in code after refresh.
If your column is actually databound, the selected value is either not stored in the database or you have some data type problem.
Is the combobox there to let the user select a value for the field or do you use it as a way to execute a command on the record?
Do you have any code you can post?
Datagrid Combo-Box value will retain the string value but will refresh any integer values automatically.
Here is what you need to do :
-When populating Combo-Box value to just convert its value to toString().
-Also if you are setting a default select value also set it with string type.
-Your Combo-box will automatically retain the value selected even after refreshing.
:)
have a combobox in ur datagridview. Assign values to it using a bindingsource.
then write an eventhandler for the datagridviews "EditingControlShowing" event.
in that, remove had handler if any exists for the comboboxes Selectedindexchanged event. then add an event handler for the selectedIndexChanged event say "ComboBoxValueChanged"
in that "ComboBoxValueChanged",
DirectCast the sender to System.Windows.Forms.DataGridViewComboBoxEditingControl and get the selected value of it.
Now use it to compute any value you want.
you may wana refer to this
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx