-
Jan 2nd, 2013, 11:00 AM
#1
Thread Starter
Lively Member
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
-
Jan 2nd, 2013, 11:20 AM
#2
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!
-
Jan 2nd, 2013, 02:18 PM
#3
Thread Starter
Lively Member
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....?
-
Jan 2nd, 2013, 03:08 PM
#4
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!
-
Jan 2nd, 2013, 03:16 PM
#5
Thread Starter
Lively Member
Re: DataGridView loses data I added to an added column!
Aha, sounds like a plan
Thanks!
-
Jan 2nd, 2013, 04:45 PM
#6
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
-
Jan 2nd, 2013, 08:04 PM
#7
Thread Starter
Lively Member
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?
-
Jan 3rd, 2013, 06:04 AM
#8
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|