Problems with Gridview

I have a two pronged problem dealing with porting an app from Windows to the Web.

First, given a DataGridView of dgvGasolinePurchased, are there counterpart properties in the GridView?  Presently my DataGridView is built with:

        Me.dgvFuelPurchased.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
        Me.dgvFuelPurchased.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        Me.dgvFuelPurchased.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
        Me.dgvFuelPurchased.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        Me.dgvFuelPurchased.Dock = DockStyle.Fill
and I'd love to have these columns appear as closely to this as possible.

And Secondly, I'm constructing the DataBound Fields for the GridView as such:
        With Me.gvCustomers
            .Columns.Clear()
            .AutoGenerateColumns = False

            ' create the Id Column
            Dim F As New BoundField
            F.DataField = "PersonId"
            F.HeaderText = "Id"
            F.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Left
            .Columns.Add(F)

            ' create the Last Name
            F = New BoundField
            F.DataField = "LastName"
            F.HeaderText = "Last_Name"
            F.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Left
            .Columns.Add(F)

      .
      .
      .
      .
      .

            ' create the NumberTripsTaken
            F = New BoundField
            F.DataField = "NumberTripsTaken"
            F.HeaderText = "#_Trips_Taken"
            F.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Left
            .Columns.Add(F)
        End With

My problem here is that of the 18 columns, some columns repeat themselves, replacing intended columns.  Specifically, 1 column repeats itself 4 times, errantly replacing three intended colums.

Has anyone seen this type of behavior?


Any help on these two areas of concern would be most appreciated.

jackwebb22002Asked:
Who is Participating?
 
CodeCruiserCommented:
Does the column replace the previous three or the next three? Would using a new variable for each column help?
0
 
jackwebb22002Author Commented:
Previous three.  But it may be moot, as I've converted to a generic subroutine for building Boundfields, and the problem went away.  (routine attached)

