主頁(yè) > 知識(shí)庫(kù) > ASP.NET怎么操作DataTable實(shí)例應(yīng)用

ASP.NET怎么操作DataTable實(shí)例應(yīng)用

熱門(mén)標(biāo)簽:銀行業(yè)務(wù) 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 電子圍欄 服務(wù)器配置 Linux服務(wù)器 阿里云 Mysql連接數(shù)設(shè)置 團(tuán)購(gòu)網(wǎng)站
有機(jī)會(huì)在博客園的博問(wèn)頻道上看到一個(gè)問(wèn)題,《ASP.NET怎么操作DataTable》:


如上圖,左邊的這個(gè)表是程序構(gòu)建出來(lái)的,不是數(shù)據(jù)庫(kù)表,怎么通過(guò)操作DataTable手段得到右邊的四個(gè)表?

Insus.NET嘗試做了一下,算是練習(xí)DataTable的功力了。效果如下:


根據(jù)最初數(shù)據(jù),Insus.NET在.aspx內(nèi)放置了一個(gè)Gridview,用來(lái)顯示最開(kāi)始的數(shù)據(jù)。

復(fù)制代碼 代碼如下:

View Code


asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
Columns>
asp:TemplateField>
HeaderTemplate>
Name
/HeaderTemplate>
ItemTemplate>
%# Eval("Name") %>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField>
HeaderTemplate>
Quantity
/HeaderTemplate>
ItemTemplate>
%# Eval("Quantity") %>
/ItemTemplate>
/asp:TemplateField>
/Columns>
/asp:GridView>

創(chuàng)建一個(gè)DataTable,并填充數(shù)據(jù):
復(fù)制代碼 代碼如下:

View Code

DataTable GetData()
{
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Quantity", typeof(int));
table.Rows.Add("a", 1);
table.Rows.Add("a", 2);
table.Rows.Add("b", 2);
table.Rows.Add("b", 2);
table.Rows.Add("c", 1);
table.Rows.Add("c", 2);
table.Rows.Add("c", 3);
table.Rows.Add("c", 4);
return table;
}

然后為剛才創(chuàng)建的Gridview綁定這個(gè)DataTable:
復(fù)制代碼 代碼如下:

View Code

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}

private void Data_Binding()
{
this.GridView1.DataSource = GetData();
this.GridView1.DataBind();
}

為了得到報(bào)表1,它有三個(gè)字段,名稱(Name),數(shù)量(Amount)和行數(shù)(Rowcount),Insus.NET還參考源數(shù)據(jù),它還有一個(gè)數(shù)量(Quantity)字段。因此,Insus.NET寫(xiě)了一個(gè)類別Item,為以下導(dǎo)出報(bào)表作準(zhǔn)備:
復(fù)制代碼 代碼如下:

Item

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// summary>
/// Summary description for Item
/// /summary>
namespace Insus.NET
{
public class Item
{
private string _Name;
private int _Quantity;
private int _Amount;
private int _RowCount;

public string Name
{
get { return _Name; }
set { _Name = value; }
}
public int Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}
public int Amount
{
get { return _Amount; }
set { _Amount = value; }
}
public int RowCount
{
get { return _RowCount; }
set { _RowCount = value; }
}

public Item()
{
//
// TODO: Add constructor logic here
//
}

public Item(string name, int quantity)
{
this._Name = name;
this._Quantity = quantity;
}

public Item(string name, int amount,int rowCount)
{
this._Name = name;
this._Amount = amount;
this._RowCount = rowCount;
}
}
}

OK,現(xiàn)在我們寫(xiě)一個(gè)報(bào)表,在.aspx放在一個(gè)按鈕,以及一個(gè)GridView,來(lái)顯示報(bào)表,注意一個(gè)字段的綁定。
復(fù)制代碼 代碼如下:

View Code

asp:Button ID="ButtonReport1" runat="server" Text="報(bào)表1" OnClick="ButtonReport1_Click" />
asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false">
Columns>
asp:TemplateField>
HeaderTemplate>
Name
/HeaderTemplate>
ItemTemplate>
%# Eval("Name") %>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField>
HeaderTemplate>
Amount
/HeaderTemplate>
ItemTemplate>
%# Eval("Amount") %>
/ItemTemplate>
/asp:TemplateField>
asp:TemplateField>
HeaderTemplate>
RowCount
/HeaderTemplate>
ItemTemplate>
%# Eval("RowCount") %>
/ItemTemplate>
/asp:TemplateField>
/Columns>
/asp:GridView>

在.cs 寫(xiě)click事件:
復(fù)制代碼 代碼如下:

View Code


protected void ButtonReport1_Click(object sender, EventArgs e)
{
SortedListstring, Item> _sl = new SortedListstring, Item>();

DataTable otable = GetData();
foreach (DataRow dr in otable.Rows)
{
if (_sl.ContainsKey(dr["Name"].ToString()))
{
_sl[dr["Name"].ToString()].Amount += Convert.ToInt32(dr["Quantity"]);
_sl[dr["Name"].ToString()].RowCount += 1;
}
else
{
Item i = new Item(dr["Name"].ToString(), Convert.ToInt32(dr["Quantity"]), 1);
_sl.Add(dr["Name"].ToString(), i);
}
}

this.GridView2.DataSource = _sl.Values;
this.GridView2.DataBind();
}

第一份報(bào)表,大功告成,只要DataTable數(shù)源數(shù)據(jù)有變化,報(bào)表也會(huì)隨之變化。

接下來(lái),完成第二個(gè)報(bào)表,在Insus.NET使用Repeater包含Repeater來(lái)實(shí)現(xiàn)。因此,前臺(tái)Html代碼如下,其中第一個(gè)Repeate內(nèi)放置了一個(gè)HiddenField,來(lái)存儲(chǔ)名稱(Name)字段,當(dāng)作子Repeater的參考傳入。
復(fù)制代碼 代碼如下:

View Code


asp:Button ID="ButtonReport2" runat="server" Text="報(bào)表2" OnClick="ButtonReport2_Click" />
asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
ItemTemplate>
asp:HiddenField ID="HiddenField1" runat="server" Value='%# Container.DataItem %>' />
asp:Repeater ID="Repeater2" runat="server">
HeaderTemplate>
table border="1" cellspacing="0" cellpadding="5" style="margin: 10px; border-collapse: collapse;">
tr>
td>Name/td>
td>Quantity/td>
/tr>
/HeaderTemplate>
ItemTemplate>
tr>
td>%# Eval("Name") %>/td>
td>%# Eval("Quantity") %>/td>
/tr>
/ItemTemplate>
FooterTemplate>
/table>
/FooterTemplate>
/asp:Repeater>
/ItemTemplate>
/asp:Repeater>

首先,我們需要從DataTable,獲取名稱(Name)唯一的記錄存儲(chǔ)起來(lái),作為第一個(gè)Repeate控件的數(shù)據(jù)集數(shù)據(jù)源。
復(fù)制代碼 代碼如下:

View Code


protected void ButtonReport2_Click(object sender, EventArgs e)
{
this.Repeater1.DataSource = Names();
this.Repeater1.DataBind();
}

Liststring> Names()
{
Liststring> t = new Liststring>();
DataTable otable = GetData();
foreach (DataRow dr in otable.Rows)
{
if (!t.Contains(dr["Name"].ToString()))
t.Add(dr["Name"].ToString());
}
return t;
}

我們還要寫(xiě)第二個(gè)Repeater控件的數(shù)據(jù)源:
復(fù)制代碼 代碼如下:

View Code


ListItem> GetDataByName(string name)
{
ListItem> o = new ListItem>();
DataTable otable = GetData();
foreach (DataRow dr in otable.Rows)
{
if (name == dr["Name"].ToString())
{
Item i = new Item(dr["Name"].ToString(), Convert.ToInt32(dr["Quantity"]));
o.Add(i);
}
}
return o;
}

為第二個(gè)Repeater控件綁定數(shù)據(jù)源,在綁寫(xiě)之前,得先找到這個(gè)控件,因此,你需要在第一個(gè)Repeater控件寫(xiě)OnItemDataBound="Repeater1_ItemDataBound"事件:
復(fù)制代碼 代碼如下:

View Code


protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.FindControl("HiddenField1") != null e.Item.FindControl("Repeater2") != null)
{
var hiddenField = e.Item.FindControl("HiddenField1") as HiddenField;
var repeater = e.Item.FindControl("Repeater2") as Repeater;
repeater.DataSource = GetDataByName(hiddenField.Value);
repeater.DataBind();
}
}
}
您可能感興趣的文章:
  • asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法
  • asp.net實(shí)現(xiàn)數(shù)據(jù)從DataTable導(dǎo)入到Excel文件并創(chuàng)建表的方法
  • Asp.net中DataTable導(dǎo)出到Excel的方法介紹
  • asp.net 讀取Excel數(shù)據(jù)到DataTable的代碼
  • ASP.NET DataTable去掉重復(fù)行的2種方法
  • ASP.NET中DataTable與DataSet之間的轉(zhuǎn)換示例
  • Asp.net下使用Jquery Ajax傳送和接收DataTable的代碼
  • asp.net 數(shù)據(jù)庫(kù)的連接和datatable類
  • Asp.net靜態(tài)方法之Grid轉(zhuǎn)DataTable方法實(shí)現(xiàn)步驟
  • Asp.net實(shí)現(xiàn)選擇性的保留DataTable中的列
  • asp.net DataTable導(dǎo)出Excel自定義列名的方法

標(biāo)簽:衢州 江蘇 蚌埠 廣元 萍鄉(xiāng) 大理 棗莊 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET怎么操作DataTable實(shí)例應(yīng)用》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266