Welcome Guest Search | Active Topics | Sign In | Register

Dynamically creating editable columns in a grid Options
trevora
Posted: Thursday, July 23, 2009 5:48:10 AM
Rank: Newbie
Groups: Member

Joined: 7/23/2009
Posts: 4
Hi,

I need to create a grid dynamically, as the data coming through each time will consist of a varying number of columns.

By using the code below i've managed to get started.

Code: Visual Basic.NET
Me.gridForecast.Columns.Clear()
Me.gridForecast.FullRowMode = False

Dim column1 = New EO.Web.TextBoxColumn
column1.HeaderText = "Sku ID"
column1.DataField = "sku_ID"
Me.gridForecast.Columns.Add(column1)


However, although i have set FullRowMode to false and am using a TextBoxColumn type, the cell is not editable at runtime. I can make it editable by removing the line where the DataField is assigned, but that doesn't help me.

Please could someone let me know if they have any ideas how to get this to work?

Thanks in advance
eo_support
Posted: Thursday, July 23, 2009 7:14:14 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Your code looks fine to us. Is it possible for you to produce a test page for us to reproduce the problem? You can use the following steps to remove the dependence to your data source:

1. Restricts the data source to one row. For example, changing your SELECT statement to "SELECT TOP 1 ....". That way the Grid will only contain one row. If the problem still occurs, then continue to step 2;

2. Create a class and defines properties on that class. For example, if the data row for step 1 has an sku_ID value of "123", you can create a class Test, and define a public property "sku_ID" on it. Return "123" directly from that property;

3. Populate your Grid from your data class. It will be something like this:

Code: Visual Basic.NET
Me.gridForecast.DataSource = new Object(){New YourDataClass}
Me.gridForecast.DataBind();


Once you isolate the problem please post the result page. We will try to run it here. As soon as we can see the problem, it should be easy for us to tell you what's wrong.

Thanks!
trevora
Posted: Thursday, July 23, 2009 10:23:13 AM
Rank: Newbie
Groups: Member

Joined: 7/23/2009
Posts: 4
Hmm, as you mentioned that my code looked fine, i thought i would give it one last go before i started to look at your troubleshooting options and its now working without me having changed anything!!

I've spent the entire morning trying to get this working...maybe i should have posted on the forum earlier!!

Thanks for your help anyway - i'll get back to you if i come across any more problems.
eo_support
Posted: Thursday, July 23, 2009 10:26:46 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Not a problem. Let us know if you run into any other problems.
trevora
Posted: Tuesday, July 28, 2009 4:42:26 AM
Rank: Newbie
Groups: Member

Joined: 7/23/2009
Posts: 4
Hi - I've got another problem now.

I've created a sub-procedure that runs for each month that creates 4 columns (id, Volume, Value and Margin) for each month (as per the code in my original post). There can be varying numbers of months to display depending on the users selection so therefore the grid can have varying numbers of columns This all works fine and i end up with a grid with the correct headings etc.

However, when I then try to databind the data via a SQLDataReader to the grid i get an error saying "Data field 'vol_118' does not exist". vol_118 should be the datafield of the first column. I've checked and confirmed that the column is being created with this code, so i'm a bit stuck as to how to proceed.

In the code below the user clicks btnGet.

