Datagrid Datatable Insert rows and data

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to copy some data from one datagrid to another. The first
datagrid containing data is called DocList. The blank Datagrid that I am
trying to copy some data to is called DataGrid1.

Please tell me the simplist way to loop through an existing datagrid, access
it's columns and rows, then copy parts of that data to a blank Datagrid1.

Thank you.

What I've tried unsuccessfully:
....
Dim dt As DataTable
Dim dr As DataRow
DataGrid1.DataSource = dt

Dim DocListGridItem As DataGridItem
For Each DocListGridItem In DocList.Items
dr = dt.NewRow
dt.Rows.Add(dr)
dr.Item(0) = DocListGridItem.Cells(0).Text()
Next
....
 
Just create a second dataset 9(which I believe you already have) and then
call Merge on the dataset passing in the first as a parameter. But I would
ask, do you definitely need two different tables - if not, I'd be careful
their b/c of performance issues.
 
Rob,

Because of the fact that the datagrid does not contain any data, can you set
the datasources of both datagrids to the same datatable.

If it has to be datafrom the same table however filtered than you can do

datagrid1.datasource = dt.defaultview
dim dv2 as new dataview(dt)
dv.rowfilter = whatever
datagrid1.datasource = dv2

If you want to show different columnst, than you use datagridcolumnstyles.

I hope this helps,

Cor
 
What I am trying to do is iterate through the first datagrid, which contains
essentially one column like a CSV string. Break it appart, one by one,
massage the data, then insert it into the second initially blank datagrid in
multiple columns row for row.

Do you happen to have a good example of this?

Thank you,

Rob
 
Cor,

Thank you for the suggestion. Unfortunately I cannot convert to Visual
Studio 2005 and Ver 2.0 at this time. I am currently stuck in VS2003 ver
1.1, so I'm not aware how to make the Dataview help me at this time.

What I have tried unsuccessfully so far is with an error:

System.NullReferenceException: Object reference not set to an instance of an
object
Line 104: Dim aNewRow As DataRow =
DsMain.Tables("ToImport").NewRow

'DocList is a Datagrid containing one column of Data seperated by a period.

Dim DsMain As New DataSet
DsMain.Tables.Add("ToImport")
DsMain.Tables("ToImport").Columns.Add("Col1")
DsMain.Tables("ToImport").Columns.Add("Col2")
DsMain.Tables("ToImport").Columns.Add("Col3")
DsMain.Tables("ToImport").Columns.Add("Col4")
DsMain.Tables("ToImport").Columns.Add("Col5")
DsMain.Tables("ToImport").Columns.Add("Col6")
DsMain.Tables("ToImport").Columns.Add("Col7")
DsMain.Tables("ToImport").Columns.Add("Col8")
DsMain.Tables("ToImport").Columns.Add("Col9")
DsMain.Tables("ToImport").Columns.Add("Col10")
DsMain.Tables("ToImport").Columns.Add("Col11")
Dim dt As DataTable
Dim dr As DataRow
Dim txt As String
Dim token As String
Dim DocListGridItem As DataGridItem
Dim tempint As Integer
For Each DocListGridItem In DocList.Items
Dim aNewRow As DataRow = DsMain.Tables("ToImport").NewRow
tempint = 0
aNewRow.Item(tempint) =
GetToken(DocListGridItem.Cells(0).Text(), ".") & vbCrLf
Do
If tempint = 10 Then
tempint = 0
DsMain.Tables("ToImport").Rows.Add(aNewRow)
Else
tempint = tempint + 1
End If
token = GetToken("", ".")
If token = "" Then Exit Do
txt = txt & token & vbCrLf
' create new row in the Main DataSet
aNewRow.Item(tempint) = token
Loop
Next

' assigning the dataset object into the datagrid to accept the csv
data broken down into multiple columns
DataGrid1.DataSource = DsMain
DataGrid1.DataBind()
 
Rob,

The sample should work for a datagrid as well, you would only have to use
styles to get the second grid nice.

I tested it now and saw I had to set that extra loop to set the boolean to
false, what was not needed in that datagridview sample, however basicly it
stays the same, however, in my opinion works a little bit nicer with the
datagridview, because that is pushed down direct with that commit. But that
you cannot reach in anyway with a datagrid.

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Make the basic table
Dim dt As New DataTable
dt.Columns.Add("Name")
dt.LoadDataRow(New Object() {"Ken"}, True)
dt.LoadDataRow(New Object() {"Cor"}, True)
dt.LoadDataRow(New Object() {"Peter"}, True)
DataGrid1.DataSource = dt.DefaultView
'Adding the extra column
dt.Columns.Add("Check", GetType(System.Boolean))
For Each dr As DataRow In dt.Rows
dr("check") = False
Next
'Make the selection
Dim dv As New DataView(dt)
dv.RowFilter = "Check = true"
DataGrid2.DataSource = dv
End Sub

Private Sub DataGridView1_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint
'Push the changes down to the datasource
BindingContext(DirectCast(DataGrid1.DataSource,
DataView)).EndCurrentEdit()
End Sub
///

I hope this helps,

Cor
 
Back
Top