datagrid -newrow()

  • Thread starter Thread starter Agnes
  • Start date Start date
A

Agnes

I only one datagrid, and one button.
the buttonwill process ' dtmyTable.newrow()' , However, the datagrid seems
didn't add the row byitself ??
Thanks
 
Agnes,

Is your code something as?
dim dr as datarow = dtMyTable.NewRow()
dtMytable.rows.add(dr)

When not make it that way (you can do it in one sentence as well)?

I hope that helps?

:-)

Cor
"
 
Thanks Cor,
There is a property called dtMyTable.insertat (dr,position)
my grid got 5 rows, i need to insert the new row between 2nd and 3rd
I try dtMyTable.insertat(dr,2) . but it seems fail .. it still add to the
last row.
Can we do that "insert a row in specific row" ???
thanks again
 
Hi Agnes,

I have seen this problem before and I did not get it to work.

Maybe somebody else know the problem.

Cor
 
Only reflected in the DataRowsCollection returned by Rows.

Dim nr As DataRow
Dim x As Int32

nr = DataSet11.Tables(0).NewRow
nr(0) = "Ox"
DataSet11.Tables(0).Rows.InsertAt(nr, 2)

For x = 0 To DataSet11.Tables(0).Rows.Count - 1

Debug.WriteLine(DataSet11.Tables(0).Rows(x).Item(0).ToString())

Next


//OUTPUT

Highfield Road
Abbey Close
Ox
Bower Way
Bower Way
Roadway To Hell


--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
I search from other newsgroup . some
said-------------------------------------------
The MSDN says that InsertAt could not be a solution to your problem. I think
this is an answer, that is: there is nothing wrong in the way you use the
InsertAt, but simply it cannot do what you expect it does. In other words,
it makes no much sense insisting in solving this matter using InsertAt, but
find alternatives like moving the rows in other DataTable object in the
order you like.
------------------------------------------------------------
 
Dim mdt As DataTable = Me.DataSet11.Tables(0).Copy

This will sort it




--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Hi Terry

Because of your message I was thinking what was the kull again.

We had the same conclussion.

:-)

Cor

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("bla")
dt.Columns.Add("Oxes")
Dim mystring() As String = {"Highfield Road", _
"Abbey(Close)", "Bower(Way)", "Bower(Way)", "Roadway To Hell"}
For i As Integer = 0 To mystring.Length - 1
Dim dr As DataRow = dt.NewRow
dr(0) = mystring(i)
dt.Rows.Add(dr)
Next
Dim dr2 As DataRow = dt.NewRow
dr2(0) = "Ox"
dt.Rows.InsertAt(dr2, 2)
Dim dtnew As DataTable = dt.Copy
dt = dtnew.Copy
DataGrid1.DataSource = dt
End Sub
 
Cor, your code work great.
However, my gird is master-detail relationship.
during my form load, My master and detail use the same dataset and with the
constraint key
I try dgDetail.datasource = dt
it seems reset my relationship ,all data are shown .......
 
Cor, your code work great.
However, my gird is master-detail relationship.
during my form load, My master and detail use the same dataset and with the
constraint key
I try dgDetail.datasource = dt
it seems reset my relationship ,all data are shown .......
daInv.fill(dsInvoice,"invoiceheader")
daInv.fill(dsInvoice,"invoicedetail")

dsInvoice.relations.add("fk_invno,".................................etc)

Me.dgHeader.DataSource = dsInvoice
Me.dgHeader.DataMember = "invoiceheader"

Me.dgDetail.DataSource = dsInvoice
Me.dgDetail.DataMember = "Header.fK_invno"

So , as i choose the record in Header, the detail will switch too.......
 
Actually, you can to an update on your adapter if you want or if you dont
have one, just to a Table.AcceptChanges and this will put the data in the
right order.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Hi Terry,

The first one could be done, the second one (acceptchanges) is written
somewhere I know, however that is the wrong solution. After that there are
no updates anymore processed.

Cor
 
Hi Agnes,

Can you try it yourself what the same idea with the dataset.copy does?

dim dsnew as dataset = ds.copy
ds = dsnew.copy

Cor
 
Try your code, this time the detail grid won't show all the data.....
But as I run dt.insertat(dr,dgDetail.currentrowindex)
I test the currentrowindex which return correct row number. However, it
always insert the wrong row.
And this method only allow me to add one row.... [I put all the code in the
button's click event]
after press "once", it work but in wrong row, secondtime. no response.
 
Agnes,

Strange this works for me

Can you try it, do not forget to set that AllowSorting to false with this
method otherwise you get problems later.

I hope this helps?

Cor

\\\
Dim ds As New DataSet
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("bla")
ds.Tables.Add(dt)
dt.Columns.Add("Oxes")
DataGrid1.AllowSorting = False
Dim mystring() As String = {"Highfield Road", _
"Abbey(Close)", "Bower(Way)", "Roadway To Hell"}
For i As Integer = 0 To mystring.Length - 1
Dim dr As DataRow = dt.NewRow
dr(0) = mystring(i)
dt.Rows.Add(dr)
Next
DataGrid1.DataSource = ds.Tables(0)

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim dr2 As DataRow = ds.Tables(0).NewRow
dr2(0) = "Ox"
ds.Tables(0).Rows.InsertAt(dr2, DataGrid1.CurrentRowIndex)
Dim dsnew As DataSet = ds.Copy
ds = dsnew.Copy
DataGrid1.DataSource = ds.Tables(0)
End Sub
 
Back
Top