I have a binded DataGridView that allows adding new rows. The problem is that a new object is automatically inserted in the binding source when the CurrentRow is the last grid row.
I want that a new object to be added to the binding source only when the user starts typing in one of the last row cells.
That's not the way DataGridView works, and having tried before to change the way adding new items works, I have to caution you against trying it.
Your best option is to have your binding source implement the ICancelAddNew interface. If this interface is implemented, then the DataGridView will call CancelNew if the user leaves the row without entering any data into the new item. You can also use a BindingList<T> as your data source, or wrap your data source in a BindingSource; both of these classes implement ICancelAddNew.
Related
I want to create an application that is refreshing DataGridView content with data about surrounding Wifi networks on every second. I wonder what would be the best binding solution for this task. I think using BindingList is meaningless, because it would update the datagridview on every single update on the List, while it would be better to refresh it after whole List is updated. Meaby simple List and rebinding is appropriate here, what do you think?
You can prevent the DataGridView from updating on every change to your BindingList by the following
myBindingList.RaiseListChangedEvents = false;
// Update BindingList
myBindingList.RaiseListChangedEvents = true;
myBindingList.ResetBindings();
The ResetBindings() call will cause your DataGridView to be refreshed to reflect the changes to myBindingList.
I want to add a new line of row when pressing a button. In datagridview it would be:
datagridview1.Rows.Add()
What is the equivalent code for that in gridcontrol? Please help me.
You cannot add a new row directly to your GridControl, since this is just a container for the views. However, if you're using a GridView inside your GridControl (or any other descendant of ColumnView), you can add a new row using AddNewRow() method.
(myGridcontrol.MainView as DevExpress.XtraGrid.Views.Grid.GridView).AddNewRow();
Link to documentation
EDIT: You can access your view in a different way, of course.
The DevExpress GridControl must always be bound to a datasource: you cannot add rows directly to the GridControl object or its child GridViews.
Instead, you must bind your GridControl to a data source (via the GridControl.DataSource property), and add/remove rows via this data source.
See the 'Binding To Data' documentation at the DevExpress site for more information on the kinds of data sources that can be used with a GridControl.
You can use AddNewRow to add new row and SetRowCellValue to insert value to that row.
yourgridViewName.AddNewRow();
yourgridViewName.SetRowCellValue(rowhandle,columnName,value);
gridViewMappedFileds.UpdateCurrentRow();
Put yourgridName.RowCount-1 for rowhandle to insert the row at last.Put gridViewMappedFileds.Columns["ColumnName"] to give your columnname.
I work with a WinForms app.
I want to add one row to a DataGridView that this row must enter to database.
What event handler must I use to do this?
Is your DataGridView bound to that database? If so, you should be adding and deleting rows in that data source, not in the grid control itself. Try calling the AddNew method on the binding source.
If you're using a standalone DataGridView that isn't bound to a database, then you can call one of the many overloads of the Add method of the grid control's Rows collection.
This might help you out to add a row to the DataGridView:
http://msdn.microsoft.com/de-de/library/system.windows.forms.datagridview%28v=vs.80%29.aspx
If the DataGridView isn't bound to the database, try to bound it or write the data to the database programmatically.
I have a GridView who's DataSource is set to a DataTable. The DataTable is updated by some backend logic every few seconds, at which point a delegate is called to refresh GridView.
Currently I am simply resetting the DataSource, but that causes a problem - it interrupts any ongoing edits in the grid view and makes the selection 'jump' to the top-left cell.
The update logic basically creates a new (identical with regard to columns and rows) DataTable.
Is there any standard way to do it without any drawbacks? Is my only option updating the current DataSource row by row, inserting values programmatically?
Thanks!
You should use a BindingList or some data source that supports change notification.
I'm confused by many things in this question. If you're using a GridView, and not a DataGridView, then you're either using ASP.NET, WPF, or .NET 1.1. Which is it?
Next: you're creating a new DataTable entirely? Well of course the control's going to get reset when you reset the DataSource. It doesn't know that the schema of your new DataTable is the same as the one it's replacing. It's got to go through the columns and re-establish the bindings.
Also, of course it's losing the current row. The current row belongs to the old DataTable, not the new one.
If you want a bound control to retain its state when you update the underlying data source, update the underlying data source, don't replace it with a new one.
Do your updates happen on a background thread? I don't know if it will work in your scenario, but you could try this threaded binding list; see the example to see the worker merrily editing the grid.
How to remove selected items from ListBox when a datasource is assigned to it in C#?
When trying to remove, got error
"Items collection cannot be modified when the DataSource property is set."
But when i try to remove item from datasource (datatable) ,
it thorws error as "datarow is not in current row collection".
Find that item in the DataSource object and remove it, then re-bind the ListBox.
EDIT:
Here's how you delete from a DataTable as your DataSource, regardless of the .NET version.
DataRowView rowView = listBox.SelectedItem as DataRowView;
if (null == rowView)
{
return;
}
dt.Rows.Remove(rowView.Row);
I haven't tried with anything other than WinForms DataGridViews, but I highly recommend BindingListView, which is both faster than DataTables/Views and allows you to bind generic List<T>s as your DataSource.
Alternatively, use a list that implements IBindingList or inherits from BindingList. When objects are added or removed from a Binding List, any controls bound to it are automatically notified of the change and will update themselves accordingly. If you are using BindingList and your class also implements INotifyProperty changed, Any changes to class properties will also be updated automatically in the databinding control. For example, if a column in a datagrid(view) is bound to a property, "Name", and you change "Name" in the datasource, the datagrid will automatically update. If you add a new item to the datasource, the datagrid will update automatically. Binding List also supports notification in the other direction. If a user edits the "Name" field ina datagrid, the bound object will be updated automatically. Going off topic slightly, if you go a little further and impliment "SupportsSortingCore" and the associated methods in BindingList, you can add automatic sorting to your data. Clicking on a columnm header will automatically sort the list and display the header sort direction arrow.
If the ListBox has a datasource assigned, you must remove items from the datasource and then rebind the ListBox
You need to modify the data source rather than the Items collection of the control. Depending on what kind of data source you are binding to, there are going to be different things you have to do so that your UI updates.
The best way is find a collection that fits your needs and implements IBindingList or IBindingListView. Those two interfaces implement even handlers that listen for a CollectionChanged event and update your UI accordingly.
If your collection doesn't support those interfaces, you're going to have to re-bind your data source every time somebody adds/removes an item.
when you get the message "Items collection cannot be modified when the DataSource property is set."
setting the datasource to something else, empty list or null does not help when
the code initializecomponent is not completed.
to avoid that error, one must do the change of datasource or the item list during or after form load.
I know it does not seem to make sense. Hoever, the visual studio designer will generate code in the form designer.cs or vb that will add items to the listbox if any code that changes the items is found before end of initialize components
While Chris Doggett posted a valid solution, I ran into problems while using it. By using that method it was not allowing a subsequent GetChanges(DataRowState.Deleted) to work properly.
To better solve my problem, I only had to change a single line - the last line.
DataRowView rowView = listBox.SelectedItem as DataRowView;
if (null == rowView)
{
return;
}
rowView.Row.Delete();
This allowed my GetChanges call to work properly.
This worked for me
DataTable temp = (DataTable)lstBlocks.DataSource;
temp.Rows.RemoveAt(position);
its vary simple , assign a new blank value to listbox
eg..
Dim ABC As New List(Of String)()
ListBox1.DataSource = ABC
ListBox implementation is bugged, you need to create a new data source instance for the component for it to recognize a change.
Eg:
ActivitiesList.DataSource = _activities;
_activities = new List<Activity>(_activities);
_activities.Remove((Activity)ActivitiesList.SelectedItem);
ActivitiesList.DataSource = _activities;