Code: Visual Basic.NET
Protected Sub btnGet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGet.Click

        BuildGrid()
        BuildGridData()

    End Sub

    Protected Sub BuildGrid()

        Dim VBUtil As New VBCommonCode

        ' Existing record
        Try
            Dim sConn As String = System.Configuration.ConfigurationManager.AppSettings("SWASSQLConnectionString")
            Dim connection As SqlConnection = New SqlConnection(sConn)
            Dim Command As SqlCommand = New SqlCommand()

            Command.Connection = connection
            Command.CommandText = "sp_cprs_forecast_periodcolumns"
            Command.CommandType = CommandType.StoredProcedure
            Command.Parameters.AddWithValue("@FromPeriod_ID", VBUtil.NullInt(Me.drpPeriodFrom.SelectedValue, 0))
            Command.Parameters.AddWithValue("@ToPeriod_ID", VBUtil.NullInt(Me.drpPeriodTo.SelectedValue, 0))

            Dim aAdapter As SqlDataAdapter = New SqlDataAdapter(Command)
            Dim dDataSet As New DataSet
            Dim rDataRow As DataRow

            ' Get column data
            aAdapter.Fill(dDataSet, "periodcolumns")

            ' Create sku_id column
            Me.gridForecast.Columns.Clear()
            Me.gridForecast.FullRowMode = False

            ' Create sku_id column
            Dim column1 = New EO.Web.StaticColumn
            column1.HeaderText = "Sku ID"
            column1.DataField = "sku_ID"
            Me.gridForecast.Columns.Add(column1)

            ' Create sku_id column
            Dim column2 = New EO.Web.StaticColumn
            column2.HeaderText = "Sku Name"
            column2.DataField = "sku_Name"
            Me.gridForecast.Columns.Add(column2)

            ' Create sku_id column
            Dim column3 = New EO.Web.StaticColumn
            column3.HeaderText = "Sku Description"
            column3.DataField = "sku_Description"
            Me.gridForecast.Columns.Add(column3)

            ' Create sku_id column
            Dim column4 = New EO.Web.CheckBoxColumn
            column4.HeaderText = "Sku Status"
            column4.DataField = "sku_Status"
            Me.gridForecast.Columns.Add(column4)

            ' Create sku_id column
            For Each rDataRow In dDataSet.Tables("periodcolumns").Rows
                BuildGridColumn(rDataRow("YearPeriod"), CStr(rDataRow("period_id")))
            Next


            ' ERROR TRAP
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw
        Finally
            ' Close code here
        End Try


    End Sub


    Protected Sub BuildGridColumn(ByVal yp As String, ByVal p As String)

        ' ID Column
        Dim column1 = New EO.Web.StaticColumn
        column1.HeaderText = "id_" + p
        column1.Name = "id_" + p
        column1.DataField = "id_" + p
        column1.Visible = False
        Me.gridForecast.Columns.Add(column1)

        ' Volumn Column
        Dim column2 = New EO.Web.TextBoxColumn
        column2.HeaderText = yp + " Vol"
        column2.Name = "vol_" + p
        column2.DataField = "vol_" + p
        Me.gridForecast.Columns.Add(column2)

        ' Margin Column
        Dim column3 = New EO.Web.TextBoxColumn
        column3.HeaderText = yp + " Mar"
        column3.Name = "mar_" + p
        column3.DataField = "mar_" + p
        Me.gridForecast.Columns.Add(column3)

        ' Value Column
        Dim column4 = New EO.Web.TextBoxColumn
        column4.HeaderText = yp + " Val"
        column4.Name = "val_" + p
        column4.DataField = "val_" + p
        Me.gridForecast.Columns.Add(column4)

    End Sub


    Protected Sub BuildGridData()

        Dim VBUtil As New VBCommonCode
        Try
            Dim sConn As String = System.Configuration.ConfigurationManager.AppSettings("SWASSQLConnectionString")
            Dim iRetValue As Integer
            Dim conGQDS As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(sConn)
            Dim cmdCommand As New System.Data.SqlClient.SqlCommand("sp_cprs_forecast_read", conGQDS)
            Dim rReader As System.Data.SqlClient.SqlDataReader

            conGQDS.Open()

            With cmdCommand

                .CommandType = Data.CommandType.StoredProcedure
                .Parameters.Add("@ret", Data.SqlDbType.Int)
                .Parameters(0).Direction = Data.ParameterDirection.ReturnValue

                ' INPUT PARAMETERS
                .Parameters.AddWithValue("@Customer_ID", VBUtil.NullInt(Me.drpCustomer.SelectedValue, 0)) '
                .Parameters.AddWithValue("@FromPeriod_ID", VBUtil.NullInt(Me.drpPeriodFrom.SelectedValue, 0)) '
                .Parameters.AddWithValue("@ToPeriod_ID", VBUtil.NullInt(Me.drpPeriodTo.SelectedValue, 0)) '
                .Parameters.AddWithValue("@FilterHeader", VBUtil.NullInt(Me.rdoGroupFilter.SelectedValue, 0)) '
                .Parameters.AddWithValue("@FilterDetail", VBUtil.NullInt(Me.drpGroupSelect.SelectedValue, 0)) '
                .Parameters.AddWithValue("@Retired", Me.chkRetired.Checked.ToString) '

                ' CALL STORED PROCEDURE
                rReader = .ExecuteReader()

                ' GET EXECUTION SUCCESS FLAG
                iRetValue = CType(.Parameters("@ret").Value, Integer)

            End With

            'PROCESS EXECUTION SUSSESS RESULTS
            Select Case iRetValue

                Case 0 ' Success
                    While rReader.Read

                        Me.gridForecast.DataSource = rReader
                        Me.gridForecast.DataBind()

                    End While
                Case Else
                    Throw New Exception("sp_cprs_forecast_read returned an unexpected value of " + iRetValue.ToString())
            End Select

            ' ERROR TRAP
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw
        Finally
            ' Close code here

        End Try

    End Sub


Thanks in advance,
eo_support
Posted: Tuesday, July 28, 2009 10:28:26 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

There is no reason for the Grid to complain if the data field exists. You can verify that by checking the field right before you data bind the Grid:

Code: Visual Basic.NET
rReader.Read()
Dim value as Object = rReader.Items("vol_118")


This should verify whether you have the correct field.

Thanks!
trevora
Posted: Wednesday, July 29, 2009 5:07:16 AM
Rank: Newbie
Groups: Member

Joined: 7/23/2009
Posts: 4
Fixed now thanks!

I confirmed via your code that the data field was different to what i was expecting, looked back and found out the column headings coming through from the stored procedure had been altered without me knowing.

Thanks for your help,
Trevor
eo_support
Posted: Wednesday, July 29, 2009 8:19:06 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Great. Thank you very much for the update!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.