Confusion over erasing data records and sharing data

I have a datagrid that I am using LINQ to fill, then I add some custom columns and populate them programmatically - if users click on the column header to re-sort, all the data in the added columns disappears. I am obviously missing something very basic, but I don't seem to see the forest for the trees.

I also have a few more questions about how I am doing:

  • in my code I am referring to custom datagrid cells by name, but cells from LINQ I have to use the reference cell number (i.e.: (1,2) instead of (total, 2) (I understand the name is replaced with int) - can I do I name the columns? How about if the end user will re-order them?

This is one of the first times I've used a datagrid, so any pointers would be fine.

LINQ code to output data

  Dim query = From m In db.details _
               Where m.InboundDate >= CType(MonthCalendar1.SelectionStart, DateTime) _
               And m.InboundDate <= CType(MonthCalendar1.SelectionEnd, DateTime).AddHours(23).AddMinutes(59) _
               And m.ClientNo = 1 _
               Join md In db.Manifests On md.ManifestID Equals m.MainID _
               Select m.Zone, m.MainID, m.InboundDate, m.Zip, md.LadingPkgQty, m.Weight

      

to fill data and add columns

billingDatagrid.DataSource = query

billingDatagrid.Columns.Add("Package Rate", "Package Rate")
billingDatagrid.Columns.Add("LTL", "LTL Rate")
billingDatagrid.Columns.Add("Freight", "Freight")
billingDatagrid.Columns.Add("Fuel Surcharge", "Fuel Surcharge")
billingDatagrid.Columns.Add("Linehaul", "Linehaul")
billingDatagrid.Columns.Add("Billed Amount", "Billed")

      

Sample code how I access datagrid columns:

Select Case currentZone
    Case 1
        packageRate = Val(billingDatagrid(4, currentrow).Value) * zone1PkgRate
        billingDatagrid("Package Rate", currentrow).Value = packageRate

        If Val(billingDatagrid(5, currentrow).Value) > 500 Then
            LTLCharge = zone1_ltlBase + (Val(billingDatagrid(5, currentrow).Value) - 500) * zone1_ltlOver
        Else
            LTLCharge = zone1_ltlBase
        End If

        billingDatagrid("LTL", currentrow).Value = LTLCharge

      

At the end of all this, I will need to create a .csv file to export - obviously, it is important that the correst data is saved with every line!

Thanks in advance for your advice.

0


a source to share


2 answers


You can create a class that has all the columns you want in the grid, and in your linq query query expression, create a new instance of the class for each row.

Example:

Public Class GridData
    Public Zone as (type here)
    ...all your other columns from the query
    Public PackageRate as (type here)
    ...all your other columns you add programatically
End Class

      

Then in the request, you can do:

Select New GridData with {.Zone=m.Zone, .MainID=m.MainID, .InboundDate=m.InboundDate, .Zip=m.Zip, .LadingPkgQty=md.LadingPkgQty, .Weight=m.Weight, .PackageRate=Nothing, etc }

      



With this, you simply assign the query to the data source as you do and all columns should appear and stay in place. Hope it helps. I think this can solve both problems, since the column names must be set as well, but I have not tested this part.

Edit: You can assign the PackageRate value in a loop even before you assign it to the grid data source.

For each row in query
   'you can read and write any of the properties of the GridData Class as row.Client, etc here
Next

      

Then assign it to datagrid

0


a source


OK - this is the final answer - code work and data stays where it needs to -

Thanks for the help!

My class:

Public Class GridData

Private _clientno
Private _manifest
Private _packagerate

Public Property PackageRate() As Double
    Get
        Return _packagerate
    End Get
    Set(ByVal value As Double)
        _packagerate = value
    End Set
End Property
Public Property manifest() As String
    Get
        Return _manifest
    End Get
    Set(ByVal value As String)
        _manifest = value
    End Set
End Property
Public Property client() As Int16
    Get
        Return _clientno
    End Get
    Set(ByVal value As Int16)
        _clientno = value
    End Set
End Property

      

End class

Linq:



     Dim query = From m In db.details _
            Where m.InboundDate >= CType(MonthCalendar1.SelectionStart, DateTime) _
            And m.InboundDate <= CType(MonthCalendar1.SelectionEnd, DateTime).AddHours(23).AddMinutes(59) _
            And m.ClientNo = 1 _
              Join md In db.Manifests On md.ManifestID Equals m.MainID _
            Select New GridData With {.PackageRate = Nothing, .manifest = m.MainID, .client = m.ClientNo}

   'No No -- billingDatagrid.Columns.Add("PackageRate", "PackageRate")

      

and some code to fill the packer with the value from the customer number:

   billingDatagrid.DataSource = query
    Dim currentrow As Int16 = 0
    For Each r In billingDatagrid.Rows
        billingDatagrid("PackageRate", currentrow).Value = billingDatagrid("client", currentrow).Value
        currentrow += 1
    Next

      

The above code now works ...

Thanks.

0


a source







All Articles