将数据表绑定到网格视图 [英] binding datatable to grid view

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

问题描述

我有以下代码:

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.

您已在代码中执行此操作:

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.

您将不得不使用会话变量或数据库来存储 dt 的状态.

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

以下是 C# 中的示例:

Here is an example in 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(); 
    }

和 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 

所以现在代码所做的是实例化一个新的数据表对象,只要我们在页面上(第一次回发)并添加列.一旦它定义了数据表,我们就将它放入某个会话状态.当您单击添加按钮时,您不能在之前的代码中继续使用 dt,因为 dt 范围在您之前的代码中丢失了.我们通过分配存储在临时数据表之前的会话数据表来做到这一点.我们添加行并在下次添加行时以这种方式重置会话,它将显示第二行,然后是第三行,依此类推...

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 书籍,例如 Beginning ASP.net 3.5 in 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天全站免登陆