Setting Column Index in a Datagrid

Setting Column Index in a Datagrid

Old forum URL: forums.lhotka.net/forums/t/1599.aspx


Brownpa1 posted on Thursday, October 26, 2006

I have created a datasource and am assigning that datasource to a datagrid.  My problem is that my column index's keep changing on their own.  I keep going into the Edit Columns on my datagridview task and changing them back to the order I need them to be, however it will work for a while and then all of  a sudden its back to being messed up.  I can't really see any reason why it is doing this.  When I'm fetching the information through my stored procedure I have it in the exact order I want it to be in.  Any help you can give me on this would be greatly appreciated.

ajj3085 replied on Thursday, October 26, 2006

Have you tried throwing a bindingsource between the datasource and the grid?   That seems to have fixed problems for me.. don't know why though.

Brownpa1 replied on Thursday, October 26, 2006

Do you mean something like this:

'Insures the indexing is correct
Dim bSource As New BindingSource
bSource.DataSource = m_Position.Hardwares            'Hardwares is a child class
Me.dgvHardware.DataSource = bSource

ajj3085 replied on Thursday, October 26, 2006

Well the bindingsource is usually created by putting it in the component tray of the form.

But yes that's the general gist.

Brownpa1 replied on Monday, October 30, 2006

I did have the bindingsource between the datasource and the grid, using my component tray.  However, this didn't work.  It still is changing my index at random.  Does anybody have any other ideas as to why this is happening?  I am new to using datagrids in vb.net 2005 and  just can't seem to figure out why this is happening.

Smirk replied on Monday, October 30, 2006

I've been using DataGridView, so I don't know if it will be the same for DataGrid or not, but...

I had to go to the Form Designer (yourForm.Designer.vb - right click View Code) code and re-arrange the AddRange() code to the order that I wanted, and then it seems to stay put.

Don't know if that will work for you...

Mark Haley

Brownpa1 replied on Tuesday, October 31, 2006

I went to the myForm.Designer.vb and your right, it was in the order that my datagridview keeps switching back to.  I put my columns in the order I would like them to be and hopefully, this will take care of the issue.  Thanks a lot for your help.  This has been a real frustration for me.

Copyright (c) Marimer LLC