Filling SelectList from DataTable

I fulfilled the request and returned datatable,

myDataAdapter.Fill(myDataTable);

Now, what is the best way to load the values ​​of the "Value" and "Text" strings into a SelectList?

thanks

+3
source share
7 answers

Here is what I came up with, it seems to work well, but I was wondering if this is the best way.

First, I created an object that looks like my results form my query,

public class MyTestObj
{
    public string Value_CD { get; set; }
    public string Text_NA { get; set; }
}

Then I create an ILIST and populate it with datatable strings

IList<MyTestObj> MyList = new List<MyTestObj>();
foreach (DataRow mydataRow in myDataTable.Rows)
{
  MyList.Add(new MyTestObj()
  {
    Value_CD = mydataRow["Value"].ToString().Trim(),
    Text_NA  = mydataRow["Text"].ToString().Trim()                      
  });
}

return new SelectList(MyList, "Value_CD", "Text_NA");

Any comments on this approach?

+1
source

I could not find a way to directly bind the DataTable to the SelectList, so I decided to create an extension method for this:

public static SelectList ToSelectList(this DataTable table, string valueField, string textField)
{
    List<SelectListItem> list = new List<SelectListItem>();

    foreach (DataRow row in table.Rows)
    {
        list.Add(new SelectListItem() 
        {
            Text = row[textField].ToString(), 
            Value = row[valueField].ToString()
        });
    }

    return new SelectList(list, "Value", "Text");
}
+7
source
    public static System.Web.Mvc.SelectList DT2SelectList(DataTable dt, string valueField, string textField){            
        if (dt == null || valueField == null || valueField.Trim().Length == 0
            || textField == null || textField.Trim().Length ==0)
            return null;


        var list = new List<Object>();

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            list.Add(new
            {
                value = dt.Rows[i][valueField].ToString(),
                text = dt.Rows[i][textField].ToString()
            });
        }
        return new System.Web.Mvc.SelectList(list.AsEnumerable(), "value", "text");
    }

u

+4

, DataTable 2 ( ),

1. Set the DropDownList DataSource to the table
2. Set the DataTextField to the text column name
3. Set the DataValueField to the value column name
4. Call the Databind method
+2

. , MyTestObj, DataRow. :

MyList.Add(new MyTestObj(mydataRow));
0

, , , , .

List<SelectListItem> listName= new List<SelectListItem>();

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() });
   }

, datatable.

.

0

Datatable AsDataView. SelectList DataTable. :

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add("1", "test");
//populate selectlist with DataTable dt
var selectList = new SelectList(dt.AsDataView(), "Id", "Name");

Here Id in SelectList is the selectedValue column , and Name is DisplayMember . strong>.

0
source

Source: https://habr.com/ru/post/1717935/


All Articles