How to make an Edit check only change status with double click - c#

I have a gridview and a repositorycheckedit column, I want it to change its value only when it is double-clicked, and prevent it from changing with a single click.
thanks

I think you can achieve this in the following way:
Make the column read-only. This will disable the native behavior of allowing a click to edit the value.
colCheck.OptionsColumn.Readonly = true;
Add an in-place repository item of a check box and assign it to the column. If this column is a boolean, this is normally unnecessary (check is the default control type for a boolean), but this will help for the next step
Fire the double-click event on the repository item and alter the check property as follows:
~
private void repositoryItemCheckEdit1_DoubleClick(object sender, EventArgs e)
{
Foo f = gridView1.GetFocusedRow() as Foo;
f.Check = !f.Check;
gridView1.RefreshData();
}
Let me know if that helps.

Related

How do I alter the value being updated in a ListView?

I can't seem to find an answer to this, maybe I'm not using the correct terminology.
I have a ListView that is editable, I want it so that if a user clicks on Edit and then Update, that the field is updated with the value from another textbox, not the field they are editing.
The reason for this is that I have a Colour Picker that alters the value of a textbox, when they click Update I want this value to be the updated value.
I guess I utilise the ItemUpdating event, but I don't have much in the way of code because I'm pretty lost. I have this so far:
protected void ListView2ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
var selectedItem = ListView2.Items[ListView2.EditIndex];
// I have no idea what to put here
something = ColourChosen.Value;
}
Here is an image that I hope will make what I'm trying to do a little more understandable:
If any one could point me in the right direction of any examples, that would be much appreciated.
Although this doesn't answer my initial question this does what I want to happen.
What I should be doing is altering the database that ListView is attached to.
I use this code:
protected void ListView2ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
using (var myEntities = new i96X_utilEntities())
{
var myPlotColour = (from plotC in myEntities.PlotColours
where plotC.ID == selectedID
select plotC).Single();
myPlotColour.PlotColour1 = ColourChosen.Value;
myEntities.SaveChanges();
}
}
So, even though I have no idea how to intercept a field being updated in a ListView, in this example I don't need to.

Hide the Surrogate key in LookUpEdit

I am using the LookUpEdit control. I bound two columns, a primary key (GUID) and a human readable string. I only want people to see the human readable string, but I want to preserve the relationship between the string value and it's primary key value.
I can't seem to hide the primary key from displaying on dropdown. I've tried the DevExpress forum and their proposed solution does not work for me. So I tried hooking into the ListChanged event. That is not working either.
private void DataBind()
{
messageTypeCbB.ListChanged += new ListChangedEventHandler(messageTypeCbB_ListChanged);
messageTypeCbB.Properties.DataSource = viewModel.SomeNoteTypes.ToArray();
//another attempt at hiding the columns. This fails too.
//messageTypeCbB.Properties.ForceInitialize();
//messageTypeCbB.Properties.PopulateColumns();
//messageTypeCbB.Properties.Columns[0].Visible = false;
messageTypeCbB.Properties.DisplayMember = "NodeType";
messageTypeCbB.Properties.ValueMember = "SomeNoteType_ID";
fromTxt.Text = viewModel.From;
dateTimeDd.DateTime = viewModel.Date;
}
void messageTypeCbB_ListChanged(object sender, ListChangedEventArgs e)
{
//For whatever reason this won't hide the column
(sender as DevExpress.XtraEditors.LookUpEdit).Properties.Columns[0].Visible = false;
}
How can I hide the surrogate key? (Hide ValueMember display only DisplayMember)
DevExpress v11.1.6
Have you tried the other method suggested in that forum listing? i.e. not setting Visible to false but rather explicitly adding the columns you want to be displayed to the Columns collection. Try Columns.Clear() then add your column(s). Or try PopulateColumns() then Columns.RemoveAt(0);
I was calling DataBind() from my Form constructor. I moved the call into the Form Load event and it solved the problem.

DevExpress DXGrid column header double click event

I have to check / uncheck all the checkboxes (toggle) in a column when the user double clicks the column header.
How can I implement this behaviour in the DevExpress DxGrid control?
I have searched the DevExpress support forum but I haven't found a solution.
Also, i am working on MVVM Pattern.
This case works for WinForms, not tested in WPF yet, I posted might it direct you to some lights:
There is a workaround to accomplish this behave, you have to implement yourGrid_DoubleClick Event Handler, then calculate the hit Info of the mouse click, the hit info object will tell you if the double click was on a column, something like:
private void yourGridViewName_DoubleClick(object sender, EventArgs e)
{
DevExpress.XtraGrid.Views.Grid.GridView sndr =
sender as DevExpress.XtraGrid.Views.Grid.GridView;
DevExpress.Utils.DXMouseEventArgs dxMouseEventArgs =
e as DevExpress.Utils.DXMouseEventArgs;
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hitInfo =
sndr.CalcHitInfo(dxMouseEventArgs.Location);
if (hitInfo.InColumn)
{
string x = hitInfo.Column.Name;
//Rest of your logic goes here after getting the column name,
//You might now loop over your grid's data and do your logic
}
}
but you have to notice that this action will not prevent the sorting that column's header do, you might need to disable sorting for this grid
Hope this helped.

