Solved

Problems with Gridview

Posted on 2009-04-07
11
311 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

815 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

10 Experts available now in Live!

Get 1:1 Help Now