简体   繁体   中英

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?

MSDN Link

Eg:

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

Where "columnName" is the name of your 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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