Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > How to avoid accessing row values with hard coded index

Reply
Thread Tools

How to avoid accessing row values with hard coded index

 
 
Microsoft
Guest
Posts: n/a
 
      09-24-2003
Hi there ,

My code look like this,

string var1,var2,var3,var4;

foreach (DataGridItem item in MyDataGrid.Items)

{

var1 = item.Cells[0].Text;

var2 = item.Cells[1].Text;

var3 = item.Cells[2].Text;

var4 = item.Cells[3].Text;

}

How can access the row data with column header text instead of hard coded
index's. Like



var1 = item.Cells["ID"].Text;

var2 = item.Cells["FromDate"].Text;

var3 = item.Cells["ToDate"].Text;

var4 = item.Cells["TransferDate"].Text;



Thanks

Baski


 
Reply With Quote
 
 
 
 
Alvin Bruney
Guest
Posts: n/a
 
      09-27-2003
this is an oversight on the part of MS. You can only access via an index. If
you really need this you will need to do a typed dataset
"Microsoft" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi there ,
>
> My code look like this,
>
> string var1,var2,var3,var4;
>
> foreach (DataGridItem item in MyDataGrid.Items)
>
> {
>
> var1 = item.Cells[0].Text;
>
> var2 = item.Cells[1].Text;
>
> var3 = item.Cells[2].Text;
>
> var4 = item.Cells[3].Text;
>
> }
>
> How can access the row data with column header text instead of hard coded
> index's. Like
>
>
>
> var1 = item.Cells["ID"].Text;
>
> var2 = item.Cells["FromDate"].Text;
>
> var3 = item.Cells["ToDate"].Text;
>
> var4 = item.Cells["TransferDate"].Text;
>
>
>
> Thanks
>
> Baski
>
>



 
Reply With Quote
 
 
 
 
Perecli Manole
Guest
Posts: n/a
 
      10-03-2003
What I did is created an enumeration with one member for each column with a
value assigned to that of the coresponding index.

Private Enum EnumName
Column1 = 0
Column2 = 1
...
...
End Enum

Then whenever you need to access the cell do like so:
item.Cells[EnumName.Column1 ].Text

The benefit with this is that when you add or remove a column all you have
to do is adjust the enumeration. You won't have to go through every line of
code and change the indexes.

Perry


"Alvin Bruney" <vapordan_spam_me_not@hotmail_no_spamhotmail.com > wrote in
message news:O%(E-Mail Removed)...
> this is an oversight on the part of MS. You can only access via an index.

If
> you really need this you will need to do a typed dataset
> "Microsoft" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi there ,
> >
> > My code look like this,
> >
> > string var1,var2,var3,var4;
> >
> > foreach (DataGridItem item in MyDataGrid.Items)
> >
> > {
> >
> > var1 = RegisterHiddenField> >
> > var2 = item.Cells[1].Text;
> >
> > var3 = item.Cells[2].Text;
> >
> > var4 = item.Cells[3].Text;
> >
> > }
> >
> > How can access the row data with column header text instead of hard

coded
> > index's. Like
> >
> >
> >
> > var1 = item.Cells["ID"].Text;
> >
> > var2 = item.Cells["FromDate"].Text;
> >
> > var3 = item.Cells["ToDate"].Text;
> >
> > var4 = item.Cells["TransferDate"].Text;
> >
> >
> >
> > Thanks
> >
> > Baski
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Refactoring hard-coded values Aidan Gauland XML 1 06-30-2011 01:07 AM
Re: Coded bar email {rearrange to avoid Usenet bots} George Orwell Computer Security 0 01-09-2010 08:11 AM
Re: Coded bar email {rearrange to avoid Usenet bots} Anonymous Remailer (austria) Computer Security 0 01-07-2010 08:38 PM
Coded bar email {rearrange to avoid Usenet bots} ŠAriŽ Computer Security 8 01-06-2010 09:59 PM



Advertisments