Click here to Skip to main content
14,775,130 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
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
Updated 2-Nov-20 12:27pm

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;
   
Comments
shonezi 12-Jun-13 12:43pm
   
it doesnt work
Surendra Adhikari SA 12-Jun-13 21:29pm
   
then there may be some code that effects the column other then this block.
give proper indexing into grid

by set property (index="number")
   
//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;


}
   
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
   
v2
Comments
CHill60 5-May-15 8:47am
   
In other words use Solution 1 that was posted nearly 2 years ago.
Hi,

I was also facing the same issue, got a very simple solution for this. You can use the column name instead of using the index id while accessing it.

For example, use can do like this:

dg["ListType", i].Value = "abc" ( here i is the "row number" and ListType is the column name.
   
Comments
Richard MacCutchan 5-Jan-18 4:53am
   
Well done, only FOUR years too late.
Responding to an old post/question, i tried turning off the
AutoGenerateColumns
property of the datagridview before assigning the displayindex.
dgv1.AutoGenerateColumns = false;
dgv1.Columns["column1HeaderText"].DisplayIndex = 0;
dgv1.Columns["column2HeaderText"].DisplayIndex = 1;
dgv1.Columns["column3HeaderText"].DisplayIndex = 2;
One suggestion is that if there are some columns/any column that has to be hidden then hide it after setting the display index.
dgv1.Columns["column1HeaderText"].Visible = false;
Hope this helps someone!
   
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900