开发者

How to databind with LIST

开发者 https://www.devze.com 2023-02-28 23:19 出处:网络
Suppose I have one customer clas开发者_如何学编程s and I just populate list with customer class like

Suppose I have one customer clas开发者_如何学编程s and I just populate list with customer class like

List <Customer> lst=new List <Customer>;
lst.add(new customer(id=1,name="jhon"));
lst.add(new customer(id=2,name="keith"));

Now can I bind the instance of List to dropdown or Datagridview? If not possible then just show me with small sample. thanks


List<Customer> lst = new List<Customer>;
lst.add(new Customer(id=1,name="jhon"));
lst.add(new Customer(id=2,name="keith"));


myGridView.DataSource = lst;
myGridView.DataBind();


foreach (string name in lst)
{      
    //ddl - combobox
    ddl.Items.Add(new ListItem(lst[name].ToString()));
}

 ddl .DataSource = lst;
 ddl .DisplayMember = "Name";
 ddl .ValueMember = "id";

As per above code you can set display member and value member :) Hope that helps

0

精彩评论

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