DataGridView Columns order

DataGridView Columns order

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


SergioTorres posted on Sunday, August 05, 2007

Hi There,

I'm brand new to CSLA, so please forgive my questions.

I am using a ReadOnlyList and a ReadOnlyChild (which I generated using cslagen) classes to fill a datagridview.

My problem is that, although my stored procedure has a given columns order, the datagridview columns appear with a different one. My datagridview.autogeneratecolumns property is set to True.

How can I get the datagridview columns to appear in the same order the stored procedure columns are? Shouldn't it be by default?

Thanks,

Marjon1 replied on Sunday, August 05, 2007

Sergio,

The key here is that the datagridview is bound to your ReadOnlyList which is separate from the stored procedure results. Unless you are using an index to retrieve the values from the database, but if you are using the safedatareader and using a string to identify the columns, then it doesn't matter what order the columns are returned in; the objects would be identical.

The grid that you are binding to does not even know that this stored procedure really exists, other than the fact that the ReadOnlyList would be using it to create itself.

However, I've yet to find any standard sequence in which the columns are created; either in datagridview or using DevExpress XtraGrid (both of which do the same thing). I'm about to start doing some research on this, but am eager to see what over type of responses that you get. Might save me some work!

dagware replied on Monday, August 06, 2007

Ditto! This is a (relatively minor) pain in the rump.

Dan

ambgabi replied on Monday, December 31, 2007

I set up my grid once when the form loads.

private ObjectBindingSource _sessionsBindingSource = new ObjectBindingSource();

_sessionsBindingSource.DataSource = _sessions; //_sessions is a BusinessListBase<SessionList, Session>, youmay just want to bind it to a new BusinessBaseList

foreach (PropertyDescriptor prop_descr in TypeDescriptor.GetProperties

(typeof(Session), new Attribute[] { new BrowsableAttribute(true) }))

{

_sessionsBindingSource.BindableProperties.Add

(new BindableProperty(prop_descr.Name, prop_descr.DisplayName));

}

 

foreach (DataGridViewColumn c in grdResults.Columns)

{

c.ReadOnly = true;

c.Visible = false;

}

int colCounter = 0;

grdResults.Columns["ScheduleGroup_Enrollment_Child_Name"].DisplayIndex = colCounter++;

grdResults.Columns["ScheduleGroup_Enrollment_Child_Name"].Visible = true;

grdResults.Columns["ScheduleGroup_Enrollment_Child_Name"].Width = 100;

grdResults.Columns["ScheduleGroup_Enrollment_Child_Name"].HeaderText = "Child";

Rebind it if your source changed

_sessionsBindingSource.DataSource = _sessions;

grdResults.DataSource = _sessionsBindingSource;

 

This way the order of the columns will be as you want them

Make sure to set them up only once, not every time your source changes

Copyright (c) Marimer LLC