Solved

Problems with Gridview

Posted on 2009-04-07
11
301 Views
Last Modified: 2013-11-26
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.

0
Comment
Question by:jackwebb22002
  • 6
  • 5
11 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Does the column replace the previous three or the next three? Would using a new variable for each column help?
0
 

Author Comment

by:jackwebb22002
Comment Utility
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
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Try these two lines

F.HeaderStyle.Width = "300px"
F.ItemStyle.Width = "300px"
0
 

Author Comment

by:jackwebb22002
Comment Utility
"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
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jackwebb22002
Comment Utility
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
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
0
 

Author Comment

by:jackwebb22002
Comment Utility
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
 

Author Comment

by:jackwebb22002
Comment Utility
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
 

Author Closing Comment

by:jackwebb22002
Comment Utility
Since you are the only one to try to help me, you get the points,  thanks.
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Thank you for awarding the points.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now