|
I'm having problems with looping thru a datagrid to get calculations for each record. Help with my for loop will be greatly appreciated.
Dim intRec As Integer
'My Issue is Here.
For intRec = 0 To dgOrderReport.DataSource
Select Case DatePart("w", d1)
Case 1
subtract = subtract + 1
Case 7
subtract = subtract + 2
End Select
Select Case DatePart("w", d2)
Case 1
subtract = subtract + 2
Case 7
subtract = subtract + 1
End Select
absday = DateDiff("d", d1, d2) + 1 - subtract
fncGetDays = absday - CInt(2 * (absday \ 7))
If fncGetDays < 0 Then fncGetDays = 0
intRec += 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
Thank You<code></code>
|
|
|
|
|
I cannot figure out what you're trying to do...But I do notice that you've got a For loop and you never use the counter inside of the loop for anything.
For intRec = 0 To dgOrderReport.DataSource
needs to be changed to
For intRec = 0 To dgOrderReport.DataSource.Rows.Count - 1
just for starters
|
|
|
|
|
Basically what I'm trying to do is - subtract a time field in my record from my system time for each record. Business Days Only
example
order NO Name Req_date Days late
11111 Test 8/17/2007 3
Days Late is a dynamic column
I need to do this for each record - and I can not get my loop to work - when it loops it I only get the calculation for the first record and it puts that calculation in every record. Thanks For the Reply - I've changed it to a dataset so that I can use rows.count-1 -- This is what I have
For intRec = 0 To MyDataSet.Tables(0).Rows.Count - 1
Select Case DatePart("w", d1)
Case 1
subtract = subtract + 1
Case 7
subtract = subtract + 2
End Select
Select Case DatePart("w", d2)
Case 1
subtract = subtract + 2
Case 7
subtract = subtract + 1
End Select
absday = DateDiff("d", d1, d2) + 1 - subtract
fncGetDays = absday - CInt(2 * (absday \ 7))
If fncGetDays < 0 Then fncGetDays = 0
intRec += 1
Next<code></code>
Thank You
|
|
|
|
|
somewhere in here you need to have something to the effect of
MyDataSet.Tables(0).Rows(intRec).Item("Days Late") = value
where do you assign this value? In one of the functions?
|
|
|
|
|
' MyDataSet.Tables(0).Rows(intRec).Item("Days Late") = value '
This is the value that I'm trying to find for each record which I believe I am getting with ' fncGetDays = absday - CInt(2 * (absday \ 7)) '
after the date calculations
I'm getting that value within that loop? Should I or should I not do it that way?
I think that should be fine
How do I get the loop to go to the next record after getting the value for the first record and so on?
Thank You
|
|
|
|
|
Now that you have a dataset you can easily use the datarow approach:
<br />
Dim rw As DataRow<br />
For each rw in MyDataSet.Tables(0).Rows<br />
MsgBox(rw.Item("Days Late"))<br />
Next<br />
This will loop through each row, and will stop once there are no more rows.
Ofcourse you'll want to use rw.Item in something other than a MsgBox...
Good luck,
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
Thanks -
I'm still only getting the calculations for the first record - what in world am I missing?
Should I put this
Dim rw As DataRow
For each rw in MyDataSet.Tables(0).Rows
rw.Item("Days Late"))
Next
within my current for loop or should I just get rid of what I have and just use
the approach above?
Thank You
|
|
|
|
|
So I'm still trying with this disaster loop...no luck,
I've moved things around a bit. For less confusion that I may be having...
'I create My Dynamic Data Column - Days Late
Dim dc As DataColumn
dc = New DataColumn("Days Late")
MyDataSet.Tables(0).Columns.Add(dc)
'Create my variables
Dim x As Integer
Dim row As Integer = 0
'This is my loop that does not go to the next record
For row = 0 To MyDataSet.Tables(0).Rows.Count - 1
x = DateDiff("d", dgOrderReport.Item(row, 3), System.DateTime.Today)
'Place Result into "Days late" Column
dc.Expression = x
Next row
I believe I'm missing row =+ 1 - but when I put this within my loop I get the infinite loop - I need to exit that for correct? Will this give me the results I need.?.
I've also tried it this way
'I create My Dynamic Data Column - Days Late
Dim dc As DataColumn
dc = New DataColumn("Days Late")
MyDataSet.Tables(0).Columns.Add(dc)
Dim dr As DataRow
Dim x As Integer
For Each dr In MyDataSet.Tables(0).Rows
x = DateDiff("d", dgOrderReport.Item(0, 3), System.DateTime.Today)
~~~~I have problems here - how will I give the column the value of x???
dc.Expression = dr.Item("days Late")
next dr
What is the best way to do this?
Thank You
|
|
|
|
|
Don't panic.
So let's take it a step at a time:
1. Forget about your old loop (For row = 0 To MyDataSet.Tables(0).Rows.Count - 1 ), its a recepy for disaster.
2. Not really important, but you don't need to write next dr , just next will do.
3. Did adding the column work? Although the loop isn't really working, are you getting any errors pertaining to the dataset? If so, you may have to add a line like MyDataSet.AcceptChanges() right after you added the new column.
4. The bit: x = DateDiff("d", dgOrderReport.Item(0, 3), System.DateTime.Today) won't work. You will have to adapt it also to the new method, something like this:
dim MyDate as Date<br />
MyDate = dr.Item("Column name of the column with the date")<br />
x = MyDate.Substract(Now).Days
Or shorter still:
dim MyDate as Date<br />
MyDate = dr.Item("Column name of the column with the date")<br />
dr.Item("days Late") = MyDate.Substract(Now).Days<br />
5. I didn't quite understand whether you are trying to get the value from dr.Item("days Late") or whether you are trying to register the value of x into dr.Item("days Late") . Either way its easy enough:
Getting: x = dr.Item("days Late") (copy the value of the column to x)
Registering: dr.Item("days Late") = x (copy the value of x to the column)
Hope this works for you,
Johan
My advice is free, and you may get what you paid for.
|
|
|
|
|
I am creating an application in which there is a Button and a ListView Control.
I am using VB2005.
The Subroutine below just copies the listviewitem's and subitem's text.
Controls: A button named "Search" and a listview with CheckBoxes property enabled named "lv1"
But the error comes:
System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'lv1' accessed from a thread other than the thread it was created on."
I know i have to use Delegate to Marshall Call. But i am not getting it properly.
Here is the Problem Code:
Private Sub startsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startsearch.Click<br />
Dim trd As Thread<br />
trd = New Thread(AddressOf Me.Search)<br />
trd.Start()<br />
End Sub<br />
<br />
Private Sub Search()<br />
Dim i As Integer<br />
<br />
For i = 0 To lv1.Items.Count - 1<br />
<br />
If (lv1.Items.Item(i).Checked = False) Then 'Error Comes here<br />
Debug.Write("Checked=True")<br />
Else<br />
GoTo increase<br />
End If<br />
<br />
Dim a as String = lv1.Items.Item(i).SubItems.Item(1).Text 'Here<br />
Dim b as String = lv1.Items.Item(i).SubItems.Item(2).Text 'Here<br />
Dim c as String = lv1.Items.Item(i).SubItems.Item(3).Text 'Here<br />
<br />
increase:<br />
Next i<br />
End Sub<br />
Can anyone plz give me the corrected code for the above?????
|
|
|
|
|
The problem is simple. You're accessing a control from a thread other than the one the control was created on, namely the, UI thread. This is prone to problems and isn't allowed.
This code doesn't belong in a second thread since it primarily deals with the state of the contents of the control. You get no advantage by using a background thread here.
Also, the Goto statement is completely unnecessary and the logic of this section is very screwed up. Rewritten, you code should look like:
For i = 0 to lv1.Items.Count - 1
If Not lv1.Items(i).Checked Then
Debug.WriteLine("Checked=True")
Dim a As String = ...
Dim b As String = ...
Dim c As String = ...
' You might want to actually do something with these strings here...
End If
Next i
|
|
|
|
|
Hi
Can anybody tell me how can I associate different event handlers for an array of controls that I have created dynamically.
Suppose I create ten buttons at runtime. I want that every button perform differnt action e.g. give a message of such type.
"You clicked" & (Button.Name)
How this can be done.
Thanks
reman
|
|
|
|
|
If you are using vb.net you need to use addhandler:
Associates an event with an event handler at run time.
AddHandler event, AddressOf eventhandler
Hope that helps.
Ben
|
|
|
|
|
Adding to what kubben said, you can use the same handler for all buttons,
then inside that handler use the first argument "sender", cast it to Button
and you can read the button's name, or whatever you want to access.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I am trying to get values from MS Access Db and calculate the time diff and display data in gridview using arrays but unable to do this. I am new to this coding, can some one help me on this.
Example of DB
Employeename date time inout
aaa 8/1/2007 8:00 in
aaa 8/1/2007 12:00 out
aaa 8/1/2007 13:00 in
aaa 8/1/2007 18:00 out
Out as
aaa 8/1/2007 8:00 12:00 13:00 18:00 8:00(hrs for the day)
Can someone help me on this.
|
|
|
|
|
hi that's preety simple
take 2 datetime variables for 2 dates coming from database and store them as follows
dim dt1 as DateTime,dt2 as DateTime
dt1= #<fieldname of="" table="">#
dt2=#<fieldname of="" table="">#
then
dim t as TimeSpan = dt1.subtract(dt2)
then u can show using t.hours , t.minutes and t.seconds
Hope that helps
|
|
|
|
|
don't forget to add field names withing the hash(#) like
dt1= #field1#
dt2=#field2#
|
|
|
|
|
I'm building a system to create batch files, such as fee statement for every person, and one person per page. Could anyone tell me what the steps are to do this, please?
Also, is it possible to code such as the summarised total amount of fee payment by all people on a single file? I mean creating a method on the crystal report with just a text field of that total amount. Because I can't find a way to do it.
Your Helps are Deeply Appreciated. Thank You Very Much
|
|
|
|
|
Hello!
I have a 'picture box' on a windows form. When i click witin picture box a textBox control is added to picturebox at the location where i have clicked.
I want to know, how can i drag that text box within the picturebox at runtime?
I would be thankful for any help....
colgateas900197
|
|
|
|
|
By writing code to capture mouse actions on the textbox, and to change it's properties so it follows the mouse.
Also, you will probably find it easier if you get rid of the picturebox, draw the image yourself, and just add the textbox to your form. The picturebox is adding complication that you do not need.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Thanks! I am working on it... Hopfully it will work!
colgateas900197
|
|
|
|
|
i have used this command on the VS 2003 but am using VS 2005, VB and its telling me that file not found, and even i experimentally create the file, i think there is an error in it but can get the exact error
Shell("cmd /c net view " > "C:\cnames.txt", AppWinStyle.Hide, True)
guess its suppossed to get the computers on the lan and save the whole view onto a txt file....
whats the file not found error coming from,....any1 wanna help? Thanks
phatkin
|
|
|
|
|
Why would you want the '>' to be outside the quotes? Why is your command not:
Shell("cmd /c net view > C:\cnames.txt", AppWinStyle.Hide, True)
I'd guess that it's actually resolving the first part of your parameters, so what you've got is actually:
Shell(False, AppWinStyle.Hide, True)
|
|
|
|
|
Thanks so much.
thats what i wanted to do
it works now
\Thanks
phatkin
|
|
|
|
|
Hi,
Is it possible to dynamically change the name of the web services ??
If yes, Kindly let me know how to go about it.
Thanx in advance.
Susheel Kumar
|
|
|
|