How to avoid accessing row values with hard coded index

Discussion in 'ASP .Net Datagrid Control' started by Microsoft, Sep 24, 2003.

  1. Microsoft

    Microsoft Guest

    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
    Microsoft, Sep 24, 2003
    #1
    1. Advertising

  2. Microsoft

    Alvin Bruney Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
    Alvin Bruney, Sep 27, 2003
    #2
    1. Advertising

  3. 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%...
    > 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" <> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
    Perecli Manole, Oct 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia
    Replies:
    1
    Views:
    522
    Curt_C [MVP]
    Jan 6, 2005
  2. Luke Airig
    Replies:
    1
    Views:
    587
    Dimitre Novatchev
    Dec 24, 2003
  3. rodchar
    Replies:
    2
    Views:
    360
    rodchar
    Jul 1, 2008
  4. Aidan Gauland

    Refactoring hard-coded values

    Aidan Gauland, Jun 29, 2011, in forum: XML
    Replies:
    1
    Views:
    1,307
    Joe Kesselman
    Jun 30, 2011
  5. Kevin
    Replies:
    1
    Views:
    116
    Evertjan.
    Oct 22, 2005
Loading...

Share This Page