Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET

Editable DataGrid Binding - VS 2003 RAD approach (ASP.NET 1.1) - EditGrid

Rate me:
Please Sign up or sign in to vote.
4.07/5 (15 votes)
2 Mar 2006CPOL3 min read 93.4K   248   29  
Editable DataGrid binding - VS 2003 RAD approach (ASP.NET 1.1).
Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DatasetEditGrid1 = New EditGrid.DatasetEditGrid
        CType(Me.DatasetEditGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        '
        'DatasetEditGrid1
        '
        Me.DatasetEditGrid1.DataSetName = "DatasetEditGrid"
        Me.DatasetEditGrid1.Locale = New System.Globalization.CultureInfo("en-US")
        CType(Me.DatasetEditGrid1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub
    Protected WithEvents DatasetEditGrid1 As EditGrid.DatasetEditGrid
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

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

            'Fill values into your dataset.
            'If using a database data source, replace this with your adapter.fill
            With Me.DatasetEditGrid1.EditGrid
                .AddEditGridRow("Robert", "Site 1", True)
                .AddEditGridRow("Bill", "Site 2", False)
                .AddEditGridRow("Joe", "Site1", True)
            End With

            'Cache dataset. (You can also use session to cache)
            Me.ViewState("DatasetEditGrid1") = Me.DatasetEditGrid1
        Else
            'Point to the cached dataset.
            Me.DatasetEditGrid1 = CType(Me.ViewState("DatasetEditGrid1"), DatasetEditGrid)
        End If
    End Sub

    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
        Me.DataGrid1.EditItemIndex = e.Item.ItemIndex
    End Sub

    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand
        UpdateDatasetFromGrid()
        Me.DataGrid1.EditItemIndex = -1
    End Sub

    'Relate the Dataset's Column Names to the Column Index in the Datagrid
    Private Enum DatasetColumnNameToGridColumnIndex
        Name = 1
        Location = 2
        ClockedIn = 3
    End Enum

    'Reverse bind from the datagrid to the dataset.
    Private Sub UpdateDatasetFromGrid()
        For Each ColName As DatasetColumnNameToGridColumnIndex In System.Enum.GetValues(GetType(DatasetColumnNameToGridColumnIndex))
            SetDatasetValuesFromGrid(Me.DatasetEditGrid1.EditGrid, ColName.ToString, Me.DataGrid1.Items(Me.DataGrid1.EditItemIndex), ColName)
        Next

        'Normally you would save (adapter.update) to the database here.
    End Sub

    'Generic Code to do Reverse binding from a DataGridItem cell to its corresponding DataTable item.
    Private Sub SetDatasetValuesFromGrid(ByVal DataTable As DataTable, ByVal DataTableColumnName As String, ByVal DataGridItem As DataGridItem, ByVal DataGridCellIndex As Integer)
        For Each control As Control In DataGridItem.Cells(DataGridCellIndex).Controls
            If TypeOf control Is CheckBox Then
                Me.DatasetEditGrid1.EditGrid(DataGridItem.DataSetIndex)(DataTableColumnName) = CType(control, CheckBox).Checked
                Exit For
            ElseIf TypeOf control Is TextBox Then
                Me.DatasetEditGrid1.EditGrid(DataGridItem.DataSetIndex)(DataTableColumnName) = CType(control, TextBox).Text
                Exit For
            End If
        Next
    End Sub

    Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
        Me.DataGrid1.EditItemIndex = -1
    End Sub

    'Always bind the grid to the dataset just before rendering.  This will show the most recent state.
    Private Sub DataGrid1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.PreRender
        'Bind the Grid.
        Me.DataGrid1.DataBind()
    End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Kiefer Consulting
United States United States
Sacramento, CA based Senior .Net and SharePoint Solution Architect for Kiefer Consulting
(1-800-794-1928)
B.S. in Mathematics from UCDavis
.NET Wizard - Experts-Exchange
MCSD, MCTS: MOSS 2007 Config

Some of the bigger questions:
1. What is the meaning of my life?
To satisfy the purpose of your creator(s). (Meaning must derive from purpose. Those who create you, give you meaning.)

2. Who is my creator?
Ultimately, God is your creator. God designed and created the universe and everything in it. You and others in your life can also be a part of your creation, overriding or furthering God's purpose.

3. What is God's purpose for me?
To love and be loved by your creator and others and to enjoy the life you've been given.
This can be distinguished two ways.
a. Use your built in common sense (morality/feelings)
b. Use the creator's handbook. Fortunately our creator did not abandon us. He is with us now and even lived and died as one of us. Check out his biography in "The Bible"

Note on free-will vs. predetermination:
God exists outside the constraints of time. He exists at every point in time simultaneously and knew of your birth and every decision you will/have made. But this does not mean God predetermined any of it. Pre and post are time related concepts that do not apply to God. God always has been and always will be. He determines our universe to exist, gives it the parameters of natural law, and allows us to make our own way through it (free-will). Note that these are all present tense, it would be more appropriate to use past, present, and future tense combined. God's purpose is for us to love him and one another. But a prerequisite of love is the free-will to love. So even though God wants us to love him/others, he can not ensure it. He can, however, help us if we allow him to. A miracle is God modifying natural parameters in response to human will.

Comments and Discussions