Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hello
 
i have a little issue with datagridview, I need to reorder columns in datagridview
 

 
I have 10 columns loaded into datagridview, 5 of them are invisible, which I made invisible in simple code
 

 
and the rest 5 I want to place in order with displayindex property
 
and here is the thing now
 
I put in code
 
dg.AutoGenerateColumns = false;
 
dg.Columns["Column1].Visible = false;
dg.Columns["Column2].Visible = false;
dg.Columns["Column3].Visible = false;
dg.Columns["Column4].Visible = false;
dg.Columns["Column5].Visible = false;
 
dg.Column["Column6].DisplayIndex = 1;
dg.Column["Column7].DisplayIndex = 2;
dg.Column["Column8].DisplayIndex = 3;
dg.Column["Column9].DisplayIndex = 4;
dg.Column["Column10].DisplayIndex = 5;
 
but Column9 gets on place 3 not 4, while Column8 gets on place 8, THEY CHANGE PLACES!!!
I even set it up to save DisplayIndex-es into Properties.Settings of my solution and then I read the displayindexes into textboxes for each column to see, and THERE IT SAYS Column9 is on place 4 but IT IS NOT, it's on 3!!!!
 
I am puzzled
help, anyone
Posted 11-Jun-13 23:19pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Problem arises due to invisible columns .
set DisplayIndex of invisibe column also .(that is give DisplayIndex to both columns (visible and non visible in order).
as follows
 
 
dg.Columns["Column1"].Visible = false;
dg.Columns["Column2"].Visible = false;
dg.Columns["Column3"].Visible = false;
dg.Columns["Column4"].Visible = false;
dg.Columns["Column5"].Visible = false;
 
dg.Column["Column6"].DisplayIndex = 1;
dg.Column["Column7"].DisplayIndex = 2;
dg.Column["Column8"].DisplayIndex = 3;
dg.Column["Column9"].DisplayIndex = 4;
dg.Column["Column10"].DisplayIndex = 5;
 
dg.Column["Column1"].DisplayIndex = 6;
dg.Column["Column2"].DisplayIndex = 7;
dg.Column["Column3"].DisplayIndex = 8;
dg.Column["Column4"].DisplayIndex = 9;
dg.Column["Column5"].DisplayIndex = 10;
  Permalink  
Comments
shonezi at 12-Jun-13 12:43pm
   
it doesnt work
Adhikari Surendra at 12-Jun-13 21:29pm
   
then there may be some code that effects the column other then this block.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

give proper indexing into grid
 
by set property (index="number")
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

//write same code on EventHandler Shown:
 
MyForm.Shown += new System.EventHandler(this.DlgEmpfListView_Shown);
 
private void MyForm_Shown(object sender, EventArgs e)
{
dg.Columns["Column1"].Visible = false;
dg.Columns["Column2"].Visible = false;
dg.Columns["Column3"].Visible = false;
dg.Columns["Column4"].Visible = false;
dg.Columns["Column5"].Visible = false;
 
dg.Column["Column6"].DisplayIndex = 1;
dg.Column["Column7"].DisplayIndex = 2;
dg.Column["Column8"].DisplayIndex = 3;
dg.Column["Column9"].DisplayIndex = 4;
dg.Column["Column10"].DisplayIndex = 5;
 
dg.Column["Column1"].DisplayIndex = 6;
dg.Column["Column2"].DisplayIndex = 7;
dg.Column["Column3"].DisplayIndex = 8;
dg.Column["Column4"].DisplayIndex = 9;
dg.Column["Column5"].DisplayIndex = 10;
 
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 85
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 20 Feb 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100