move to the first row

mirzao

Active member
Joined
Nov 10, 2005
Messages
44
Location
Malaysia
Programming Experience
Beginner
Hi everyone,

I'm fairly new in using datagrid. I have a continous loop, but I want to insert the data in only 3 rows. When the last row is filled in, the next data will be inserted into the first row again. How to do it? I thought of deleting the previous data and insert the new one. Is there any pointer that can moves to where we inserted data?
 
I have a code that sends and reads data from the same 3 ports repeatedly. It is for monitoring the device that connected to it. The following code generates rows depending on how many times it reads from ports. I only want to use 3 rows because I use 3 ports. This is my code

Do

For i=1 to 3 'ports
If frun true then

'send request and read data from ports

drow = dt.NewRow

drow("Handler") = MyArray(i - 1)
drow("Idle Time") = count
drow("Temperature Setup loss") = count2

dtable.Rows.Add(drow)


DataGrid1.DataSource = dtable

else if fstop=true then
exit sub
end if

Next

Loop

 
By the sound of it you should not be adding new rows but editing the existing rows. Add your three rows to begin with and then inside the For loop just use the loop counter to get the row at that index, then edit it.
 
mirzao said:
How to only add 3 rows?Can you explain more how to edit..Thanks
You create three rows and add them. You already know how to do that because you're doing it in the code you posted. Simply add three blank rows or with default values at the start, then each time you ets a port just edit the row that corresponds to that port:
VB.NET:
myDataRow =  myDataTable.Rows(index)
 
dr = dt.NewRow
dr("No") = i
dr("Handler") = MyArray(i - 1)
dr("Idle Time") = count
dr("Temperature Setup loss") = count2
dr("Lot Setup loss") = count3
dr("Unscheduled Down Time") = count4
dr("Speed loss") = count5
dt.Rows.Add(dr)
dr.BeginEdit()
dr = dt.Rows(i)

dr.EndEdit()

I tried to run the code and get an error "There is no row at position i"
 
Arrays and collections are indexed from 0. If you are starting your loop counter at 1 then that's where the issue is. You've even used MyArray(i - 1) in your code. And why are you editing a row that you just added?
 
Yeah, I get that. I just defined n=0 outside the loop and put this code inside loop.It works, thank you. But when I started to run there is another error "Index was outside the bound of an array". Ho w yo tackle this?
n = n + 1
If n > 2 Then
dr.BeginEdit()
dr = dt.Rows(i)
dr.EndEdit()
dr("Handler") = MyArray(i)
dr("Idle Time") = count
Else
dr = dt.NewRow
dr("Handler") = MyArray(i)
dr("Idle Time") = count

dt.Rows.Add(dr)
EndIf
 
Last edited:
You use the debugger. That's why you paid for it. If you get an unhandled exception you press the break button to halt execution at the problem line. You then examine the references on that line. You can mouse over variables to see what they contain. You can use the Autos, Locals and Watch windows to evaluate any expression you like. Look at the value you are using as an index and look at the Length of the array or the Count of the collection that you are indexing. Ask yourself why the index is invalid. Where did that index value come from? The method you use to get that index must be faulty.
 
Back
Top