开发者

c# getting item in row from datatable

开发者 https://www.devze.com 2023-01-31 07:42 出处:网络
foreac开发者_如何转开发h (DataRow row in dt.Rows) { string[] temprow={\"\",\"\",\"\",row[3].ToString()};
    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.

0

精彩评论

暂无评论...
验证码 换一张
取 消