开发者

Binding gridview with arraylist asp.net/c#

I have created 开发者_StackOverflow社区a grid view, can I bind that grid view columns with a array list ?

If it is possible can you please write a sample code.

Thank you in anticipation


Yes,it is possible. You have to specify columns in you grid view, bind them to some expression, depending on your needs and contents of the ArrayList (what kind of objects do you sotre in it?) and then just type in your code behind:

myGrid.DataSource = myArrayList;
myGrid.Databind();


After creating an ArrayList, you just add the arraylist into a datatable and bind the datatable to the Gridview. Then in the design page you mention AutoGenerateColumns property as "true".

Like the following code: In design page:

<asp:GridView ID="gvarray" runat="server" Width="328px">
     <Columns>
          <asp:TemplateField HeaderText="Select One">
              <ItemTemplate>
                  <asp:Label ID = "rdoday" runat = "server" text = '<%# DataBinder.Eval (Container.DataItem, "Item") %>' />

              </ItemTemplate>
          </asp:TemplateField>
     </Columns>

In code-behind:

ArrayList Array_L = new ArrayList();
        Array_L.Add("One");
        Array_L.Add("two");
        Array_L.Add(2);
DataTable dt = new DataTable();
dt.Columns.Add("Name");
for (int i = 0; i < Array_L .Count();i++)
{
    dt.Rows.Add();
    dt.Rows[i]["Name"] = Array_L [i].ToString();
}
gvarray.DataSource = dt;
gvarray.DataBind();


You can create a new ArrayList to get all the data from SqlDataSource. Then, you can use data table to insert data from ArrayList into specified columns and rows of grid view.

The following is the code behind file:

ArrayList myList = sampleController.getDataFromTable();
        DataTable dataTable = new DataTable();
        DataColumn dataColumn;
        dataColumn = new DataColumn("UserID");
        dataTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("Username");
        dataTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("MobileNo");
        dataTable.Columns.Add(dataColumn);
        dataColumn = new DataColumn("Address");
        dataTable.Columns.Add(dataColumn);

        foreach (object str in myList)
        {
            Customer customer= new Customer();
            customer= (Customer)str;
            DataRow dataRow = dataTable.NewRow();
            dataRow[0] = customer.DailyRunNo;
            dataRow[1] = customer.Area.Name;
            dataRow[2] = customer.Employee.EmployeeName;
            dataRow[3] = customer.SalStatus;

            dataTable.Rows.Add(dataRow);
        }
        gvCustomer.DataSource = dataTable;
        gvCustomer.DataBind();//Bind datatable

The following is in ASPX design page:

<asp:TemplateField HeaderText="User ID">
                        <ItemTemplate>
                            <asp:Label ID="lblDailyRunNo" runat="server" Text='<%# Eval("UserID") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="UserName">
                        <ItemTemplate>
                            <asp:Label ID="lblAreaNo" runat="server" Text='<%# Eval("Username") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Mobile Number">
                        <ItemTemplate>
                            <asp:Label ID="lblSalespersonName" runat="server" Text='<%# Eval("MobileNo") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Address">
                        <ItemTemplate>
                            <asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Address") %>' />
                        </ItemTemplate>
                    </asp:TemplateField>


It is not logical to use ArrayList when data to be bind is in tabular structure. You use ArrayList at that moment when data need to bind with Dropdown or ListView Controls.

If you want to bind data to gridview / datalist / DataList controls then better option you should use DataTable/Dataset or you can use LIST ex: LIST.

Here i have one Example for Datatable with gridview.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections;

namespace TestApp
{
     public partial class MyWebPage: System.Web.UI.Page         
     {
         static string GetConnectionString()
         {
             return ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString;
         }
         protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
             {
                BindData();
             }
         }

         private void BindData()
         {
            string connstring = GetConnectionString();
            using(SqlConnection cn = new SqlConnection())
            {
                string query = "your sql query";
                SqlDataAdapter da = new SqlDataAdapter(query,cn);
                cn.Open();
                DataTable dtMyTable = new DataTable();
                da.Fill(dtMyTable);
                GridView1.DataSource = dtMyTable;
                GridView1.DataBind();
              }
            }
      }
  }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