[NET]如何把Databean List邦定到Grid

如何把Databean List邦定到Grid

问题:

习惯用Java编写3层框架程序的人都会提出这样的疑问:能否把现行的Java框架移植到dotNet中来。回答是肯定的。

在移植过程中我们首先要遇到的问题是,在Java中我们Databean来传递数据,而dotNet中调用Ado.NET返回值是DataTable或DataSet,并且Grid控件的DataSource数据也能直接接收DataTable或DataSet对象。所以目前很多人都用 DataTable或DataSet对象作为数据传输对象。

用 DataTable或DataSet对象作为数据传输对象有一个缺点,当我们要对DataTable或DataSet对象内的数据作处理时要用行号和列名去取得值,导致数据访问层和业务逻辑层,表示层联系紧密,无法做到彻底分离,逻辑混乱。

解决方法:

首先在数据访问层把DataTable或DataSet对象转换成Databean的List,然后再用Databean的List直接邦定到Grid控件上。

例子:

把DataTable或DataSet对象转换成Databean的List的例子等到框架做好后再公布。现在先给出用Databean的List直接邦定到Grid控件上的例子。

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>無題のページ</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:DataGrid   id="DataGrid1"   runat="server"   Width="529px"   Height="208px"   AutoGenerateColumns="False">  
      <Columns>  
      <asp:BoundColumn   HeaderText="a1"   DataField="product_id"></asp:BoundColumn>  
      <asp:BoundColumn   HeaderText="a2"   DataField="product_nm"></asp:BoundColumn>  
      </Columns>  
      </asp:DataGrid>  
        <asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
    </form>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //   在此处放置用户代码以初始化页面  
        Test t = new Test();
        t.LoadData();
        this.DataGrid1.DataSource = t.RowCollection;
        this.DataGrid1.DataBind();

        this.DropDownList1.DataTextField = "Product_nm";
        this.DropDownList1.DataValueField = "Product_id";
        this.DropDownList1.DataSource = t.RowCollection;
        this.DropDownList1.DataBind();
    }
}

public class RowData
{
    private   string product_id;
    private string product_nm;
    public string Product_id
    {
        get { return product_id; }
        set { product_id = value; }
    }
    public string Product_nm
    {
        get { return product_nm; }
        set { product_nm = value; }
    }

    public RowData(string id, string des)
    {
        this.Product_id = id;
        this.Product_nm = des;
    }
}

class Test
{
    private List<RowData> m_RowDataCollection;
    public List<RowData> RowCollection
    {
        set { m_RowDataCollection = value; }
        get { return m_RowDataCollection; }
    }

    public void LoadData()
    {
        this.RowCollection = new List<RowData>();
        this.RowCollection.Add(new RowData("7201", "aaaaaa"));
        this.RowCollection.Add(new RowData("7202", "aaaaaa"));
        this.RowCollection.Add(new RowData("7203", "aaaaaa"));
        this.RowCollection.Add(new RowData("7204", "aaaaaa"));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值