Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binding datatable to grid view

I have the following 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?

like image 413
Abu Muhammad Avatar asked Jan 22 '23 19:01

Abu Muhammad


1 Answers

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

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 

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.

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

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

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 

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...

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.

like image 63
JonH Avatar answered Jan 26 '23 16:01

JonH