Add Row to DataGrid populated by DataTable from SQL Query

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
 
A

Alex Feinman [MVP]

Try either calling DataTable.AcceptChanges() or using
DataTable.LoadDataRow( arr, true ) to add a row.
 
A

Alex Feinman [MVP]

I don't know that there is a problem at all. In my tests it simply works as
expected. I add a row and the grid is immediately updated. My suggestion was
just a fishing expedition
 
R

Robert J

Thanks Alex: I found that is was working, I just had another call on the
Lost Focus of
a control that recalled in the old data.

Robert
 

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