Creating an unbound datagrid


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Creating an unbound datagrid

  1. #1
    Join Date
    Mar 2005
    Posts
    53

    Creating an unbound datagrid

    I need to create a datagrid that contains 3 columns.
    Column1: Day of Week

    Column2: StartTime [This needs to contain 3 drop downs. ddlHour (Values 1-12), ddlTime (Values AM/PM), ddlMin (Values 00,15,30,45)]

    Column3: EndTime [This needs to contain 3 drop downs. ddlHour (Values 1-12), ddlTime (Values AM/PM), ddlMin (Values 00,15,30,45)]

    This datagrid will always contain 7 rows (one row for each day).

    Does anyone know how I can create this datagrid? I am not pulling any data from the database. I need to hardcode everything.

    Thanks,
    Ninel

  2. #2
    Join Date
    Jan 2007
    Posts
    41
    Why do you need a datagrid? Wouldn't a table work fine? Unless you are using the sort function....bt without datasource, there is nothing to sort really.

  3. #3
    Join Date
    Oct 2004
    Location
    Bangalore, India
    Posts
    47

    Creating an unbound datagrid in asp.net

    Hi,




    The following sample code from the book:

    "Develop your own Web accounting application using ASP.net"


    ----


    1. Declarations

    Dim myConnection As SqlConnection = New SqlConnection(“Data Source=SYS1;Integrated Security=SSPI;Initial Catalog=FinAccounting”)

    Dim ddlDataSet As DataSet = New DataSet()
    Dim mycommand As New SqlCommand()

    Dim mytrans As SqlTransaction
    Dim objDT As System.Data.DataTable
    Dim objDR As System.Data.DataRow

    2. Building the DataTable Structure for the Datagrid

    We need to use a DataTable as the DataSource to program the datagrid. A DataTable is used, as it is not linked to any database. We know that DataTable provides a way to dynamically create a purely memory-resident representation of a database table. Typically, we will fill a DataTable from an existing database, but we can also create one programmatically, as we will be doing here. Recall we have alreay done an exercise in the second chapter, in which the DataGrid is directly linked to the table in the database.

    DataTable


    In a DataTable, columns are represented by the columns property, and rows are represented by the rows property. Thus, DataTables will be the perfect choice for the creation of Journal Voucher. We can build the columns just as we would within a database, using the columns property of the DataTable, and add rows to the DataTable with the Rows property. With the DataTable built, we can then bind the DataTable to a DataGrid to display the results in an intuitive manner. As DataTables contain rows and columns, we will be able to effectively mock the structure of a conventional database table. The rows will be added to the DataTable as the user adds accounts to the grid. We will add the following columns with corresponding data types:

    Name String
    Damt Integer
    Camt Integer

    For the present, we just want the structure of the DataTable built when the page loads for the first time. We don’t want to actually start to define rows until the user selects an account to add to the DataGrid. Looking at the code, we notice that the makeTable() function is called only when the page is loaded for the first time. This is the reason for the IsPostBack check.

    Next, we will add columns to the DataTable for Name, Damt, and Camt, assigning them the data types for string, integer and integer respectively. Finally, the DataTable is added into a Session, conveniently named “Accounts”, for storage. The next step involves adding Accounts to the Datagrid, which is no harder than defining new rows for the DataTable. Now that we know structure of the DataTable, we construct new rows and add them to the appropriate position within the DataTable.

    Function makeTable()
    objDT = New System.Data.DataTable(“Accounts”)
    objDT.Columns.Add(“Name”, GetType(String))
    objDT.Columns.Add(“damt”, GetType(Integer))
    objDT.Columns.Add(“camt”, GetType(Integer))
    Session(“Accounts”) = objDT
    MyGrid_ID.DataSource = objDT
    MyGrid_ID.DataBind()
    End Function

    Note: The DataGrid control maintains state between posts to the server and it only needs to be bound to a data source the first time the page is loaded or when the data source is updated.

    3. Programming the Page_Load event

    When the page loads for the first time, we call makeTable to create the structure of the DataTable.

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    makeTable()
    End If
    End Sub

Similar Threads

  1. Replies: 0
    Last Post: 09-02-2005, 07:28 PM
  2. Replies: 1
    Last Post: 03-11-2005, 10:14 AM
  3. Replies: 0
    Last Post: 07-31-2003, 12:18 PM
  4. Is there guidelines for proper use of DataGrid
    By Thomas Eyde in forum .NET
    Replies: 8
    Last Post: 08-01-2002, 06:43 PM
  5. Threading and the .NET Datagrid
    By McCleud in forum .NET
    Replies: 2
    Last Post: 08-01-2002, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center