简体   繁体   中英

How to Use Column Name for Datagridview in C#

I am using DataGridView with 5 Column AS

SrNo   Name   Address  City  Country

Whenever i am retrieving Data From Data Grid View i Use Following Code :

SomeoneName =  MYdataGrid[1,1].Value ;

I have one problem with above code that if i add any Column so in entire program i should be re arrange the column no.s for example if I add FirstName Column before name Column then

SomeoneName =  MYdataGrid[1,1].Value ;

code will change as

SomeoneName =  MYdataGrid[2,1].Value ;

My Question is there is any way to rearrange colomn nos. Automatically...

When you Assassin DataGridView Columns then give column Name Same As your Column Header like

_SrNo   _Name   _Address  _City  _Country

and whenever want to use datagridview value use

SomeoneName =  MYdataGrid[_Name.Index,1].Value ;

so whenever how many columns you add it will rearrange Automatically. Hope This Will Help

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