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; usi...
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.";
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();
}
///
/// GridView1的数据加载
///
private void addGridView1()
{
al.Clear();//清除al
string[] dtd = dtdata.Split(',');
addArrayList(dtd);
addDataTableStruct();
addDataTableData(al);
GridView1.DataSource = dt;
GridView1.DataBind();
}
///
/// DropDownList1数据加载
///
private void addDropDowmList1()
{
string[] zg = zhongguo.Split(',');
addArrayList(zg);
foreach (string m in al)
{
DropDownList1.Items.Add(m);
}
}
///
/// 把数组中的内容填到al(Array)中
///
/// 数组名称
private void addArrayList(string[] array)
{
for (int i = 0; i < array.Length; i++)
{
al.Add(array[i]);
}
}
///
/// 添加DataTable的列结构
///
private void addDataTableStruct()
{
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Tel");
}
///
/// 往dt(DataTable)中添加数据
///
/// 包含大量横排的数据,像数组
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;
}
}
}