如何从其名称获取DataGridView的索引 [英] how to get the index of DataGridView from its name
本文介绍了如何从其名称获取DataGridView的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Windows Apllication中,我有一个datagridview(例如:dgv),有一些列(A,B,C).
现在,在运行时,我想从列名中获取列索引.
假设我通过"A",则结果应为"0".如果是"B":1
等等.
有人可以帮忙吗?
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??
推荐答案
DataGridViewColumn类的Index属性可用于上述目的,例如
The Index property of DataGridViewColumn class can be used for the above purpose like
dgv.Columns["A"].Index.ToString();
从ColumnHeaderText获取列的索引 [/编辑]
To get index of the Column from ColumnHeaderText[/Edit]
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;
}
int intIndex = gridview.rows [e.rowindex] .value
int intIndex=gridview.rows[e.rowindex].value
我希望这是您正在寻找的
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 :)
这篇关于如何从其名称获取DataGridView的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文