Saturday, March 24, 2012

Loop through DataSet and Load DataGrid

Hi,

How to loop through a dataset and manually populate a datagrid.

At the moment I'm using DataSource and DataBind(), but there are instances where I don't want to bind all the columns

I.E. I want to save a new version of an article, I want to load the original authors into a datagrid without the original ID's then save them again as new records, with new id's

If there is a better way of handling this I'd love to hear about it

Cheers AlYou mean like adding DataRows to a datatable?
Here is a non working illustration of what I'm trying to do.

I'm creating an editable grid which users can add records to, when I process the form if the id field is blank then I know it's a new record and so I must call Insert and not Update.

I think I've got the idea of looping through the ds just need to assign the values to the datagrid

Dim iRows As Integer = dstCopy.Tables(0).Rows.Count
For i As Integer = 0 To iRows ' Not sure might need a -1 here
dgEditGrid.row.add()
dgEditGrid.Columns.Item(0) = dstCopy.Tables(0).Rows(i)("First_Name")
dgEditGrid.Columns.Item(1) = dstCopy.Tables(0).Rows(i)("Middle_Name")
dgEditGrid.Columns.Item(2) = dstCopy.Tables(0).Rows(i)("Last_Name")
dgEditGrid.Columns.Item(3) = dstCopy.Tables(0).Rows(i)("Affiliation")
' In some instances I want to set this column to null, i.e where a new version is being saved
' I want to load the old values without their keys, the user can then edit them as they wish
' and save, resulting in a new record and preserving the history
If Revision Then
dgEditGrid.Columns.Item(4) = ""
Else
dgEditGrid.Columns.Item(4) = dstCopy.Tables(0).Rows(i)("id")
End If
Next i
You can .Delete() the row you don't want and perform a Rows.Add() for the new row you want added to the table.
Mendhak,

I'm not sure I understand what you're saying.

I don't want to delete any rows I just don't want to populate one of the datagrid columns in some circumstances.
Thought that If I could manually update the dataset then I wouldn't need to do the above

If revision Then
Dim iRows As Integer = dstCopy.Tables(0).Rows.Count
For i As Integer = 0 To iRows -1
dstCopy.Tables(0).Rows(i)("id") = dbnull.value
Next i
End If

except it doesn't seem to be working
Going by your code you might as well just bind the datatable, and inserts you'll just add a row to the datatable...
Thought that If I could manually update the dataset this I wouldn't need to do the above

If revision Then
Dim iRows As Integer = dstCopy.Tables(0).Rows.Count
For i As Integer = 0 To iRows ' Not sure might need a -1 here
dstCopy.Tables(0).Rows(i)("id") = ""
Next i
End If

Getting an error which I think is because I'm passing in "" when it's expecting an Int32, is it possible to set this to empty? -
System.FormatException: Input string was not in a correct format. at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.String.System.IConvertible.ToInt32(IFormatProvider provider) at System.Convert.ToInt32(Object value) at System.Data.Common.Int32Storage.Set(Int32 record, Object value) at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <> in id Column. Expected type is Int32.

DBNull should work
tpm,

thanks was just editing my last post with just that :thumb:

0 comments:

Post a Comment