R
Robert J
Hi all,
I am trying to add a new row to a DataGrid that was initially populated
by having it's DataSource set to a DataTable that was filled using a
SqlDataAdapter Fill method.
I vaguely recall something about not being able to add rows to a DataTable
that was populated by an Adapter Fill method. Can someone clear this up
becuase the new row is not showing up in the DataGrid. Should I build the
DataTable myself by looping through the records in the Quesry ?
Well here is the Code:
Dim strSQL As String
' Create Instance of Connection and Command Object
Dim myConn As New SqlConnection
strSQL = "SELECT jhquotedet.qdcode, jhquotedet.qddesc, 0 as
AddHrs, "
strSQL = strSQL & "jhquotedet.qdprice,
jhquotedet.qdrecommended, jhquotedet.qdcat, "
strSQL = strSQL & "jhquotedet.qdquantity,
jhquotedet.qdrepairer, jhquotedet.qdaudit "
strSQL = strSQL & "FROM jhquotehed LEFT OUTER JOIN
jhquotedet ON "
strSQL = strSQL & "jhquotehed.qhkey = jhquotedet.qdquote "
strSQL = strSQL & "Where jhquotehed.qhmta = " & MTA
myConn.ConnectionString = "server=" & Server & "; uid=" &
myUser & "; password=" & myPwd & "; database=" & Dbase
Dim myComm As New SqlCommand(strSQL, myConn)
myComm.CommandTimeout = 30
Dim DA As SqlDataAdapter = New SqlDataAdapter
DA.SelectCommand = myComm
' Execute the command
myConn.Open()
Dim DTable As DataTable = New DataTable
DTable.TableName = "RectItems"
DA.Fill(DTable)
grdRectItems.DataSource() = DTable
Code to Add New Row:
Dim DTable As DataTable
Dim DRow As DataRow
' Get the DataTable associated with this datagrid.
DTable = CType(grdRectItems.DataSource, DataTable)
' Create new row from DataTable.
DRow = DTable.NewRow()
' Add data to each column in the new row.
DRow(0) = strCode
DRow(1) = Trim(qdDesc.Text)
DRow(2) = Trim(ExtraHours.Text)
DRow(3) = dbPrice
DRow(4) = qdRecommended.Text
DRow(5) = strCat
DRow(6) = CInt(Val(inQty.Text))
DRow(7) = strSupplier
DRow(8) = 0
DTable.Rows.Add(DRow)
' Tried setting the DataSource to Nothing first no change
grdRectItems.DataSource() = Nothing
grdRectItems.DataSource() = DTable
' Tried this to get the New Row to display
grdRectItems.Refresh()
grdRectItems.Invalidate()
Thanks for any help
Robert
I am trying to add a new row to a DataGrid that was initially populated
by having it's DataSource set to a DataTable that was filled using a
SqlDataAdapter Fill method.
I vaguely recall something about not being able to add rows to a DataTable
that was populated by an Adapter Fill method. Can someone clear this up
becuase the new row is not showing up in the DataGrid. Should I build the
DataTable myself by looping through the records in the Quesry ?
Well here is the Code:
Dim strSQL As String
' Create Instance of Connection and Command Object
Dim myConn As New SqlConnection
strSQL = "SELECT jhquotedet.qdcode, jhquotedet.qddesc, 0 as
AddHrs, "
strSQL = strSQL & "jhquotedet.qdprice,
jhquotedet.qdrecommended, jhquotedet.qdcat, "
strSQL = strSQL & "jhquotedet.qdquantity,
jhquotedet.qdrepairer, jhquotedet.qdaudit "
strSQL = strSQL & "FROM jhquotehed LEFT OUTER JOIN
jhquotedet ON "
strSQL = strSQL & "jhquotehed.qhkey = jhquotedet.qdquote "
strSQL = strSQL & "Where jhquotehed.qhmta = " & MTA
myConn.ConnectionString = "server=" & Server & "; uid=" &
myUser & "; password=" & myPwd & "; database=" & Dbase
Dim myComm As New SqlCommand(strSQL, myConn)
myComm.CommandTimeout = 30
Dim DA As SqlDataAdapter = New SqlDataAdapter
DA.SelectCommand = myComm
' Execute the command
myConn.Open()
Dim DTable As DataTable = New DataTable
DTable.TableName = "RectItems"
DA.Fill(DTable)
grdRectItems.DataSource() = DTable
Code to Add New Row:
Dim DTable As DataTable
Dim DRow As DataRow
' Get the DataTable associated with this datagrid.
DTable = CType(grdRectItems.DataSource, DataTable)
' Create new row from DataTable.
DRow = DTable.NewRow()
' Add data to each column in the new row.
DRow(0) = strCode
DRow(1) = Trim(qdDesc.Text)
DRow(2) = Trim(ExtraHours.Text)
DRow(3) = dbPrice
DRow(4) = qdRecommended.Text
DRow(5) = strCat
DRow(6) = CInt(Val(inQty.Text))
DRow(7) = strSupplier
DRow(8) = 0
DTable.Rows.Add(DRow)
' Tried setting the DataSource to Nothing first no change
grdRectItems.DataSource() = Nothing
grdRectItems.DataSource() = DTable
' Tried this to get the New Row to display
grdRectItems.Refresh()
grdRectItems.Invalidate()
Thanks for any help
Robert