Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reorder columns in a DataGridView?

so I fill my DGV with some data and set some columns invisible:

        var part = inventory.espiromex_product.Where(p => p.descriptionsmall == cmbMainP.Text).First().partnumberp;
        dtgAssy.DataSource = inventory.espiromex_productsub.Where(p => p.partnumberp == part);
        dtgAssy.Columns["idproductsub"].Visible = false;
        dtgAssy.Columns["partnumberp"].Visible = false;
        dtgAssy.Columns["partnumbersubp"].Visible = true;
        dtgAssy.Columns["quantity"].Visible = true;
        dtgAssy.Columns["comments"].Visible = true;
        dtgAssy.Columns["assemblyno"].Visible = false;
        dtgAssy.Columns["assemblynodesc"].Visible = false;
        dtgAssy.Columns["uomid"].Visible = true;
        dtgAssy.Columns["subassemblylevelnumber"].Visible = false;
        dtgAssy.Columns["scrappercent"].Visible = true;

this is just fine but columns are sorted Alphabetically, How can I reorder columns programmatically?

note that inventory is an Entitie and I am using Linq to Entities.

like image 494
Luiscencio Avatar asked May 18 '10 15:05

Luiscencio


2 Answers

You can set the DisplayIndex property of the individual columns.

like image 94
Austin Salonen Avatar answered Nov 06 '22 19:11

Austin Salonen


Another suggestion: don't let the datagridview make decisions for you. Instead of letting the dgv automatically generate columns for you at binding time, write the code to create the columns in the order you want and with the attributes you want and then bind the data source. Relying on the dgv to do the work can create unpredictable results as new versions come out or you make subtle changes to your data source.

like image 37
cdkMoose Avatar answered Nov 06 '22 19:11

cdkMoose