Dynamic GridView that can load Any Table

Last post 05-24-2007 4:53 PM by shepujw. 2 replies.

Sort Posts:

  • Dynamic GridView that can load Any Table

    05-24-2007, 12:03 PM
    • Loading...
    • shepujw
    • Joined on 05-21-2007, 1:48 PM
    • Posts 17

    How do I create a GridView, with sorting, paging, editing, deleting capability, that get's loaded by a tableadapter based on a querystring "table" variable?

    I have around 20 lookup tables that I want to put on an admin portion of a website for the admin to manage.  I don't want to create 20 different pages (1 for each table).  I want to create one page that has a list of the tables to be edited as links with a querystring table variable tied to them.  When the admin clicks on the link, a gridview is loaded with the passed table variable.

    I have created a DAL with DataTables and TableAdapters for all of the lookup tables. 

  • Re: Dynamic GridView that can load Any Table

    05-24-2007, 12:50 PM

    I am working on a similar problem. You will have no SQL datasource - instead you will be binding to a DataSet or Datatable. I dont think you need individual classes for each table - one generic DAL for tables will do. In my case I have decided to store metadata about my views. here is some sample code which "builds" a view using metadata. It works fine for display, but I am running into big problems with updates. Look at - http://forums.asp.net/t/1114528.aspx  Once you do not use an SQL datasource all goes to crap - at this point I cannot even retrieve the updated values for further processing! if, and when I solve some of my problems and this thread is still active I will post some results.

    Public Class ctrlGenerateView
    Private Sub MapDataView(ByVal grid As GridView, ByVal param As Long)
    Dim col As BoundField
    Dim com As DataControlField
    Dim meta As dbMetaData
    Dim metaView As New dbMetaGridView
    Dim tab As New DataTable
    meta = dbMetaData.getObject
    metaView = meta.GetGridViews(grid.ID.ToString)
    If Not metaView Is Nothing Then
    grid.AutoGenerateColumns = False
    grid.AutoGenerateEditButton = metaView.IsUpdateEnabled
    grid.AutoGenerateDeleteButton = metaView.IsDeleteEnabled
    Dim idx As Integer
    Dim column As New dbMetaGridColumns
    For idx = 0 To metaView.Columns.Count - 1
    col =
    New BoundField
    column = metaView.Columns(idx)
    col.HeaderText = column.HeaderText
    col.DataField = column.DataField
    col.Visible = column.IsVisible
    col.ReadOnly = column.IsReadOnly
    grid.Columns.Add(col)
    Next
    End If
    Dim dba As New dbDataAccess
    Dim SQL As String
    SQL = metaView.BaseSQL
    SQL = SQL.Replace(
    "@param", param.ToString)
    tab = dba.ReadQuery(SQL)
    grid.DataSource = tab
    grid.DataBind()
    End Sub
    End Class

     

  • Re: Dynamic GridView that can load Any Table

    05-24-2007, 4:53 PM
    • Loading...
    • shepujw
    • Joined on 05-21-2007, 1:48 PM
    • Posts 17

     I almost got it to work.  I'm having trouble defining a DataTable based on the passed strTableName variable.  I'd like to use the DataTable that I defined in the DLL.  I set the table name of the DataTable equal to the passed strTableName, but no columns are found when it tries to loop thru the columns.  I think I need to initialize or load the table first, but I don't know how.
     

    Partial Class managing_lookup
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Dim strTableName As String

            strTableName = Request.QueryString("table")

            LoadGrid(strTableName)

            If Not IsPostBack Then
                AddColumnsToGrid(strTableName)
            End If

           
        End Sub

        Protected Sub LoadGrid(ByVal strTableName As String)
            With Me.ObjectDataSource1
                .SelectMethod = "GetData"  'Need to change all table adapters to GetData
                .DeleteMethod = "Delete"
                .InsertMethod = "Insert"
                .UpdateMethod = "Update"
                .OldValuesParameterFormatString = "Original_{0}"
            End With
            'Set Type Name
            Me.ObjectDataSource1.TypeName = "dsPart_CONVTableAdapters." & strTableName & "TableAdapter"

            'Update Parameters
            SetUpdateParameters(strTableName)

            'Insert Parameters
            SetInsertParameters(strTableName)

            'Delete Parameters
            Me.ObjectDataSource1.DeleteParameters.Add("Original_{0}", TypeCode.Int32)

           

        End Sub

        Protected Sub SetUpdateParameters(ByVal strTableName As String)
            Dim strDataTableName As String
            strDataTableName = "dsPart_CONV." & strTableName & "DataTable"

            'Sets DataTable equal to passed strTableName
            Dim datTable As New DataTable

            datTable.TableName = strTableName

            For Each col As DataColumn In datTable.Columns
                Me.ObjectDataSource1.InsertParameters.Add(col.ColumnName, col.DataType.ToString) 'Do I Need Data Type
            Next

            Me.ObjectDataSource1.InsertParameters.Add("Original_{0}", TypeCode.Int32)

        End Sub

        Protected Sub SetInsertParameters(ByVal strTableName As String)
            'same as set update parameters find way to dynamically set datTable based on strTableName
            Dim strDataTableName As String
            strDataTableName = "dsPart_CONV." & strTableName & "DataTable"

            'Sets DataTable equal to passed strTableName
            Dim datTable As New DataTable
            datTable.TableName = strTableName

            For Each col As DataColumn In datTable.Columns
                Me.ObjectDataSource1.InsertParameters.Add(col.Caption, col.DataType.ToString)
            Next

        End Sub

        Protected Sub AddColumnsToGrid(ByVal strTableName As String)
            'same as set update parameters find way to dynamically set datTable based on strTableName
            Dim strDataTableName As String
            strDataTableName = "dsPart_CONV." & strTableName & "DataTable"

            'Sets DataTable equal to passed strTableName
            Dim datTable As New DataTable
            datTable.TableName = strTableName

            With GridView1
                .DataSourceID = "ObjectDataSource1"
                .AutoGenerateColumns = False
                .DataKeyNames = New String() {"ID"}
                .AllowPaging = True
                .AllowSorting = True
                .PageSize = 10
            End With

            Dim bf As New BoundField
            For Each col As DataColumn In datTable.Columns
                With bf
                    .HeaderText = col.ColumnName
                    .DataField = col.ColumnName
                    .SortExpression = col.ColumnName
                End With
                Me.GridView1.Columns.Add(bf)
            Next

           
            Dim cmdField As New CommandField

            With cmdField
                .ButtonType = ButtonType.Button
                .ShowCancelButton = True
                .ShowEditButton = True
                .ShowDeleteButton = True
            End With
        End Sub
    End Class
     

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter