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.
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.
Creating an unbound datagrid in asp.net
The following sample code from the book:
"Develop your own Web accounting application using ASP.net"
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.
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:
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.
objDT = New System.Data.DataTable(“Accounts”)
Session(“Accounts”) = objDT
MyGrid_ID.DataSource = objDT
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
By naijacoder in forum ASP.NET
Last Post: 09-02-2005, 08:28 PM
Last Post: 03-11-2005, 11:14 AM
Last Post: 07-31-2003, 01:18 PM
By Thomas Eyde in forum .NET
Last Post: 08-01-2002, 07:43 PM
Last Post: 08-01-2002, 11:54 AM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center