Deleting bound item from DataGridView - vb.net

I have a List of an object, set to DataSource of a BindingSource, set to DataSource of a DataGridView. Then I added a DataGridViewButtonColumn which I want a click to remove that record from the collection. So I simply call collection.RemoveAt(e.rowIndex) from the DataGridView CellClick event. However this produces an IndexOutOfRange Exception; "1 does not have a value"..
So is happening here? I have maybe a vague idea, but would like to understand exactly which events are failing etc.. and what do i need to do to perform this action properly?
EDIT:
This seems not to happen if I use a BindingList.. But when using a List, I get this problem..

The reason you're seeing the error is because you're ultimately binding to a List. List doesn't include the notifications of changes that your DataGridView needs to receive to reflect the changes you make.
If you really want to work around this you could do the following: just before you make a changes to your List, set the BindingSource's DataSource property to Nothing. When you're done making changes reset your List to the DataSource.
This is a pretty ugly solution as you can imagine. What you'll want to do this bind to an object that implements IBindingList, either a custom collection you create or a BindingList.
Here's a reasonable starting point for investigating this further:
DataGridView Control Overview (Windows Forms)

Related

vb.net datagridview and bindingsource

Im in the process over converting some legacy VB6 code over, in particular a TDBGrid linked to an ADODC data control.
Everything is going ok, ive got my columns and bindignsource reading sweet as a nut and performing what its supposed to do, correctly - but im having a problem converting this type of method over.
In the vb6 app, while a user scrolls through the grid, the grid fetchstyle (similar to cellformatting) go looking at the equivalent row of data in the adodc.
In .net, I cant seem to get that functionality to be the same, unless I add that field from the database, into a column in the grid and make it invisible (which I dont really want to be doing if i can help it) then read the cell via cellformatting and then perform some action, such as changing the cellstyle backcolor to something.
Is it possible to refer to a row in the bindingsource that would be the same row in the grid that a user is at, without having that field from the bindingsource - in a DGV column?
or do i just have to put up with placing more columns in the DGV than I want and just live with it?
Thanks!
I think you want the BindingSource.Current
object.

How to Customize ListView Column Headers

I am fairly new to programming and I have never attempted writing my own class before. I would like to try. What I am trying to achieve is to write my own (basic) listview control to start with. I am struggling with the first bit...
As a start I want to try and centre all of the Column Headers but keep the row text using their own formatting. I would like to implement a custom property if possible, but to start with I just want to override the DrawColumnHeader event.
Can someone point me in the right direction (maybe an example). I have setup a new class, inherited the listview control and added the following event:
Protected Overrides Sub OnDrawColumnHeader(ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs)
However, I am finding that any code I place in this event isn't getting fired when the listview gets drawn.
Reason was that I didn't have the OwnerDraw property set to True.

Is there an event which fires before a user enters a row into the datagridview in .net 3.5

I am trying to create a wrapper to replace the sheridan datagrid actvex control with a datagridview.
The sheridan datagrid control supports an event which fires just before the user is going to add a row to the grid called BeforeInsert.
Does anyone know of an equivalent event in the datagridview control or or some other code I could write which produces the same effect?
I'm having a look at all the events here, I don't see one for Adding or Inserting a row, just Added. Although there's one for Deleting and Deleted strangely enough. I won't pretend to know why. To your question, how exactly will users be adding rows? You could try playing around with the "AllowUserToAddRowsChanged" event and change that property programatically when the user wants to add a row.

Insert/Remove item in listbox bound to database

I want to be able to reorder my listbox which is bound to my sql ce database by clicking up and down arrow buttons. Since my listbox is populated directly from my database using the entity framework, I think I have to delete the object (from the listbox) and reinsert it (in the row above) if I want to move the item up the list.
I have no view model, my listbox is populated directly from my database in my code like this:
listBoxProperties.ItemsSource = entities.Properties.ToList();
Does my question make sense?
Cheers
ormally you would handle the moving of your list item in your view model which would hold the ObservableCollection the control was bound to - and then that would be reflected in your control via the binding.
It is going to get messy trying to accomplish this dirrectly on the control - which is your only way since your binding to a throwaway copy of the EF properties list.
As your UI dev goes on you are only going to run into more issues like this. I strongly recommend getting a view model in place sooner rather that later.

VB.net: How do I "refresh" my comboBox?

I have a combobox that is populated by a database table I have. In my Load event for my form I have it populated by...
Me.BusinessTableAdapter.Fill(Me.BillingdbDataSet.Business)
My problem is whenever I "add", "edit", or "delete" a business I have stored I can't find a way to update the combobox I have. Whenever I exit and reopen my program and I can see the changes but was hoping someone could inform me on how to have it updated after a change has happened.
The problem is you need something that implements INotifyCollectionChanged.
MSDN
You can enumerate over any collection
that implements the IEnumerable
interface. However, to set up dynamic
bindings so that insertions or
deletions in the collection update the
UI automatically, the collection must
implement the INotifyCollectionChanged
interface. This interface exposes the
CollectionChanged event, an event that
should be raised whenever the
underlying collection changes.
You can refresh it manually, but the ideal situation is the bind to an observable collection class - http://msdn.microsoft.com/en-us/library/ms668604.aspx HTH
Wade
If you're using datasets you can use the dataview class which raises events when the underlying dataset is changed. Then you could either look into data binding them to the combo-box or write your own update code and put that in an event handler for the event raised when the list changes.