简体   繁体   English

如何在运行时按列名获取值而不是列索引号?

[英]How to get the value by column name at runtime for a Lookup instead of column index number?

I can get the value for the lookup at runtime:我可以在运行时获取查找值:

Id = this.dgDevLkp[row, 3].ToString().TrimEnd();

I'm wondering if I can do something like this:我想知道我是否可以做这样的事情:

Id = this.dgDevLkp[row, columnName].ToString().TrimEnd();

Is this possible?这可能吗?

Why not use the DataGridViewColumn's Index property?为什么不使用 DataGridViewColumn 的Index属性?

MSDN Link MSDN链接

Eg:例如:

Id = this.dgDevLkp[row, columnName.Index].ToString().TrimEnd();

Where "columnName" is the name of your DataGridViewColumn.其中“columnName”是您的 DataGridViewColumn 的名称。

Thanks for all suggestions感谢所有建议

The following code worked for me以下代码对我有用

int ColumnIndex = this.dsDevLkp.Tables[dgtbsDevLkp.MappingName].Columns["ColumnName"].Ordinal;
Id = this.dgDevLkp[row, ColumnIndex].ToString().TrimEnd();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM