ASP.NET怎么操作DataTable實例應(yīng)用
有機會在博客園的博問頻道上看到一個問題,《ASP.NET怎么操作DataTable》:
如上圖,左邊的這個表是程序構(gòu)建出來的,不是數(shù)據(jù)庫表,怎么通過操作DataTable手段得到右邊的四個表?
Insus.NET嘗試做了一下,算是練習(xí)DataTable的功力了。效果如下:
根據(jù)最初數(shù)據(jù),Insus.NET在.aspx內(nèi)放置了一個Gridview,用來顯示最開始的數(shù)據(jù)。
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)建一個DataTable,并填充數(shù)據(jù):
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綁定這個DataTable:
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();
}
為了得到報表1,它有三個字段,名稱(Name),數(shù)量(Amount)和行數(shù)(Rowcount),Insus.NET還參考源數(shù)據(jù),它還有一個數(shù)量(Quantity)字段。因此,Insus.NET寫了一個類別Item,為以下導(dǎo)出報表作準(zhǔn)備:
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)在我們寫一個報表,在.aspx放在一個按鈕,以及一個GridView,來顯示報表,注意一個字段的綁定。
View Code
<asp:Button ID="ButtonReport1" runat="server" Text="報表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 寫click事件:
View Code
protected void ButtonReport1_Click(object sender, EventArgs e)
{
SortedList<string, Item> _sl = new SortedList<string, 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();
}
第一份報表,大功告成,只要DataTable數(shù)源數(shù)據(jù)有變化,報表也會隨之變化。
接下來,完成第二個報表,在Insus.NET使用Repeater包含Repeater來實現(xiàn)。因此,前臺Html代碼如下,其中第一個Repeate內(nèi)放置了一個HiddenField,來存儲名稱(Name)字段,當(dāng)作子Repeater的參考傳入。
View Code
<asp:Button ID="ButtonReport2" runat="server" Text="報表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)唯一的記錄存儲起來,作為第一個Repeate控件的數(shù)據(jù)集數(shù)據(jù)源。
View Code
protected void ButtonReport2_Click(object sender, EventArgs e)
{
this.Repeater1.DataSource = Names();
this.Repeater1.DataBind();
}
List<string> Names()
{
List<string> t = new List<string>();
DataTable otable = GetData();
foreach (DataRow dr in otable.Rows)
{
if (!t.Contains(dr["Name"].ToString()))
t.Add(dr["Name"].ToString());
}
return t;
}
我們還要寫第二個Repeater控件的數(shù)據(jù)源:
View Code
List<Item> GetDataByName(string name)
{
List<Item> o = new List<Item>();
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;
}
為第二個Repeater控件綁定數(shù)據(jù)源,在綁寫之前,得先找到這個控件,因此,你需要在第一個Repeater控件寫OnItemDataBound="Repeater1_ItemDataBound"事件:
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();
}
}
}
相關(guān)文章
asp.net String.format中大括號的加入方法
String.format中大括號的加入方法,需要的朋友可以參考下。2010-05-05asp.net core razor自定義taghelper的方法
這篇文章主要介紹了asp.net core razor自定義taghelper的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09.net core 靜態(tài)類獲取appsettings的方法
這篇文章主要介紹了.net core 靜態(tài)類獲取appsettings的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06ASP.NET ashx實現(xiàn)無刷新頁面生成驗證碼
這篇文章主要為大家詳細介紹了ASP.NET ashx實現(xiàn)無刷新頁面生成驗證碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09asp.net使用ajaxFileUpload插件上傳文件(附源碼)
本文詳細講解了asp.net使用ajaxFileUpload插件上傳文件,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12.net全局定時定期執(zhí)行某些操作在Global.asax中具體實現(xiàn)
全局定時定期執(zhí)行某些操作看起來是多么自動化的一個問題不過在.net的Global.asax文件中稍微配置即可實現(xiàn),詳細配置如下,感興趣的朋友可以參考下哈2013-04-04asp.net jquery無刷新分頁插件(jquery.pagination.js)
采用Jquery無刷新分頁插件jquery.pagination.js 實現(xiàn)無刷新分頁效果:本示例Handler中采用StringBuilder的append方法追加HTML,小數(shù)據(jù)量可以,但是大數(shù)據(jù)或是布局常變,建議返回JSON格式的數(shù)據(jù),性能和靈活性更好,望使用者好好把握2013-01-01