GridView与DropDownList数据加载的例子

发布时间:2020-10-11编辑:脚本学堂
为大家介绍一个GridView与DropDownList数据加载的例子,供大家学习参考。

gridview数据加载的完整代码。
 

复制代码 代码示例:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class zsl_0305 : System.Web.UI.Page
{
    string zhongguo="脚本学堂,欢迎大家的光临。http://www.jb200.com";
    string dtdata = "1,小青,15896636,2,小红,2566987,3,小刚,2369887";
    ArrayList al = new ArrayList();
    DataTable dt = new DataTable();
  
    protected void Page_Load(object sender, EventArgs e)
    {

        addDropDowmList1();

        addGridView1();
    }

    /// <summary>
    /// GridView1的数据加载
    /// </summary>
    private void addGridView1()
    {
        al.Clear();//清除al
        string[] dtd = dtdata.Split(',');
        addArrayList(dtd);
        addDataTableStruct();
        addDataTableData(al);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    /// <summary>
    /// DropDownList1数据加载
    /// </summary>
    private void addDropDowmList1()
    {
        string[] zg = zhongguo.Split(',');
        addArrayList(zg);
        foreach (string m in al)
        {
            DropDownList1.Items.Add(m);
        }
    }
    /// <summary>
    /// 把数组中的内容填到al(Array)中
    /// </summary>
    /// <param name="array">数组名称</param>
    private void addArrayList(string[] array)
    {
        for (int i = 0; i < array.Length; i++)
        {
            al.Add(array[i]);
        }
    }
    /// <summary>
    /// 添加DataTable的列结构
    /// </summary>
    private void addDataTableStruct()
    {
        dt.Columns.Add("ID");
        dt.Columns.Add("Name");
        dt.Columns.Add("Tel");
    }
    /// <summary>
    /// 往dt(DataTable)中添加数据
    /// </summary>
    /// <param name="e">包含大量横排的数据,像数组</param>
    private void addDataTableData(ArrayList e)
    {
        string[] rowsingle = new string[3];
        for (int i = 0; i < e.Count;)
        {
            rowsingle[0] = e[i].ToString();
            rowsingle[1] = e[i+1].ToString();
            rowsingle[2] = e[i+2].ToString();
            dt.Rows.Add(rowsingle);
            i = i + 3;
        }
    }
}