ASP.Net/Data Binding/DataGrid

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Bind ArrayList to asp datagrid (C#)

   <source lang="csharp">

<%@Page Language="C#"%> <html> <body> <asp:datagrid id="dgrTest" runat="server"

              forecolor="#000000"
              backcolor="#ffffff"
              cellpadding="3"
              gridlines="none"
              width="50%"
              font-name="tahoma,arial,sans-serif"
              font-size="14px">
 <headerstyle forecolor="#ffffff"
              backcolor="#808080"
              font-name="tahoma,arial,sans-serif"
              font-size="12px"
              font-bold="true"
              wrap="false" />
 <itemstyle   font-name="tahoma,arial,sans-serif"
              font-size="10px" />
 <alternatingitemstyle font-name="tahoma,arial,sans-serif"
              font-size="16px"
              backcolor="#ffff00" />

</asp:datagrid> </body> </html> <script Language="C#" runat="server"> void Page_Load() {

  // create an ArrayList of values to bind to
  ArrayList arrValues = new ArrayList(4);
  arrValues.Add("A");
  arrValues.Add("B");
  arrValues.Add("C");
  arrValues.Add("D");
  arrValues.Add("E");
  // set the DataSource property of the control to the
  // array and bind it to display the values
  dgrTest.DataSource = arrValues;
  dgrTest.DataBind();

} </script>

      </source>
   
  


Bind data to asp:datagrid (VB.net)

   <source lang="csharp">

<%@ Page Language="vb" %> <%@ Import Namespace="System.Data" %> <html> <head>

  <title>Databound Control Example</title>
  <script runat="server">
     Sub Page_Load()
        MyDataGrid.DataSource = CreateData()
        MyDataGrid.DataBind()
        MyDataList.DataSource = CreateData()
        MyDataList.DataBind()
        MyRepeater.DataSource = CreateData()
        MyRepeater.DataBind()
     End Sub
     Function CreateData() As DataTable
        Dim DT As New DataTable()
        Dim Row1, Row2, Row3, Row4 As DataRow
        DT.Columns.Add(New DataColumn("name", System.Type.GetType("System.String")))
        DT.Columns.Add(New DataColumn("city", System.Type.GetType("System.String")))
        Row1 = DT.NewRow()
        Row1("name") = "Name 1"
        Row1("city") = "City 1"
        DT.Rows.Add(Row1)
        Row2 = DT.NewRow()
        Row2("name") = "Name 2"
        Row2("city") = "City 2"
        DT.Rows.Add(Row2)
        Row3 = DT.NewRow()
        Row3("name") = "Name 3"
        Row3("city") = "City 3"
        DT.Rows.Add(Row3)
        Row4 = DT.NewRow()
        Row4("name") = "Name 4"
        Row4("city") = "City 4"
        DT.Rows.Add(Row4)
        Return DT
     End Function
  </script>

</head> <body>

Databound Control Example

  <form runat="server">
     <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
        <asp:tablerow runat="server">
           <asp:tablecell runat="server">
              DataGrid Control:
           </asp:tablecell>
           <asp:tablecell runat="server">
              <asp:datagrid id="MyDataGrid"
                 allowpaging="true"
                 allowsorting="true"
                 alternatingitemstyle-backcolor="LightSkyBlue"
                 backcolor="Blue"
                 forecolor="White"
                 cellpadding="2"
                 cellspacing="0"
                 headerstyle-backcolor="DarkBlue"
                 headerstyle-forecolor="Yellow"
                 pagerstyle-mode="NumericPages"
                 pagesize="5"
                 runat="server"/>
           </asp:tablecell>
        </asp:tablerow>
        <asp:tablerow runat="server">
           <asp:tablecell runat="server">
              DataList Control:
           </asp:tablecell>
           <asp:tablecell runat="server">
              <asp:datalist id="MyDataList"
                 alternatingitemstyle-backcolor="LightSkyBlue"
                 backcolor="Blue"
                 bordercolor="Black"
                 cellpadding="2"
                 cellspacing="0"
                 forecolor="White"
                 headerstyle-backcolor="DarkBlue"
                 headerstyle-forecolor="Yellow"
                 repeatcolumns="1"
                 repeatdirection="vertical"
                 repeatlayout="table" 
                 runat="server">
                 <headertemplate>
                    Composers
                 </headertemplate>
                 <itemtemplate>
                    <%# databinder.eval(container.dataitem, "name") %>
                 </itemtemplate>
              </asp:datalist>
           </asp:tablecell>
        </asp:tablerow>
        <asp:tablerow runat="server">
           <asp:tablecell runat="server">
              Repeater Control:
           </asp:tablecell>
           <asp:tablecell runat="server">
              <asp:repeater id="MyRepeater" runat="server">
                 <headertemplate>
</headertemplate> <itemtemplate> </itemtemplate> <footertemplate>
Name
City
<%# DataBinder.Eval(Container.DataItem, _ "name") %> <%# DataBinder.Eval(Container.DataItem, _ "city") %>
                 </footertemplate>
              </asp:repeater>
           </asp:tablecell>
        </asp:tablerow>
     </asp:table>
  </form>

</body> </html>

      </source>
   
  


Bind hashtable to asp datagrid (C#)

   <source lang="csharp">

<%@Page Language="C#"%> <html> <body> <asp:datagrid id="dgrTest" runat="server"

              autogeneratecolumns="false"
              forecolor="#000000"
              backcolor="#ffffff"
              cellpadding="3"
              gridlines="none"
              width="50%"
              font-name="tahoma,arial,sans-serif">
 <headerstyle forecolor="#ffffff"
              backcolor="#808080"
              font-name="tahoma,arial,sans-serif"
              font-size="12px"
              font-bold="true"
              wrap="false" />
 <itemstyle   font-name="tahoma,arial,sans-serif" />
 <alternatingitemstyle font-name="tahoma,arial,sans-serif"
              backcolor="#ffff00" />
 <columns>
   <asp:boundcolumn headertext="Key" datafield="Key" dataformatstring="{0}" />
   <asp:boundcolumn headertext="Value" datafield="Value" dataformatstring="${0:f2}" />
 </columns>

</asp:datagrid> </body> </html> <script Language="C#" runat="server"> void Page_Load() {

  // create a HashTable of values to bind to
  Hashtable tabValues = new Hashtable(4);
  tabValues.Add("A", 49.56);
  tabValues.Add("B", 28.33);
  tabValues.Add("C", 55);
  tabValues.Add("D", 20.74);
  tabValues.Add("E", 41.1);
  // set the DataSource property of the control to the
  // hashtable and bind it to display the values
  dgrTest.DataSource = tabValues;
  dgrTest.DataBind();

} </script>

      </source>