WinForms, BindingSource, prevent CurrentItem change - c#

How can I prevent bindingSource Current item from changing?
(there is no changing event with cancel argument...)
This is scenario:
I have a dataGridView, and text-boxes on the same form.
I am using text-boxes to change values in the datasource (with standard databinding)
Bindings are written manually (After save button is clicked)
When user selects another row using DataGridView, bindingSource.Current propery is changed, and text boxes are updated with values from selected row. Changes that user entered are lost.
Is there any way to prevent this problem?
Can I prevent bindingSource.Current property from changing?
Is there any better option to prevent this behaviour?
(disabling dataGridView is unfortinutelly not an option)

It sounds like you don't want to not change bindingSource.Current, but rather you want to save the contents of the text boxes before you change the current row? If you've bound a collection to the bindingSource, then don't the text boxes refer to properties in the current element in that collection?
I'm not really sure what you're trying to do, but a shot in the dark might be to bind the same DataSource to two different BindingSource objects, something like this:
gridBindingSource.DataSource = theDataSource;
textBoxBindingSource.DataSource = theDataSource;
myDataGrid.DataSource = gridBindingSource;
firstNameTextBox.Bindings.Add (new Binding ("Text", textBoxBindingSource, "FirstName"));
but this would be weird, because if theDataSource is appropriate for a grid control, then it's a collection of things that have a FirstName property. Maybe if you were more specific in your question.
ETA: If you want to save the text box contents to the current row, call ValidateChildren () on the container before the bindingSource.Current property changes.

I have a somewhat similar framework with both grid and textboxes on same form. When user clicks the EDIT button (or Add), I just disable the gridview control itself...
MyDataGrid.Enabled = false;
continue editing..
Then in the SAVE, if all is ok,
MyDataGrid.Enabled = true;

Related

How to bind Controls to Data, based on seletced item of ComboBox?

I am having trouble understanding how to bind data to some controls.
From the image below, I have n objects in a list that I want to be editable, but I don't want n amount of controls. I want to select which object from the combo box - so that I can reuse one set of controls.
I have bound data to my combo box, and bound data to a grid view - 'easy'.
But my numericalUpDown doesn't have a DataSource member.
How can I bind it?
And, would I change the binding when comboBox1_SelectedIndexChanged() is called? If not, what is a good way to do this?
Thank you for any advice.
I am not sure if this is the best or neatest way, but it definitely works and gives me what I needed.
I believe I need to clear the binding each time, when the selected object I am binding to changes.
The below code clears any existing bindings, and binds the control to the selected data object, binding the Text property of the control to the Xpos value of the object.
Taking the index value of the combo box, and then using it to access the object that we want in the list.
Within comboBox1_SelectedIndexChanged:
int index = comboBox1.SelectedIndex;
pos_xUpDown.DataBindings.Clear();
pos_xUpDown.DataBindings.Add("Text", objectlist[index], "Xpos");
pos_yUpDown.DataBindings.Clear();
pos_yUpDown.DataBindings.Add("Text", objectlist[index], "Ypos");
pos_zUpDown.DataBindings.Clear();
pos_zUpDown.DataBindings.Add("Text", objectlist[index], "Zpos");

Gridview Automatically Selects first Row after a sort

I am working on a master/detail gridview and detailsview in asp.net web forms using an objectdatasource. The details view is displaying extra information about the selected row from the gridview. When I sort the grid, I want the currently selected row, before sort occurs, to remain in the details view after the sort is completed. Instead, the gridview is auto selecting the new first row whenever I sort.
I found a partial solution to this problem. If I set WhateverGridview.SelectedIndex= -1 onsort and the value becomes null. This makes it deselect any rows after a sort. This leaves the details view blank. However, what I want to do is maintain the selected row not nullify it.
So, does anyone have a good way to retain the selected value or prevent the details view from displaying after sort event fires.
Here you need to use the GridView.EnablePersistedSelection property. Set this property to true.
Setting this property to true means that GridView will make sure that the selection of a row is based on data-key values.
By default GridView makes row selections based on index. This is the reason why when you sort, gridview is selecting row based on index and you lose your actual selected row.

How to make a specific Column Uneditable In datagridview?

Using a DataGridView, how can I make a specific column uneditable while the grid view itself has "Allow editing" enabled?
Also, how can I execute an event when the selected index in a ComboBox in the DataGridView has changed? Here, ComboBox is a column type.
Another question is, how can I make the header title aligned to the center? I can't find the appropriate property.
You've got a few questions here.
(1) How can I make a specific column uneditable in DataGridView?
Set the ReadOnly flag on the particular column you want to make uneditable.
dataGridView.Columns["YourColumnName"].ReadOnly = true;
(2) How can I execute an event when the selected index on a ComboBox in the DataGridView changes?
If it's in your DataGridView, it's not a ComboBox; it's a DataGridViewComboBoxColumn. According to MSDN:
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
This one I'm not familiar with, as I've never tried it myself. It appears you want to subscribe to the EditingControlShowing event and then see if something like this works for you (with a little tweaking).
(3) How can I make the header title align in the center?
Set the HeaderCell.Style.Alignment
dataGridView.Columns["YourColumnName"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

C# Datagridview not adding new rows when databound

I have a databound dgv that contains combobox columns.
When the user selects an item from the combobox, then a new row is added as expected, and the values are added to a databound list. However, when I type a new value into the combobox, rather than select an option that is currently populated in the combobox, then no new row is added, although the value has still been added to the databound list.
I can't also add a new row programmatically as the dgv is databound.
Anybody any ideas?
Thanks.
I don't know how you implemented this (can't hurt if you would post the code).
But maybe you are using a wrong eventhandler?
Maybe you could try to add the entered value to the combobox, and then change the index to the added value.
seems to be related to the way you 'confirm' what the user did. Maybe you use the 'SelectionChange' for the ComboBox, and that event does not fire if you enter a text, even if you press enter.
handle the preview key pressed event, test if key was enter, and then handle this the same way you handle SelectionChanged.

Stuck with datagridview and combo box column

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

Categories