Results 1 to 8 of 8

Thread: DataGridView loses data I added to an added column!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    DataGridView loses data I added to an added column!

    Am I being dumb? I read a XML file into a DataGridView...

    Code:
    Dim oDSet = New System.Data.DataSet
    oDSet.ReadXml(strFilePath)
    DataGridView1.DataSource = oDSet.Tables(0)
    The XML file contents show in the grid OK.
    I iterate through the rows, and if there's bad data in the row I set the colour to red, and add a value to what would end up as a hidden new column, with the intention of sorting later to show all the error rows together.

    Code:
            DataGridView1.Columns.Add("test1", "Errors")
            For Each drow As System.Windows.Forms.DataGridViewRow In DataGridView1.Rows
                If drow.Cells("Whatever").Value.ToString = "Wrong" Then
                    DataGridView1.Rows(drow.Index).DefaultCellStyle.BackColor = Color.Red
                    DataGridView1.Rows(drow.Index).Cells("test1").Value = "ERR"
                End If
            Next
    It sort of works, although I've found that I can't sort easily on a non-data-bound new column.

    My question is, though, if I click on a column heading to sort, all my red changes - and new values in the new test1/"Errors" column - all vanish!
    Why is that, and how do I actually achieve this?
    Thanks

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: DataGridView loses data I added to an added column!

    If you sort, you refresh the binding, so anything which hasn't been added to the underlying datatable will, well, disappear!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Re: DataGridView loses data I added to an added column!

    Thanks - so I have to manually copy the XML dataset to the DataGridView, so it's unbound - set up the columns by hand, etc - and all will be well....?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: DataGridView loses data I added to an added column!

    Well, you could do that but I would add columns to the datatable and enter the 'hidden' values there so that the binding is always valid.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Re: DataGridView loses data I added to an added column!

    Aha, sounds like a plan
    Thanks!

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: DataGridView loses data I added to an added column!

    I'd be inclined to leave that column in there full time, even in the XML. It seems like it would be useful. Then, right after you set the datasource for the DGV, hide the column that you want hidden.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Re: DataGridView loses data I added to an added column!

    Thanks. Unfortunately the XML isn't mine, it's an export from some other system - hence I'll need to check carefully each row contains valid values before doing anything with it - and alert the user if anything is dodgy.
    I suppose that the sorting (clicking a col hdr) will scrap my efforts to turn rows red, and I'll need to intercept some kind of "after sorting" event and re-do the red stuff?

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Apr 2011
    Posts
    75

    Re: DataGridView loses data I added to an added column!

    Got it working, thanks.

    Example: Add a column to a dataset read from XML, and check all rows for a bad field and set the new "Errors" field :
    Code:
            oDSet = New System.Data.DataSet
            oDSet.ReadXml(strFilePath)
            oDSet.Tables(0).Columns.Add("Errors")
            '  Show the col headings in the VS output pane
            'For Each col As System.Data.DataColumn In oDSet.Tables(0).Columns
            '    Console.WriteLine(col.ColumnName.ToString)
            'Next
    
            For Each drow As System.Data.DataRow In oDSet.Tables(0).Rows
                If drow.Item("SomeColumn").ToString = "Bad data" Then
                    drow.Item("Errors") = "!" ' more user friendly instead : add description of problem :)
                End If
            Next
    Then it's a case of managing the DGV to set error rows red - this after-sort works for me
    Code:
        Private Sub DataGridView1_Sorted(sender As Object, e As System.EventArgs) Handles DataGridView1.Sorted
            For Each drow As System.Windows.Forms.DataGridViewRow In DataGridView1.Rows
                If drow.Cells("Errors").Value.ToString <> "" Then
                    DataGridView1.Rows(drow.Index).DefaultCellStyle.BackColor = Color.Red
                End If
            Next
        End Sub
    - but the guts of that needs to be ripped out into a Sub that can be called when the grid is first shown, as well as when sorted and refreshed.

    Is there really no more 'sticky' way to make row colours depend on row conditions?
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width