结合数据表到网格视图 [英] binding datatable to grid view

查看:110
本文介绍了结合数据表到网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

Imports System.Data

Partial Class Students_AddWishes Inherits System.Web.UI.Page

    Public dt As New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dt.Columns.Add("ID", System.Type.GetType("System.Int32"))
        dt.Columns.Add("univirsity", System.Type.GetType("System.Int32"))
        dt.Columns.Add("major", System.Type.GetType("System.Int32"))
    End Sub

    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim row1 As DataRow = dt.NewRow()
        row1("ID") = dt.Rows.Count + 1
        row1("univirsity") = ddlUnivs.SelectedValue
        row1("major") = ddlMajors.SelectedValue
        dt.Rows.Add(row1)
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub

End Class

问题是,它仅显示一个行或记录。如何使它显示多条记录?

The problem is it shows only one row or record. How to make it shows many records?

推荐答案

你不检查你的页面加载事件,如果它是一个回:

Your page load event you are not checking if it is a post back:

 If Not IsPostBack Then
     'process code if it is not a post back
 End If

每当你点击btnAdd按钮,你的页面做了回发到服务器。

Everytime you click the btnAdd button your page does a post back to the server.

我只注意到你可能不理解对象的续航时间。

I just noticed that you probably are not understanding the life time of an object.

您已经在code做到了这一点:

You had done this in your code:

Public dt As New DataTable 

但问题是你必须定义这是一个类变量,一旦在页面加载你有DT型的实例,可能有与之相关的一些列。但只要你记录一个事件如按钮单击引用被销毁,创建一个新的DT。

The problem with that is you have defined this is a class variable and once the page has loaded you have an instance of type dt that may have some columns associated with it. But as soon as you record an event such as a button click that reference is destroyed and a new dt is created.

您将不得不作出一些使用session变量或数据库来存储DT的状态。

You will have to make some use of session variables or a database to store the state of dt.

下面是在C#示例:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
            dt.Columns.Add("univirsity", System.Type.GetType("System.Int32"));
            dt.Columns.Add("major", System.Type.GetType("System.Int32"));
            Session["MyDataTable"] = dt;
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        DataTable t = (DataTable)Session["MyDataTable"];
        DataRow row1 = t.NewRow();

        row1["ID"] = t.Rows.Count + 1;
        row1["univirsity"] = 3;
        row1["major"] = 31;
        t.Rows.Add(row1);

        Session["MyDataTable"] = t;
        GridView1.DataSource = t;
        GridView1.DataBind(); 
    }

和同一code在vb.net:

And the same code in vb.net:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
    If Not Page.IsPostBack Then 
        Dim dt As New DataTable() 
        dt.Columns.Add("ID", System.Type.[GetType]("System.Int32")) 
        dt.Columns.Add("univirsity", System.Type.[GetType]("System.Int32")) 
        dt.Columns.Add("major", System.Type.[GetType]("System.Int32")) 
        Session("MyDataTable") = dt 
    End If 
End Sub 

Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) 
    Dim t As DataTable = DirectCast(Session("MyDataTable"), DataTable) 
    Dim row1 As DataRow = t.NewRow() 

    row1("ID") = t.Rows.Count + 1 
    row1("univirsity") = 3 
    row1("major") = 31 
    t.Rows.Add(row1) 

    Session("MyDataTable") = t 
    GridView1.DataSource = t 
    GridView1.DataBind() 
End Sub 

所以,现在什么code的作用是实例化一个新的DataTable对象,只要我们在页面上(第一回发),并添加列。一旦它定义数据表,我们把它扔在一些会话状态。当您单击Add按钮,你不能在你的previous code只是继续使用DT因为DT范围是在你之前的code丢失。我们通过分配这是之前一个临时存储数据表的数据表sessioned做到这一点。我们增加了一行,下一次我们增加一个排它会显示第二行,然后第三行,并重新设置会话方式等等...

So now what the code does is instantiate a new datatable object as long as we are on the page (first post back) and adds the columns. Once it has defined the data table we throw it in some session state. When you click the add button you cannot in your previous code just keep using dt because dt scope was lost in your prior code. We do this by assigning the sessioned datatable which was stored prior to a temp datatable. We add the row and reset the session that way the next time we add a row it will display the second row, then third row, and so on...

我推荐一个好的asp.net书,如开始ASP.net 3.5在C#2008上有相同主题提出吨vb.net书籍。

I recommend a good asp.net book on such as Beginning ASP.net 3.5 in C# 2008. There are a ton of vb.net books on the same subject matter.

这篇关于结合数据表到网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