Populating a SelectList from a DataTable

前端 未结 8 1094
说谎
说谎 2020-12-18 11:12

I ran a query and returned a datatable,

myDataAdapter.Fill(myDataTable);

Now what is the best way to load the row values \"Value\" and \"Te

8条回答
  •  半阙折子戏
    2020-12-18 11:24

    It's way too late for me to answer but I think this might be an optimized way to do it, hope it works.

    List listName= new List();
    
    for (int i = 0; i < datatableName.Rows.Count; i++)
       {
         listName.Add(new SelectListItem { Text = datatableName.Rows[i]["ColumnName"].ToString(), Value = datatableName.Rows[i]["ColumnName"].ToString() });
       }
    

    You can also loop through the column names as well by applying another loop and using that as an index at the column identifier of the datatable.

    Thank You.

提交回复
热议问题