How to avoid accessing row values with hard coded index

M

Microsoft

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
 
A

Alvin Bruney

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
 
P

Perecli Manole

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 said:
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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top