Play-Asia.com - Buy Video Games for Consoles and PC - From Japan, Korea and other Regions!

C# - Binding Data to List Control

An ASP.NET list control can be bound to any data source that implements the IEnumerable, ICollection, or IListSource interfaces (for example, DataView, OleDBDataReader, SQLDataReader, Hashtable, and ArrayList). eXtensible Markup Language (XML) data can also be bound to the list controls, to fill a DataView object with its data and then bind the List Control to the DataView object.
 

This article will show you some examples of how to bind a data to List Control as follow:

Binding a List Control to an ArrayList

<%@Page Language="C#" %>
<script language="C#" runat="server">
     void Page_Load(Object sender, EventArgs e){
          if(!Page.IsPostBack){
 
               ArrayList arlist = new ArrayList();
               arlist.Add("Red");
               arlist.Add("Green");
               arlist.Add("Blue");
               arlist.Add("Yellow");

              myRBList.DataSource = arlist;
              myRBList.DataBind();
          }
     }

     void Submit_click(Object src, EventArgs e){
          if(myRBList.SelectedIndex > -1){
              msgLabel.Text = "You select : " + myRBList.SelectedItem.Text;
          }
     }
</script>
<html>
<body>
<form runat="server" id="form1" name="form1">

<asp:RadioButtonList id="myRBList" runat="server" />
<br><br>
<asp:button id="btnSubmit" runat="server" Text="Submit" onclick="Submit_click" />
<br><br>
<asp:label id="msgLabel" runat="server" />

</form>
</body>
</html>

Binding a List Control to a Hastable

<%@Page Language="C#" %>
<script language="C#" runat="server">
     void Page_Load(Object sender, EventArgs e){
          if(!Page.IsPostBack){
 
               Hashtable h = new Hashtable();
               h.Add("Product 1", "5");
               h.Add("Product 2", "8");
               h.Add("Product 3", "2");
               h.Add("Product 4", "13");

              myGrid.DataSource = h;
              myGrid.DataBind();
          }
     }
</script>
<html>
<body>
<asp:DataGrid id="myGrid" runat="server" AutoGenerateColumns="false">
   <Columns>
        <asp:TemplateColumn HeaderText="Product">
              <ItemTemplate>
                     <%# ((DictionaryEntry)Container.DataItem).Key %>
              </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="Quantity">
              <ItemTemplate>
                     <%# ((DictionaryEntry)Container.DataItem).Value %>
              </ItemTemplate>
        </asp:TemplateColumn>
   </Columns>
</asp:DataGrid>

</body>
</html>

Binding a List Control to Dataview

<%@Page Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server">
     void Page_Load(Object sender, EventArgs e){
           SqlConnection conn = new SqlConnection("server=localhost;uid=sa;pwd=;
                          database=northwind");
          SqlDataAdapter adapter = new SqlDataAdapter("select * from employees", conn);

          DataSet ds = new DataSet();
          adapter.Fill(ds, "employees");

         mygrid.DataSource = ds.Tables["employees"];
         mygrid.DataBind();
     }
</script>
<html>
<body>

<asp:datagrid id="mygrid" runat="server" />

</body>
</html>

Binding a List Control to XML Data Source

<%@Page Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.IO" %>

<script language="C#" runat="server">
     void Page_Load(Object sender, EventArgs e){

          DataSet ds = new DataSet();
          
          FileStream fs = new FileStream(Server.MapPath("mydata.xml") ,
                                                                 FileMode.Open, FileAccess.Read);
          StreamReader reader = new StreamReader(fs);
          ds.ReadXml(reader);

          fs.close();

          DataView source = new DataView(ds.Tables[0]);

          mygrid.DataSource = source;
          mygrid.DataBind();
     }
</script>
<html>
<body>

<asp:datagrid id="mygrid" runat="server" />

</body>
</html>
 
 
Visitor Review Write a review
No visitor review for this page yet
Write a review
Rating:
Text:
Name:
Email:
(optional)
 
Code:
 

Search:
Shopping Store Online
Clothes (26)
Gift shop (69)
Hand-made (27)
Luxuries (47)
Others (44)
Programming Tutorial
ASP (14)
CSharp
Binding Data to List Control
Data Access with ADO.NET
DataGrid Control
The ArrayList class
The BitArray Class
The Calendar Class
The Hashtable Class
The HttpCookie Class
The HttpRequest Class
The HttpResponse Class
The Queue Class
The Repeater List Control
The SortedList Class
The SqlCommand Class
The SqlConnection Class
The SqlDataReader Class
The SqlParameter Class
The SqlParameterCollection Class
The SqlTransaction Class
HTML (12)
JavaScript (6)
Photoshop (10)
PHP (11)
Entertainment
Anime (6)
E-Cards (12)
Games (20)
Music (9)