出自:【孟宪会之精彩世界】(VB.NET版)
VB.NET版的地址:
http://dotnet.aspx.cc/ShowDetail.aspx?id=175C1EA1-A261-45D3-B5AC-94F6CACCDDEF
前台代码:
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="datagrid_add.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>AddDataSetColumn</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" ShowFooter="True" Width="100%">
<HeaderStyle Font-Names="宋体" Font-Bold="True" HorizontalAlign="Center" ForeColor="Navy" BackColor="LightSalmon"></HeaderStyle>
<FooterStyle BackColor="#FFCCFF"></FooterStyle>
<Columns>
<asp:TemplateColumn HeaderText="订单号">
<ItemTemplate>
<asp:Label ID="OrderID" Runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="产品号">
<ItemTemplate>
<asp:Label ID="ProductID" Runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="单价">
<ItemTemplate>
<asp:Label ID="UnitPrice" Runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="数量">
<ItemTemplate>
<asp:Label ID="Quantity" Runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="折扣">
<ItemTemplate>
<asp:Label ID="Discount" Runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="总计">
<ItemTemplate>
<asp:Label ID="lblTotal" Runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
后台代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace datagrid_add
{
/// <summary>
/// WebForm3 的摘要说明。
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection myConn=new SqlConnection("server=field;database=northwind;uid=sa;pwd=123456");
SqlDataAdapter sa=new SqlDataAdapter("select * from [Order Details]",myConn);
DataSet ds=new DataSet();
sa.Fill(ds,"DS");
DataView dv=ds.Tables["DS"].DefaultView;
DataGrid1.DataSource=dv;
DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType elemType = e.Item.ItemType;
if ((elemType == ListItemType.Item | elemType == ListItemType.AlternatingItem))
{
DataRowView myDataRowView = ((DataRowView)(e.Item.DataItem));
DataRow myDataRow = myDataRowView.Row;
((Label)(e.Item.Cells[0].FindControl("OrderID"))).Text = myDataRow["OrderID"].ToString();
((Label)(e.Item.Cells[1].FindControl("ProductID"))).Text = myDataRow["ProductID"].ToString();
((Label)(e.Item.Cells[2].FindControl("UnitPrice"))).Text = myDataRow["UnitPrice"].ToString();
((Label)(e.Item.Cells[3].FindControl("Quantity"))).Text = myDataRow["Quantity"].ToString();
((Label)(e.Item.Cells[4].FindControl("Discount"))).Text = myDataRow["Discount"].ToString();
double dblPrice = double.Parse(myDataRow["UnitPrice"].ToString());
int intQuantity = Int32.Parse(myDataRow["Quantity"].ToString());
double intDiscount = double.Parse(myDataRow["Discount"].ToString());
string nTotal = string.Format("{0:C}", dblPrice * (1 - intDiscount) * intQuantity);
((Label)(e.Item.Cells[5].FindControl("lblTotal"))).Text = nTotal;
}
}
}
}
浙公网安备 33010602011771号