Chiro300
Chiro300

Reputation: 76

What is the best method for getting the column index from an datagridview in c#?

There are different methods to get the index of a datagridview column in C#, for example:

dataGridViewTextBoxColumn1.Index; 
dataGridViewTextBoxColumn1.DisplayIndex; 
datagridview1.Columns[column.name].Index;
datagridview1.Columns[column.name].DisplayIndex;

The dataGridViewTextBoxColumn1 is inside the DataGridViewColumnsCollection List.

Desingner Code:

this.datagridview1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.dataGridViewTextBoxColumn1,
            this.dataGridViewTextBoxColumn2,
            this.dataGridViewTextBoxColumn3
            }

I was experiencing the different methods, but in strange cases I obtain differents values, for example I use this:

datagridview1.Columns[dataGridViewTextBoxColumn1.name].Index; 

I get the real index relative to the column collection, in my test the value is 3.

but if I use this:

dataGridViewTextBoxColumn1.Index; 

I get a distinct index, in my test the value is 1.

I do not understand that, because in theory is the same property, doesn't it?,

in case of the displayindex property the result is similar. In my datagridview I have invisible and read only columns.

In other tests, I get the correct values for all cases, and this datagridivew contains more visible and invisible columns to.

My question is: what is the best method for get the correct index of the column in a DataGridView, if I use the events, for example CellContentClick to equalize the DataGridViewCellEventArgs.ColumnIndex?

example:

private void datagridview1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex.Equals(datagridview1.Columns[dataGridViewTextBoxColumn1.Name].Index))
        datagridview1.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumn2.Index].Value = true;
}

UPDATE

this is my test is a visual studio watch:

TEST 1:

enter image description here

 private void dgvTerminales_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex.Equals(dgvTerminales.Columns[clmActiva.Name].Index))
                dgvTerminales.Rows[e.RowIndex].Cells[clmEditado.Index].Value = true;
        }

TEST 2:

Other datagridview with same properties, columns and form.

enter image description here

private void dgvSucursales_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex.Equals(dgvSucursales.Columns[clmActiva.Name].Index))
                dgvSucursales.Rows[e.RowIndex].Cells[0].Value = true;
        }

clmActiva is a DataGridViewCheckBoxColumn column type.

Upvotes: 2

Views: 2622

Answers (2)

T McKeown
T McKeown

Reputation: 12847

Clearly (if you are getting different results),

column.Index;

Is not pointing to the same instance as:

datagridview1.Columns[column.name].Index;

To prove it display the .GetHashCode() value of both.

Upvotes: 0

Tsukasa
Tsukasa

Reputation: 6552

Update # I lost count

Index

Gets the relative position of the band within the DataGridView control. The value of this property does not necessarily correspond to the current visual position of the band within the collection. For example, if the user reorders columns in a DataGridView at run time (assuming the AllowUserToOrderColumns property is set to true), the value of the Index property of each column will not change. Instead, the column DisplayIndex values change. Sorting rows, however, does change their Index values.

DisplayIndex

Gets or sets the display order of the column relative to the currently displayed columns. Unlike the Index property, the DisplayIndex property corresponds to the current position of the column as displayed by the user interface (UI). By default, each column's DisplayIndex is set to numbers of increasing order, which reflects the order in which they were added. The Visible property value does not affect the DisplayIndex value.

Update

Ok so it seems the OP left out his exact code which makes this next to impossible to help but after all of the comments in this thread, here goes my next set of results.

The OP is using a DataGridViewCheckBoxColumn so in fact by using CellContentClick this is not incorrect. Changing the CheckBox value will properly fire this event.

The order of events fired would follow CellClick, CellContentClick

Now that this is out of the way, Next

Without having your exact code and trying to piece it together I have the following. (Note because I change these names in the designer, if you try to edit it in the UI it causes issues but I was matching what OP provided so as long as these are adjust in designer.cs itself it works fine lol. The visual designer will yell at you trying to use these names)

this.dataGridViewTextBoxColumn1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.dataGridViewTextBoxColumn2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dataGridViewTextBoxColumn3 = new System.Windows.Forms.DataGridViewTextBoxColumn();

this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.dataGridViewCheckBoxColumn1,
this.dataGridViewTextBoxColumn2,
this.dataGridViewTextBoxColumn3});

So my DataGridView has 1 CheckBox and 2 Text Columns

below registers all values as 0 when I click the first column with the checkbox which is correct. The event will not fire on TextBox columns.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    int a = e.ColumnIndex;
    int b = dataGridView1.Columns[dataGridViewCheckBoxColumn1.Name].Index;
    int c = dataGridViewCheckBoxColumn1.Index;

    if (e.RowIndex >= 0 && e.ColumnIndex.Equals(dataGridView1.Columns[dataGridViewCheckBoxColumn1.Name].Index))
        dataGridView1.Rows[e.RowIndex].Cells[dataGridViewTextBoxColumn2.Index].Value = true;
}

When I change the position of the CheckBox to be the second column in the grid rather than the first. Then the above values register as all 1 which is correct again.

I don't see any issues. Please again provide us with your exact code, controls ...etc. There is still something missing we can't see as there isn't an issue with what you are providing.

Upvotes: 1

Related Questions