Click here to Skip to main content
11,577,258 members (56,935 online)
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 22: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  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I too had this problem and finally resolved this problem in my application.
Apply the Display Index values in ascending order to all the columns exist in grid regardless of invisible or visible columns. You can also shuffle the invisible columns and visible Display Indexes instead of segregating all visible columns should apply with display indexes first and invisible columns afterwards; However Data Grid will display only visible columns in which order that they have the display indexes specified.

Try this :
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;
 
<pre lang="cs">// Apply the DisplayIndex in ascending order 1,2,3... etc
dg.Column[&amp;quot;Column6].DisplayIndex = 1;
dg.Column[&amp;quot;Column7].DisplayIndex = 2;
dg.Column[&amp;quot;Column8].DisplayIndex = 3;
dg.Column[&amp;quot;Column9].DisplayIndex = 4;
dg.Column[&amp;quot;Column10].DisplayIndex = 5;
// These columns are exist in the grid but not displayed, 
// hence you should set the display index for invisible columns as well.
dg.Column[&amp;quot;Column1].DisplayIndex = 6;
dg.Column[&amp;quot;Column2].DisplayIndex = 7;
dg.Column[&amp;quot;Column3].DisplayIndex = 8;
dg.Column[&amp;quot;Column4].DisplayIndex = 9;
dg.Column[&amp;quot;Column5].DisplayIndex = 10;</pre>


HTH
Mahadev
  Permalink  
v2
Comments
CHill60 at 5-May-15 8:47am
   
In other words use Solution 1 that was posted nearly 2 years ago.

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

  Print Answers RSS
0 DamithSL 155
1 OriginalGriff 115
2 Afzaal Ahmad Zeeshan 95
3 Abhinav S 90
4 Lucifier Rocks 85
0 OriginalGriff 820
1 Sergey Alexandrovich Kryukov 631
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 5 May 2015
Copyright © CodeProject, 1999-2015
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