Batch update to table from datagrid

B

Bill nguyen

I use DatagridTableStyle to display data from table
WF_Carrier_FreightChart. USers than can add/delete/edit rows in the
Datagrid.
Since the Datagrid's datasource "dCarrierFreight" is just a copy/view of the
table (dataset dFreight.tables(0) ), I need to update the original table to
reflect multiple changes in the Datagrid.

Your help is greatly appreciated.

Bill

-------------


Private Function showFreightDetail(ByVal rowID As Integer) As Integer
Dim dSQL As String



Dim dr As DataRow

Dim dvRow As DataRowView

dr = dMain.Tables(0).Rows(rowID)

'txtBolID.Refresh()

txtTest.Text = dr.Item("CarrierName")

dSQL = "SELECT CarrierID, FreightLinkID, Mile_From, Mile_To, WF_GasFreight,
WF_DslFreight, "

dSQL += " JACO_GasFreight, JACO_DslFreight FROM WF_Carrier_FreightChart "

dSQL += " WHERE carrierID = " & "'" & Trim(dr.Item("Carrierid")) & "' "

dSQL += " ORDER By CarrierID, Mile_From, Mile_to "





Try

DataGrid2.Enabled = True

Dim iType As Integer = 1

Dim iSort As Integer



dCarrierFreight = New DataSet("mpCarrier")

Dim tFreight As DataTable

tFreight = New DataTable("dpFreightTable")

Dim cRowID As DataColumn

cRowID = New DataColumn("rowID")

Dim cCarrierID As DataColumn

cCarrierID = New DataColumn("CarrierID")

Dim cMileFrom As DataColumn

cMileFrom = New DataColumn("MileFrom")

Dim cMileTo As DataColumn

cMileTo = New DataColumn("MileTo")

Dim cWFGas As DataColumn

cWFGas = New DataColumn("WFGas")

Dim cWFDsl As DataColumn

cWFDsl = New DataColumn("WFDsl")

Dim cJAGas As DataColumn

cJAGas = New DataColumn("JAGas")

Dim cJADsl As DataColumn

cJADsl = New DataColumn("JADsl")







tFreight.Columns.Add(cRowID)

tFreight.Columns.Add(cCarrierID)

tFreight.Columns.Add(cMileFrom)

tFreight.Columns.Add(cMileTo)

tFreight.Columns.Add(cWFGas)

tFreight.Columns.Add(cWFDsl)

tFreight.Columns.Add(cJAGas)

tFreight.Columns.Add(cjaDsl)

dCarrierFreight.Tables.Add(tFreight)

dFreight = MAPPOINTDataBoss.dWFIRecView(dSQL)

If dFreight.Tables(0).Rows.Count = 0 Then

Beep()

Call DisplayStatus("No records found! Nothing to display.", "red")

Else

End If

Dim dFreightRow, dCarrierFreightRow As DataRow

For Each dFreightRow In dFreight.Tables(0).Rows

dCarrierFreightRow = tFreight.NewRow

dCarrierFreightRow("rowID") = iType.ToString

dCarrierFreightRow("CarrierID") = dFreightRow.Item("CarrierID")

dCarrierFreightRow("Milefrom") = dFreightRow.Item("Mile_From")

dCarrierFreightRow("Mileto") = dFreightRow.Item("Mile_To")

dCarrierFreightRow("WFGas") = dFreightRow.Item("WF_GasFreight")

dCarrierFreightRow("WFDsl") = dFreightRow.Item("WF_DslFreight")

dCarrierFreightRow("JAGas") = dFreightRow.Item("JACO_GasFreight")

dCarrierFreightRow("JADsl") = dFreightRow.Item("JACO_DslFreight")





tFreight.Rows.Add(dCarrierFreightRow)



Next



' Datagrid table style setup

Dim ts2 As DataGridTableStyle

ts2 = New DataGridTableStyle

ts2.MappingName = "dpFreightTable"

ts2.AlternatingBackColor = Color.Aqua

ts2.AllowSorting = False

Dim txtCol As DataGridTextBoxColumn

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "CarrierID"

txtCol.HeaderText = "CarrierID"

txtCol.ReadOnly = True

txtCol.Width = 40



txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "MileFrom"

txtCol.HeaderText = "From"

txtCol.Width = 50

ts2.GridColumnStyles.Add(txtCol)

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "MileTo"

txtCol.HeaderText = "To"

txtCol.Width = 50



ts2.GridColumnStyles.Add(txtCol)

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "WFGas"

txtCol.HeaderText = "WF Gas"

txtCol.Width = 60

ts2.GridColumnStyles.Add(txtCol)



txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "WFDsl"

txtCol.HeaderText = "WF Dsl"

txtCol.Width = 60

ts2.GridColumnStyles.Add(txtCol)



txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "JAGas"

txtCol.HeaderText = "Jaco Gas"

txtCol.Width = 60

ts2.GridColumnStyles.Add(txtCol)

txtCol = New DataGridTextBoxColumn

txtCol.MappingName = "JADsl"

txtCol.HeaderText = "Jaco Dsl"

txtCol.Width = 60

ts2.GridColumnStyles.Add(txtCol)



DataGrid2.TableStyles.Clear()

DataGrid2.TableStyles.Add(ts2)



Me.DataGrid2.DataSource = dCarrierFreight

Me.DataGrid2.DataMember = "dpFreighttable"



Catch ex As Exception

Catch ex As SqlException

End Try

End Function
 
C

Cor Ligthert [MVP]

Bill,

A view is not copy
a
Dim a as datatable = myDataTable is not a copy,

It are both other references to the same table

So how do you copy your table.

Cor
 
C

Cor Ligthert [MVP]

Bill,

In the way you tell it, than your datasource is the original table and you
can use that to update, however it can as well be.

mynewtable as datatable = myoldtable.copy

Than you can use mynewtable to update and than you have to decide if you
fill the myoldtable again or merge the mynewtable in it.

It is not an abc sample, so again, tell us first how you copy that table.

Cor
 
C

Cor Ligthert [MVP]

Bill,

You are using a shallow copy, what is very hard to manage. It is a copy of
the references pointing to the original objects.

For a DataTable something without sence. Why are you doing that, while the
datatable has so many other posibilities.

Cor
 
B

Bill Nguyen

What's the best / better way to handle batch update in this case, please?

Thanks

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top