Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In windows Apllication, I have a datagridview (say:dgv), I have some columns (A,B,C).
Now, at runtime I want to get the column index from Column Name.
Suppose if I pass "A", then result should be "0". in case of "B": 1
and so on.

can somebody please help?
Is for/foreach loop is the only solution??
Posted
Updated 14-May-12 21:17pm
v2

The Index property of DataGridViewColumn class can be used for the above purpose like
C#
dgv.Columns["A"].Index.ToString();

[Edit]To get index of the Column from ColumnHeaderText[/Edit]
C#
string headerText = "A";
string index = string.Empty;
foreach (DataGridViewColumn column in dgv.Columns)
    if (column.HeaderText.Equals(headerText, StringComparison.InvariantCultureIgnoreCase)){
        index = column.Index.ToString();
        break;
    }
 
Share this answer
 
v5
Comments
Vipin_Arora 10-Apr-12 5:21am    
Thanks VJ,

My vote is +5. but, this answer is looking perfect when ColumnName and headerText are same.

But my requirement is I am getting headertext, through which I hv to get Index. (Note: My hederText and ColumnName are different)
VJ Reddy 10-Apr-12 5:43am    
I understood your requirement. Now I modified the solution. Please see.
Vipin_Arora 10-Apr-12 5:53am    
can't understand the line:
FirstOrDefault (n => n.HeaderText.Equals(headerText, StringComparison.InvariantCultureIgnoreCase));

can you plz clarify?
VJ Reddy 10-Apr-12 6:06am    
FirstOrDefault is an extension method of IEnumerable interface. I thought that DataGridViewColumnCollection implements IEnumerable interface. Sorry. I have observed that DataGridViewColumnCollection does not implement IEnumerable. So the foreach loop can be used. I have modified my solution accordingly. Please see.
VJ Reddy 10-Apr-12 11:17am    
Did you get the index with the updated code?
int intIndex=gridview.rows[e.rowindex].value
 
Share this answer
 
I hope this is what you are looking for
public Form1()
{
InitializeComponent();
}
private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
{
int index = this.dataGridView.???????????????????
DataGridViewRow dgvrow = this.dataGridView1.Rows[index];
DataRowView drvrow = (DataRowView)dgvrow.DataBoundItem;
if (drvrow != null)
{
String contract = drvrow["ttttt"].ToString();
ShowContractDetail(contract);
}
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
this.dataGridView1.Rows[e.RowIndex].Selected = true;  //the rows get selected. Goodie
}
}



Happy coding :)
 
Share this answer
 
Comments
Member 11247113 8-Jan-15 2:31am    
hgggggggggggggggg
dd
dfdfd
dfdf
dfd
dfd
df
dfd
df
df
dfd
df
df
dfd
fdf
dfd
df
df
df
df
df
df
df
df
df
dfds
fds
fsdf
f
dsf
s
fds
fds

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



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