I have a DataGridView with a binding source to an interface that has a bool. When I click the checkbox that is created in the grid, nothing happens. The value is not set and the box does not get checked.
However, and this gets very case specific, if I set a binding source to a completely different control in the same view then the checkbox in the first control is possible to click. The downside to this is that every DataGridView when loaded has a large portion of it that is completely black.
sorry I have no point to make this as a comment.
anyway did you check that the column of the DataGridView is set as readOnly = false, or enable = true.
check it also in run time, make a break point before and after doing the binding and see how your grid is set up.
Also if you can give some code maybe will see some other issue that can make this happen.
good luck
Related
Before starting to hunt down my Visible False/True issue:
How stable is setting the Visible property of non-automatic columns in
a WinForms DataGridView?
I've had had enough issues with strange artefacts in WinForms (for instance after all the years of their existing, fiddling around with FlowLayoutPanel and TableLayoutPanel at design time still gets the Visual Studio designer upset, changing the ForeColor on a TextBox doesn't always work, etc) so I am used to question the framework.
In my specific case, I set the Visible property of the two last columns on a DataGridView (all columns at design time, bound data is a collection of in memory objects) both in a backgroundWorker_RunWorkerCompleted and in a myCheckBox_CheckedChanged event.
Observations:
in the backgroundWorker_RunWorkerCompleted, setting Visible = False hides the column, but later setting Visible = True doesn't reveal it
in the myCheckBox_CheckedChanged, setting Visible = myCheckBox.Checked has no effect at all (does not hide or reveal)
Before digging deeply into this, and getting an Short, Self Contained, Correct Example out: how stable should this normally be?
My personal experience with this is that visibility of controls like bound DataGridViews can be temperamental. I would use the Update() method, and this may resolve the visibility issue. Failing that an this.Update() on the form and eventually a full form refresh using this.Refresh().
As far as the CheckBoxs visibility goes if the CheckBox is bound to some underlying data source, this could prevent such an update. However, doing
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
checkBox.Visible = checkBox.Checked;
}
should be fine. I hope this helps.
I have an ObjectListView which is essentially a wrapper around the standard .NET ListView. My problem is that I cannot figure out the correct sequence of method calls to add a new object to the control, scroll the control to ensure that the object is visible, and select the object. Below, is my code to achieve this. Everything works but, for some reason the background color for the selected item/object does not show up until I click on or re-size one of the columns. I'm not sure if the control is not being focused or what.
// objectListViewItems is of type BrightIdeasSoftware.ObjectListViewItems
objectListViewItems.AddObject(e.InsertedItem);
objectListViewItems.Refresh();
objectListViewItems.Focus();
objectListViewItems.EnsureModelVisible(e.InsertedItem);
objectListViewItems.SelectedObject = e.InsertedItem;
objectListViewItems.Focus();
The code below updates an item in the ObjectListView and works just fine. Not sure what I'm doing wrong above...
objectListViewItems.RefreshObject(itemToEdit);
objectListViewItems.Focus();
objectListViewItems.SelectObject(itemToEdit);
This should work like you proposed (I did this on several occasions). However, calling Refresh() and the second Focus() is unnecessary. Also I would rather use SelectObject() than the SelectedObject property.
Like this:
objectListView.AddObject(newItem);
objectListView.Focus();
objectListView.EnsureModelVisible(newItem);
objectListView.SelectObject(newItem);
Also, make sure that there is no code executed afterwards, that may cause another control to get the focus.
To narrow down what's happening, you could try setting
objectListView.HideSelection = false;
As for the normal ListView, this ensures that the current selection stays visible (but "grayed" out), even if the control loses focus.
Please post the complete OLV configuration (from InitializeComponent()) if you used the designer. Maybe there is some weird constellation causing this.
I have 2 grids. When user is doing edits in one grid, I want to disable the other grid from getting focus, or atleast other grid from changing its foccussed row.
Example:
Parent grid, children grid.
If user is making edit in children of particular parent. I want to prevent user from suddenly shifting focus to different parent row. how to do that ?
The only ways you can prevent a control receiving focus is to change it's Enabled or Visible properties.
Simply changing the parent's Enabled property to "false" (e.g. for the CellBeginEdit) and then back to "true" (e.g. in the CellEndEdit event) will prevent the user from selecting a new parent row.
There are a lot of ways to do this but thats how I would do it (just a concept so ignore if any property does not match):
Create a property called mode and then use enumeration to set it to Edit or None etc.
Suppose you have GridViewParent and GridViewChild. In the FocusedRow event of child grid, at the start of all code, set the value of mode to Edit and at the end of all code in the event set it to None.
Then in the FocusedRow event of parent grid check whether the mode value is edit or not, if it is edit then use e.Cancel or something to get out of the focused event of the parent.
Now if you let me know exactly which grid are you using I might send you the code.
In short, my problem is that the WPF DataGrid won't let me select anything other than the first row. When I first fill it with data, no row is selected. Next, no matter what row I click on, the first row lights up. When I bind the SelectedItem property using OneWayToSource, I see that the correct row was indeed selected. If I choose to click again on a different row, nothing happens: the UI remains stuck on the first row and the SelectedItem property retains the previously correct value. At this point, I have to ctrl-click the first row to deselect things. This allows me to repeat the above situation.
In other words:
- the UI seems to be out of sync with what is actually selected.
- Ctrl-click is required to de-select the selection.
- Ctrl-click must be executed on the first row even if the SelectedItem property indicates that a different row is selected.
I understand this is bizarre behavior. I've tried to duplicate it in a separate project with no success. As such, I'm just throwing this out there to see if anyone has any ideas why it might be acting this way?
The only things that I have not duplicated in my separate project is the use of MEF for the View/ViewModel hookup. Everything else is the same.
EDIT: I just replaced said DataGrid with a ListBox and am experiencing the same problem. I'm using Snoop to try to figure out what might be applied to the control that would change its behavior so much.
My problem was that the objects that I was adding to the DataGrid and ListBox had overridden Equals() and GetHashCode() functions. These really screwed up the way both controls rendered which item was selected, therefore explaining the SelectedItem issue. In the end, all the problems were solved by simply removing/fixing those overrides.
Woot!
I am trying to set a DataGridViewTextBoxCell.Value in formLoad, which kind of works, I can set it, but nothing is displayed.
DataGridViewTextBoxCell cel = (DataGridViewTextBoxCell)gvAirSegment.Rows[rowNumber].Cells[0];
cel.Value = "TEST";
This works, on button click, but does not work when its within a Form_Load. I have tried Refresh(), RefreshEdit(), InvalidateCell nothing seems to work, my guess is that its not fully created yet? even though I am doing this after I add all my items to the gridview... any help?
A DGV will not go into edit mode until it is visible. OnLoad is too soon, the form isn't visible yet. You could use the OnShown() method instead, it runs right after the form and all controls are painted for the 1st time. Or you could just directly assign the cell, no need to jump through the DataGridViewTextBoxCell hoop.