Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 196769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:47:44+00:00 2026-05-11T16:47:44+00:00

I have a datagrid which I am using LINQ to fill, I then add

  • 0

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

I also have a couple of other questions about how I am doing things:

  1. in my code I am accessing the datagrid custom cells by name, but the cells from the LINQ I have to use a cell reference number (i.e.: (1,2) instead of (total,2) (I realize that the name is replaced by a int) – can I name the columns? How about if the end user re-orders them?

This is one of the first times I have used a datagrid like this so any pointers would be nice.

LINQ code to pull 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

code to fill with 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")

Code example of how I am accessing the 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 am going to have to create a .csv file for export – it is obviously important that the correst data stay with each row!

Thanks in advance for advice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:47:45+00:00Added an answer on May 11, 2026 at 4:47 pm

    You could create a class that has all the columns that you want in the grid and in the linq query select statement 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 query 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 that you just assign the query to the datasource like you are doing and all columns should show up and still be there upon resort. Hope it helps. I think that could solve both your problems since the column names should also be all set, but i didn’t test that part.

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

    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 the datagrid

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a Flex DataGrid with 3 columns of which one of them has
I have a datagrid in which i manually specify the columns (AutoGenerateColumns=false). I'm using
I have a DataGrid which i am using on the page alogn with 4
I have a simple datagrid to which I am binding a collection using ItemsSource.
I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at
I have a datagrid which populated by some data from sqlserver.I have datetime filed
I am using asp.net 2.0 and c#. I have a datagrid in which I
I have a DataGrid bound to a DataView which, among other columns has, an
Currently I have a DataGridView in my app which I fill from my Linq-to-SQL
I have a DataGrid, which is showing some stuff. Each of the cells has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.