熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

ASP.NET怎麼操作DataTable實例應用

2022-06-13   來源: ASP編程 
有機會在博客園的博問頻道上看到一個問題《ASPNET怎麼操作DataTable》


如上圖左邊的這個表是程序構建出來的不是數據庫表怎麼通過操作DataTable手段得到右邊的四個表?

InsusNET嘗試做了一下算是練習DataTable的功力了效果如下


根據最初數據InsusNET在aspx內放置了一個Gridview用來顯示最開始的數據

  復制代碼 代碼如下:

  View Code


<asp:GridView ID=GridView 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>


創建一個DataTable並填充數據

  復制代碼 代碼如下:

  View Code

DataTable GetData()
{
DataTable table = new DataTable();
tableColumnsAdd(Name typeof(string));
tableColumnsAdd(Quantity typeof(int));
tableRowsAdd(a );
tableRowsAdd(a );
tableRowsAdd(b );
tableRowsAdd(b );
tableRowsAdd(c );
tableRowsAdd(c );
tableRowsAdd(c );
tableRowsAdd(c );
return table;
}


然後為剛才創建的Gridview綁定這個DataTable:

  復制代碼 代碼如下:

  View Code

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

private void Data_Binding()
{
thisGridViewDataSource = GetData();
thisGridViewDataBind();
}


為了得到報表它有三個字段名稱(Name)數量(Amount)和行數(Rowcount)InsusNET還參考源數據它還有一個數量(Quantity)字段因此InsusNET寫了一個類別Item為以下導出報表作准備

  復制代碼 代碼如下:

  Item

using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;

/// <summary>
/// Summary description for Item
/// </summary>
namespace InsusNET
{
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 amountint rowCount)
{
this_Name = name;
this_Amount = amount;
this_RowCount = rowCount;
}
}
}


OK現在我們寫一個報表aspx放在一個按鈕以及一個GridView來顯示報表注意一個字段的綁定

  復制代碼 代碼如下:

  View Code

<asp:Button ID=ButtonReport runat=server Text=報表 OnClick=ButtonReport_Click />
<asp:GridView ID=GridView 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 ButtonReport_Click(object sender EventArgs e)
{
SortedList<string Item> _sl = new SortedList<string Item>();

DataTable otable = GetData();
foreach (DataRow dr in otableRows)
{
if (_slContainsKey(dr[Name]ToString()))
{
_sl[dr[Name]ToString()]Amount += ConvertToInt(dr[Quantity]);
_sl[dr[Name]ToString()]RowCount += ;
}
else
{
Item i = new Item(dr[Name]ToString() ConvertToInt(dr[Quantity]) );
_slAdd(dr[Name]ToString() i);
}
}

thisGridViewDataSource = _slValues;
thisGridViewDataBind();
}


第一份報表大功告成只要DataTable數源數據有變化報表也會隨之變化

接下來完成第二個報表在InsusNET使用Repeater包含Repeater來實現因此前台Html代碼如下其中第一個Repeate內放置了一個HiddenField來存儲名稱(Name)字段當作子Repeater的參考傳入

  復制代碼 代碼如下:

  View Code


<asp:Button ID=ButtonReport runat=server Text=報表 OnClick=ButtonReport_Click />
<asp:Repeater ID=Repeater runat=server OnItemDataBound=Repeater_ItemDataBound>
<ItemTemplate>
<asp:HiddenField ID=HiddenField runat=server Value=<%# ContainerDataItem %> />
<asp:Repeater ID=Repeater runat=server>
<HeaderTemplate>
<table border= cellspacing= cellpadding= style=margin: px; bordercollapse: 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控件的數據集數據源

  復制代碼 代碼如下:

  View Code


protected void ButtonReport_Click(object sender EventArgs e)
{
thisRepeaterDataSource = Names();
thisRepeaterDataBind();
}

List<string> Names()
{
List<string> t = new List<string>();
DataTable otable = GetData();
foreach (DataRow dr in otableRows)
{
if (!tContains(dr[Name]ToString()))
tAdd(dr[Name]ToString());
}
return t;
}


我們還要寫第二個Repeater控件的數據源

  復制代碼 代碼如下:

  View Code


List<Item> GetDataByName(string name)
{
List<Item> o = new List<Item>();
DataTable otable = GetData();
foreach (DataRow dr in otableRows)
{
if (name == dr[Name]ToString())
{
Item i = new Item(dr[Name]ToString() ConvertToInt(dr[Quantity]));
oAdd(i);
}
}
return o;
}


為第二個Repeater控件綁定數據源在綁寫之前得先找到這個控件因此你需要在第一個Repeater控件寫OnItemDataBound=Repeater_ItemDataBound事件

  復制代碼 代碼如下:

  View Code


protected void Repeater_ItemDataBound(object sender RepeaterItemEventArgs e)
{
if (eItemItemType == ListItemTypeItem || eItemItemType == ListItemTypeAlternatingItem)
{
if (eItemFindControl(HiddenField) != null && eItemFindControl(Repeater) != null)
{
var hiddenField = eItemFindControl(HiddenField) as HiddenField;
var repeater = eItemFindControl(Repeater) as Repeater;
repeaterDataSource = GetDataByName(hiddenFieldValue);
repeaterDataBind();
}
}
}


From:http://tw.wingwit.com/Article/program/ASP/201405/30980.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.