DataRow to Hashtable

  • Thread starter Thread starter Tony Yates
  • Start date Start date
T

Tony Yates

I am wanting to know the most effecient way of converting a datrow to a
hashtable.

Many thanks if you can help

Kind Regards

Tony
 
I don't think that there is such functionality out of the box.
You might loop through columns and add their values to hashtable?
 
What do you want to store as the key and what do you want to store as its
value? If you wanted to store some values of each row in a datatable , you
could just walk through the datatable or a dataview and add the iterms to
the table:

HashTable ht = new HashTable();

//Now use one of these:
//For a datatable
foreach(DataRow dro in myDataTable.Rows){

ht.Add(dro[KeyColumn], dro[WhateverOtherValueFromColumn]);
}


//Fora dataview:
IEnumerator iterator = dv.GetEnumerator();
DataRowView drv;
while(iterator.MoveNext())
{
drv = (DataRowView)iterator.Current;
ht.Add(drv[0], drv[1]);
}

foreach(DataRowView drv2 in dv)
{
//Do the add here - same logic as w/ DataTable
}
--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
I am trying to retrive data from a datasource which will only return one
row of data.

I then want to stick that in a hashtable so it looks a little like the
following

key value
name tony
age 20
sex male

so i guess i am wanting to do something like the following

foreach(field in datarow)
{
ht.Add(datarow.fieldname, datarow.value);
}

Does this make sense ?

Regards

Tony
}
 
Hi Tony,

Tony Yates said:
I am trying to retrive data from a datasource which will only return one
row of data.

I then want to stick that in a hashtable so it looks a little like the
following

key value
name tony
age 20
sex male

so i guess i am wanting to do something like the following

foreach(field in datarow)
{
ht.Add(datarow.fieldname, datarow.value);
}

Does this make sense ?

Why don't you just use row?
row["columnname"]
 
Yeah your right, I guess it's one of those moments where you get
something in your head and don't see the simpler solution.

Many Thanks

Tony
Hi Tony,

I am trying to retrive data from a datasource which will only return one
row of data.

I then want to stick that in a hashtable so it looks a little like the
following

key value
name tony
age 20
sex male

so i guess i am wanting to do something like the following

foreach(field in datarow)
{
ht.Add(datarow.fieldname, datarow.value);
}

Does this make sense ?


Why don't you just use row?
row["columnname"]
 
Back
Top