DataGridView DisplayIndex

A

Aleksey Timonin

Hi guys,
I try to adjust columns order in DataGridView via DisplayIndex property and
it doesn't work me properly. Here is the code:

protected override void AdjustGridColumns() {
foreach (DataGridViewColumn clmn in _grdCustomizations.Columns)
clmn.Visible = false;
AdjustColumn("name", "View Customization");
AdjustColumn("userID", "User ID");
AdjustColumn("machineID", "Machine ID");
AdjustColumn("resolutionID", "Resolution ID");
AdjustColumn("createdOn", "Created On");
AdjustColumn("modifiedOn", "Modified On");
}

private void AdjustColumn(string name, string displayName){
DataGridViewColumn clmn = _grdCustomizations.Columns[name];
clmn.HeaderText = displayName;
clmn.Visible = true;
clmn.DisplayIndex = _grdCustomizations.Columns.Count - 1;
}


After I call AdjustGridColumns method it shows me the next columns order :

ID (that shoudn't be at all)
View Customization
Created On
Modified On
User ID
Machine ID
Resolution ID

So, what don't I right? Why the "ID" still visible? Why order of columns
doesn't right?

Thanks a lot
 
M

Morten Wennevik [C# MVP]

Hi Aleksey,

I could not reproduce your issue when creating a DataTable containing a
random order of your listed columns, as well as an ID column. The order was
correct and the ID column did not display.

Do you do anything with the grid after you call AdjustGridColumns() ?

If you add a column after the columns are adjusted, the grid will readjust
its columns seemingly randomly.
 
C

Chris Shepherd

Aleksey said:
After I call AdjustGridColumns method it shows me the next columns order :

ID (that shoudn't be at all)
View Customization
Created On
Modified On
User ID
Machine ID
Resolution ID

So, what don't I right? Why the "ID" still visible? Why order of columns
doesn't right?

DisplayIndex should be manually set in the order that you want the columns
displayed. What happens when you change one column's DisplayIndex is that all
other columns are adjusted to accommodate that change. So you want to go lowest
to highest index, in order.

ie:

grid.Columns["name"].DisplayIndex = 0;
grid.Columns["userID"].DisplayIndex = 1;
grid.Columns["machineID"].DisplayIndex = 2;

And so on.


Chris.
 
M

Morten Wennevik [C# MVP]

Chris Shepherd said:
Aleksey said:
After I call AdjustGridColumns method it shows me the next columns order :

ID (that shoudn't be at all)
View Customization
Created On
Modified On
User ID
Machine ID
Resolution ID

So, what don't I right? Why the "ID" still visible? Why order of columns
doesn't right?

DisplayIndex should be manually set in the order that you want the columns
displayed. What happens when you change one column's DisplayIndex is that all
other columns are adjusted to accommodate that change. So you want to go lowest
to highest index, in order.

ie:

grid.Columns["name"].DisplayIndex = 0;
grid.Columns["userID"].DisplayIndex = 1;
grid.Columns["machineID"].DisplayIndex = 2;

And so on.


Chris.

That is not necessary. Changing the DisplayIndex will cause all the columns
between the old DisplayIndex and the new DisplayIndex to be shifted. Setting
all columns in turn to the last index will effectively sort it according to
when the column is adjusted.
 
A

Aleksey Timonin

1. I have "ID" field in binding DataTable, and it is a key field. But I
don't want to show it... It hides only after I set 0 to DisplayIndex
property for one of the columns.

2. After adjustment I don't add or remove any column. Adjustment is a last
operation I do before show.

3. Even if I give to each column it's real DisplayIndex and not last, I
still get the wrong columns order

Your advice....

Thanks

Morten Wennevik said:
Hi Aleksey,

I could not reproduce your issue when creating a DataTable containing a
random order of your listed columns, as well as an ID column. The order
was
correct and the ID column did not display.

Do you do anything with the grid after you call AdjustGridColumns() ?

If you add a column after the columns are adjusted, the grid will readjust
its columns seemingly randomly.

--
Happy Coding!
Morten Wennevik [C# MVP]


Aleksey Timonin said:
Hi guys,
I try to adjust columns order in DataGridView via DisplayIndex property
and
it doesn't work me properly. Here is the code:

protected override void AdjustGridColumns() {
foreach (DataGridViewColumn clmn in _grdCustomizations.Columns)
clmn.Visible = false;
AdjustColumn("name", "View Customization");
AdjustColumn("userID", "User ID");
AdjustColumn("machineID", "Machine ID");
AdjustColumn("resolutionID", "Resolution ID");
AdjustColumn("createdOn", "Created On");
AdjustColumn("modifiedOn", "Modified On");
}

private void AdjustColumn(string name, string displayName){
DataGridViewColumn clmn = _grdCustomizations.Columns[name];
clmn.HeaderText = displayName;
clmn.Visible = true;
clmn.DisplayIndex = _grdCustomizations.Columns.Count - 1;
}


After I call AdjustGridColumns method it shows me the next columns order
:

ID (that shoudn't be at all)
View Customization
Created On
Modified On
User ID
Machine ID
Resolution ID

So, what don't I right? Why the "ID" still visible? Why order of columns
doesn't right?

Thanks a lot
 
C

Chris Shepherd

Morten said:
So, what don't I right? Why the "ID" still visible? Why order of columns
doesn't right?
DisplayIndex should be manually set in the order that you want the columns
displayed. What happens when you change one column's DisplayIndex is that all
other columns are adjusted to accommodate that change. So you want to go lowest
to highest index, in order.

ie:

grid.Columns["name"].DisplayIndex = 0;
grid.Columns["userID"].DisplayIndex = 1;
grid.Columns["machineID"].DisplayIndex = 2;
[...]
That is not necessary. Changing the DisplayIndex will cause all the columns
between the old DisplayIndex and the new DisplayIndex to be shifted. Setting
all columns in turn to the last index will effectively sort it according to
when the column is adjusted.

I agree on a logical basis. I have run into situations that led me to conclude
the clearest and safest way is generally to define your DisplayIndexes separately.

A particular example I've observed is the preservation of the DisplayIndex
across DataSource assignments where schema matches. IME for large data sets
(5,000+ records) assigning a clone of the data table as the grid's DataSource,
applying your formatting, and then reassigning the true data table as the
DataSource, then applying indexing winds up being significantly faster than
simply setting the DataSource and then performing your formatting and indexing
in one step. For some reason, even though most of the other column information
is preserved, DisplayIndexes appear to be recalculated when the DataSource is set.

I'm not disputing that the OP's approach works, I'm simply saying that I've
observed conditions under which it is more efficient to go about it another way,
and so I tend towards recommending that approach.


Chris.
 
C

Chris Shepherd

Aleksey said:
1. I have "ID" field in binding DataTable, and it is a key field. But I
don't want to show it... It hides only after I set 0 to DisplayIndex
property for one of the columns.

2. After adjustment I don't add or remove any column. Adjustment is a last
operation I do before show.

Are you doing this formatting inside a Form's constructor, or in a Form's OnLoad
event?

I ask because I was developing a proof of concept of a problem I had discovered
a while back to respond to Morten's post, and initially I had done all the
DataTable/DataGridView loading inside the constructor, and it failed. Moving it
to the OnLoad event fixed it, thus disproving the point I was attempting to
make. I'll provide the example below.
To see what I mean, just move everything (except the base.OnLoad() call) inside
the OnLoad method into the constructor, and it will stop working (at least it
does here, FW2.0).
3. Even if I give to each column it's real DisplayIndex and not last, I
still get the wrong columns order



Chris.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

DataTable dt = new DataTable("SomeTable");
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Q"),
new DataColumn("Z"),
new DataColumn("B"),
new DataColumn("X"),
new DataColumn("N"),
new DataColumn("L"),
new DataColumn("J"), // Hidden
new DataColumn("R"), // Hidden
new DataColumn("A")
});

for (int i = 0; i < 30; i++)
dt.Rows.Add(i.ToString(), (i + 5).ToString(),
(i + 50).ToString(), (i + 100).ToString(),
(i - 5).ToString(), (i - 50).ToString(),
(i - 100).ToString(), (i * 500).ToString(),
(i / 5).ToString());

dataGridView1.DataSource = dt;

foreach (DataGridViewColumn col in dataGridView1.Columns)
col.Visible = false;

setColumn("A", ":::A:::");
setColumn("B", ":::B:::");
setColumn("L", ":::L:::");
setColumn("N", ":::N:::");
setColumn("Q", ":::Q:::");
setColumn("X", ":::X:::");
setColumn("Z", ":::Z:::");

}

private void setColumn(string cName, string title)
{
DataGridViewColumn c = dataGridView1.Columns[cName];
c.HeaderText = title;
c.Visible = true;
c.DisplayIndex = dataGridView1.Columns.Count - 1;
}

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode =
System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(292, 266);
this.dataGridView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";

((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.DataGridView dataGridView1;

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top