But the first problem still plagues me, in that I need to be able to set column widths of the GridView, as I was able to with the DataGridView.  I've got fields like an address wrapping into multiple lines base on the space character.

    ''' <summary>
    ''' initializes boundfield
    ''' </summary>
    ''' <param name="gvColumnCollection">GridView Column Collection</param>
    ''' <param name="dtaField">DataField</param>
    ''' <param name="hdrText">HeaderText</param>
    ''' <param name="hdrHorizontalAlign">HeaderStyle.HorizontalAlign</param>
    ''' <param name="itemHorizontalAlign">ItemStyleAlign.HorizontalAlign</param>
    ''' <param name="htmlEncode">HtmlEncode</param>
    ''' <param name="dataFmtString">DataFormatString</param>
    ''' <remarks></remarks>
    Private Sub BldBoundField(ByRef gvColumnCollection As DataControlFieldCollection, _
                                           ByVal dtaField As String, ByVal hdrText As String, _
                                           ByVal hdrHorizontalAlign As HorizontalAlign, _
                                           ByVal itemHorizontalAlign As HorizontalAlign, _
                                           ByVal htmlEncode As Boolean, ByVal dataFmtString As String)
        F = New BoundField
        F.DataField = dtaField
        F.HeaderText = hdrText
        F.HeaderStyle.HorizontalAlign = hdrHorizontalAlign
        F.ItemStyle.HorizontalAlign = itemHorizontalAlign
        F.HtmlEncode = htmlEncode
        F.DataFormatString = dataFmtString
        gvColumnCollection.Add(F)
    End Sub

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
CodeCruiserCommented:
Try these two lines

F.HeaderStyle.Width = "300px"
F.ItemStyle.Width = "300px"
0
 
jackwebb22002Author Commented:
"300px" cause a compilation error, (OPTION STRICT ON)
so i changed it to:
        F.HeaderStyle.Width = Unit.Pixel(300)
        F.ItemStyle.Width = Unit.Pixel(300)
which had zero effect, so i changed that to:
        F.HeaderStyle.Width = Unit.Pixel(5000)
        F.ItemStyle.Width = Unit.Pixel(5000)
again, to no avail, and in fact no difference in output.

I'm guessing that there is another overriding property at work here, thus making anything I do irrelevant.



0
 
CodeCruiserCommented:
The problem is this line in your code

Me.dgvFuelPurchased.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

When you set it to auto size, it would obviously ignore any manual settings.
0
 
jackwebb22002Author Commented:
Sorry, you were looking at the code for the DataGridView, which is what I'm trying to convert to a Gridview in a Web Form. Hence I was looking for a counterpart in the GridView that accomplishes what the DataGridView does.

The dgv prefix refers to the DataGridView, and gv to the GridView.
The GridView does not have an AutoSizeColumnsMode property.
0
 
jackwebb22002Author Commented:
Sorry, that didn't help.  

The base problem still remains.  How can I control the widths of columns in a dynamically built Gridview that uses DataBound fields that I build?
0
 
jackwebb22002Author Commented:
I found the answer to the GridView column width problem.

Problem:
1.) The GridView is dynamically built,
2.) When column data is wider than the column's Header Text, the data is "WRAPPED" into multiple lines.

The solution lies in clearing out any properties in the header and item elements of the BoundField as in the two Reset methods employed immediately after instantiating a new Bound Field.


    ''' <summary>
    ''' initializes boundfield
    ''' </summary>
    ''' <param name="gvColumnCollection">GridView Column Collection</param>
    ''' <param name="dataField">DataField</param>
    ''' <param name="hdrText">HeaderText</param>
    ''' <param name="hdrHorizontalAlign">HeaderStyle.HorizontalAlign</param>
    ''' <param name="itemHorizontalAlign">ItemStyleAlign.HorizontalAlign</param>
    ''' <param name="htmlEncode">HtmlEncode</param>
    ''' <param name="dataFmtString">DataFormatString</param>
    ''' <param name="pDataType">Data Type of column</param>
    ''' <remarks></remarks>
    Private Sub BldBoundField(ByRef gvColumnCollection As DataControlFieldCollection, _
                                           ByVal dataField As String, _
                                           ByVal hdrText As String, _
                                           ByVal hdrHorizontalAlign As HorizontalAlign, _
                                           ByVal itemHorizontalAlign As HorizontalAlign, _
                                           ByVal htmlEncode As Boolean, ByVal dataFmtString As String, _
                                           ByVal pDataType As Type)
        F = New BoundField
        F.HeaderStyle.Reset()
        F.ItemStyle.Reset()
        F.DataField = dataField
        F.HeaderText = hdrText
        F.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
        F.HeaderStyle.Wrap = False
        F.ItemStyle.Wrap = False
        F.DataFormatString = dataFmtString
        If pDataType Is GetType(Integer) Then
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Right
            F.DataFormatString = dataFmtString
        ElseIf pDataType Is GetType(String) Then
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Left
        ElseIf pDataType Is GetType(DateTime) Then
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Right
            F.DataFormatString = "{0:d}"
        ElseIf pDataType Is GetType(Decimal) Then
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Right
            F.DataFormatString = "{0:c}"
        ElseIf pDataType Is GetType(Boolean) Then
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Center
        Else
            F.ItemStyle.HorizontalAlign = HorizontalAlign.Center
        End If
        F.HtmlEncode = htmlEncode
        gvColumnCollection.Add(F)
    End Sub


Then, to use this construct, call the BldBoundField method for each column of the GridView, as in:

    Protected Sub lbtnGetTrips_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnGetTrips.Click
        lblMessages.Text = String.Empty
        If m_selectedState Is Nothing Then
            lblMessages.Text = "Please Select a State"
        Else
            Try
                Dim service As New localhost.TravelService
                Dim ds As DataSet = service.GetAllTripsOfState(m_selectedState)
                Me.gvTrips.Columns.Clear()
                Me.gvTrips.DataSource = ds.Tables(0)
                Me.gvTrips.AutoGenerateColumns = False
                With ds.Tables(0)
                    Dim columnNumber As Integer
                    Dim numberColumns As Integer
                    Dim strData As String
                    Dim strHeader As String
                    numberColumns = .Columns.Count
                    For columnNumber = 0 To numberColumns - 1
                        strData = .Columns(columnNumber).ColumnName
                        strHeader = .Columns(columnNumber).ColumnName
                        BldBoundField(Me.gvTrips.Columns, strData, strHeader, _
                                              HorizontalAlign.Center, HorizontalAlign.Left, _
                                              True, "", .Columns(columnNumber).DataType)
                    Next
                End With
                Me.gvTrips.DataBind()
            Catch ex As Exception
                lblMessages.Text = "Error: " & ex.Message
            End Try
        End If
    End Sub
0
 
jackwebb22002Author Commented:
Since you are the only one to try to help me, you get the points,  thanks.
0
 
CodeCruiserCommented:
Thank you for awarding the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.