Solved

Problems with Gridview

Posted on 2009-04-07
11
322 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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