Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problems with Gridview

Posted on 2009-04-07
11
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24095627
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
ID: 24098845
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
ID: 24099067
Try these two lines

F.HeaderStyle.Width = "300px"
F.ItemStyle.Width = "300px"
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

by:jackwebb22002
ID: 24099778
"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
ID: 24104871
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
 

Author Comment

by:jackwebb22002
ID: 24105650
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 2000 total points
ID: 24105898
0
 

Author Comment

by:jackwebb22002
ID: 24160954
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
ID: 24164958
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
ID: 31567535
Since you are the only one to try to help me, you get the points,  thanks.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24195638
Thank you for awarding the points.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

609 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