Asp. NET how to operate a DataTable instance application _ Practical skills

Source: Internet
Author: User
Tags rowcount
There is a chance to see a problem on the blog, "ASP." NET how to operate the DataTable:


As shown above, the table on the left is built by the program, not the database table, how to get the four tables on the right by manipulating the DataTable method?

Insus.net tried to do it, it was a skill to practice the DataTable. The effect is as follows:


Based on the initial data, Insus.net placed a GridView in. aspx to display the first data.

Copy Code code as follows:

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>

Create a DataTable and populate the data:
Copy Code code as follows:

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;
}

Then bind this DataTable to the GridView you just created:
Copy Code code as follows:

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 ();
}

In order to get report 1, it has three fields, name (name), quantity (Amount) and number of rows (rowcount), Insus.net also reference source data, and it also has a quantity (Quantity) field. Therefore, Insus.net writes a category item to prepare for the following export report:
Copy Code code as follows:

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, now we're writing a report, in. aspx put in a button, and a GridView, to display the report, note the binding of a field.
Copy Code code as follows:

View Code

<asp:button id= "ButtonReport1" runat= "Server" text= "Report 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>

In the. CS Write Click event:
Copy Code code as follows:

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 ();
}

The first report, the success, as long as the number of DataTable source data changes, the report will change.

Next, complete the second report, which is implemented in insus.net using repeater containing repeater. Therefore, the foreground HTML code is as follows, where the first repeate place a HiddenField to store the Name field, which is passed as a reference to the child repeater.
Copy Code code as follows:

View Code


<asp:button id= "ButtonReport2" runat= "Server" text= "Report 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>

First, we need to store the record of the name unique from the DataTable, as the dataset data source for the first repeate control.
Copy Code code as follows:

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;
}

And we're going to write a second repeater control's data source:
Copy Code code as follows:

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;
}

To bind the data source for the second repeater control, you must first locate the control before binding, so you need to write the onitemdatabound= "Repeater1_itemdatabound" event in the first Repeater control:
Copy Code code as follows:

View Code


protected void Repeater1_itemdatabound (object Sende R, RepeaterItemEventArgs e)
{
if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = Listitemtype.alte Rnatingitem)
{
if (E.item.findcontrol ("HiddenField1")!= null && e.item.findcontrol ("Repeater2")!= nu ll)
{
var HiddenField = E.item.findcontrol ("HiddenField1") as HiddenField;
var repeater = E.item.findcontrol ("Repeater2") as repeater;
Repeater. DataSource = Getdatabyname (Hiddenfield.value);
Repeater. DataBind ();
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.