Get cell contents of a selected row in a DataGridView

I have a DataGridView populated from a database.
I am trying to get the contents of the row that is selected on the RowEnter event. I have set the grid's selection mode to FullRowSelect
I have tried the following:
int orderId = (int)dgUnprocessedCards.Rows[dgUnprocessedCards.SelectedCells[0].RowIndex].Cells[0].Value;
this keep throwing the error
Index was out of range. Must be non-negative and less than the size of the collection.
Any help is appreciated.
I've just tried this in a sample datagridview app and it works just fine so there must be something going on which you haven't told us about.
First thing to do is break your one big statement up into discrete smaller statements so you can see exactly where the failure is.
You can rewrite the code above to something like this for debugging purposes:
var cellindex = dgUnprocessedCards.SelectedCells[0].RowIndex;
var cellcollection = dgUnprocessedCards.Rows[cellindex].Cells[0];
int orderId = (int)dgUnprocessedCards.Value;
Also, you should be able to do the following to achieve what you want:
int orderId = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
That uses the SelectedRows collection which is a little bit more concise and I'd say the more usual way of accessing selected items from the datagridview.
Finally, you probably want to do checking around your cast of the value, since the Value might not necessarily be an int. Something like:
int orderid;
if (!int.TryParse(cellcollection.Value.ToString(), out orderid))
{
// Some logic to deal with the fact that Value was not an int
}
When is the SelectionChanged event raised?
Now - as you mention, your selection changed event fires while loading data into the grid. This doesn't seem to cause a problem in my test version but could be part of your issue.
Why this happens should not be related to the type of data source you are using, but to when you attach the selection changed eventhandler. This is because databinding causes a selection changed event to be raised.
If you add an eventhandler for the DataBindingComplete event and attach your SelectionChanged or RowEnter eventhandlers there, you should not see the handler invoked during databinding.
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
this.dataGridView1.RowEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_RowEnter);
this.dataGridView1.SelectionChanged += new System.EventHandler(this.dataGridView1_SelectionChanged);
}
Note that you will need to delete the designer generated event attachment and reference the designer generated methods for this to work.
This also works:
int orderId = (int)dgUnprocessedCards.SelectedCells[0].OwningRow.Cells[0].Value;
You can get specific column value when you click on Datagridview column
private void DataGridview_CellContentClick(object sender,
DataGridViewCellEventArgs e) { int col = e.ColumnIndex; int row =
e.RowIndex; Var value=DataGridview.Rows[row].Cells[col].Value; }
What I tried to do worked fine but the binding was calling the selection changed event. So I done what David Hall suggested (attaching and detaching the event) and I also put it in a try catch block and now it works perfectly.

How to stop a bindingSouce/ComboBox from changing selection

I have a simple form with list as a data source, a binding source bound to the list, and a combo box bound to the binding source with some fields editing whatever the bindingSource.Current is. What I would like to do is if a bool is set pop up a dialog asking if they want to save changes before they change items. If they say no I want to call CancelEdit() if yes I want to keep them on the current item so they can click the save button. How do I do this?
The second part of my question is because my underlining data source is a List<View> will CancelEdit() even do anything? (View is just a class with string Name and a List<CustomColumn>
Edit:
Let me elaborate on what I am doing to maybe help explain what I am doing.
I have a list of View, these elsewhere in the program will be enumerated to generate a DataGridView. What this menu is for is adding new "Views" and changing the order of the columns in the view (it never actually edits the the CustomColumn just adding items and changing the order of the list<CustomColumn>). What I want to happen is if someone presses cancel or changes to a new view by using the combo box without saving it will undo any changes they made to the List<CustomColumn>
If I infer your question correctly, then the answer is not one that you're going to like; the ComboBox has no mechanism for cancelling a change of selection. I wish it did, as I have come across this issue time and time again. This is how i'd work around the limitation:
bool ignoreEvent = false;
object lastSelectedItem = null;
void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (ignoreEvent) return;
if (CheckForChanges()) {
if (MessageBox.Show("Do you want to save changes?", "Save changes", MessageBoxButtons.YesNo) == DialogResult.Yes) {
ignoreEvent = true;
comboBox1.SelectedItem = lastSelectedItem;
ignoreEvent = false;
}
else {
// call CancelEdit() here
}
}
lastSelectedItem = comboBox1.SelectedItem;
}
Basically, the above code offers the means to revert the ComboBox to its previous selected value, without calling any event handler code in the process. Users will briefly see their item selection change, then snap back if they answer 'No' on the popup.
Also, you're correct in your assertion that CancelEdit() will essentially do nothing - the generic List collection does not support change detection. You may wish to use a DataTable or an ObservableCollection, both of which support change detection.

Categories