c# getting item in row from datatable
foreac开发者_如何转开发h (DataRow row in dt.Rows)
{
string[] temprow={"","","",row[3].ToString()};
dtworklist.Rows.Add(temprow);
}
dt is my datatable. i need to get the 3rd element in the row and put it into an array. the above is what i tried so far with no luck. how do i do it?
Try this:
int i=0;
List<string> l=new List<string>();
foreach (DataRow row in dt.Rows)
{
l.Add(Convert.ToString(row[2]));
}
string[] stringArray=l.ToArray();
what if you try:
foreach (DataRow row in dt.Rows)
{
DataRow added = dtworklist.NewRow();
int columnOfInterest = 2; // could be the column name as a string too
added[columnOfInterest] = row[columnOfInterest];
}
Note that I have used index of 2 because the first index in the list is zero. If you want the third item you would be interested in index 2.
精彩评论