how to change the TotalItemCount in PagedCollectionView in silverlight - silverlight-4.0

I am trying to do paging in datagrid silverlight
I am using PagedCollectionView for paging.
<sdk:DataPager Name="RequestDataPager" PageSize="10" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Path=PagedRequestRepFamilies}" HorizontalContentAlignment="Center" Width="{Binding ElementName=RequestDataGrid, Path=ActualWidth}"></sdk:DataPager>
My Req :
Here for 1 st time when datagrid loaded, it fetches Top 10 Records event though it's record count is 100
I need to change the item count to 100 instead of 10 when I am trying to change the itemcount it's saying itemcount property is readonly and cannot change.
when I click page 2 it should go to database and fetch back the next 10 records and the itemcount should still be 100 and it should go on..

Here is the Answer :
Create CurrentPageIndex property as Integer in ViewModel:
I am passing the pageindex and page size to the service and returns the 100 Records as :
result.Response = result.Response.Skip(Pageindex * pagesize).Take(pagesize).ToList()
Private Sub RequestDataGrid_PageIndexChanging(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RequestDataPager.PageIndexChanging
Dim str As String = String.Empty
If Not GetDataContext(Of RequestListViewModel).Pageindexchanged Then
If (RequestDataPager.PageIndex +1) >= 1 Then
GetDataContext(Of RequestListViewModel).GRDPager = RequestDataPager
GetDataContext(Of RequestListViewModel).Pageindexchanged = False
GetDataContext(Of RequestListViewModel).PageindexLoadingrow = False
GetDataContext(Of RequestListViewModel).IsCallRequestRepFamily = False
End If
End If
'RequestDataPager.PageIndex = GetDataContext(Of RequestListViewModel).RecordCount
End Sub
In PageIndexChanging you cannot get the Current Page index, to get the current page index in the loading row event:
Private Sub RequestDataGrid_LoadingRow(ByVal sender As System.Object, ByVal e As System.Windows.Controls.DataGridRowEventArgs) Handles RequestDataGrid.LoadingRow
e.Row.Header = e.Row.GetIndex + 1 + (CType(RequestDataGrid.DataContext, RequestListViewModel).PagedRequestRepFamilies.PageIndex * CType(RequestDataGrid.DataContext, RequestListViewModel).PagedRequestRepFamilies.PageSize)
If Not GetDataContext(Of RequestListViewModel).Pageindexchanged Then
If Not GetDataContext(Of RequestListViewModel).PageindexLoadingrow Then
GetDataContext(Of RequestListViewModel).CurrentPageIndex = RequestDataPager.PageIndex
If Not GetDataContext(Of RequestListViewModel).IsCallRequestRepFamily Then
GetDataContext(Of RequestListViewModel).FindRequestRepFamilies(GetDataContext(Of RequestListViewModel).SelectedRepFamilies)
GetDataContext(Of RequestListViewModel).IsCallRequestRepFamily = True
End If
End If
End If
End Sub
IsCallRequestRepFamily , Pageindexchanged properties are managed accordingly in the call back of the FindRequestRepFamilies
I am settign the page count for datagridpager in viewmodel after binding the collection as GRDPager.PageIndex = CurrentPageIndex

Related

Visual Basic- Loop to make listbox update as checkbox is checked

I'm having trouble having the listbox update as the checkbox is checked. I have a total of 8 "test_location" check boxes and I want the listbox to add items to "Steps_Queue_List" and store "1" in the "Test_Locations" array when the location is checked. Also want to clear the list when the checkbox is unchecked. This works so far but I would much prefer to learn how to make a loop for this:
Private Sub Location_CheckBox_1_CheckedChanged(sender As Object, e As EventArgs) Handles Location_CheckBox_1.CheckedChanged
If Location_CheckBox_1.Checked Then
Test_Locations(0) = 1
Steps_Queue_List.Items.Add("test for location" & 1, 1)
ElseIf Location_CheckBox_1.Checked = False Then
Test_Locations(0) = 0
Steps_Queue_List.Items.RemoveAt(0)
End If
End Sub
Private Sub Location_CheckBox_2_CheckedChanged(sender As Object, e As EventArgs) Handles Location_CheckBox_2.CheckedChanged
If Location_CheckBox_2.Checked Then
Test_Locations(1) = 1
Steps_Queue_List.Items.Add("test for location" & 2, 2)
ElseIf Location_CheckBox_2.Checked = False Then
Test_Locations(1) = 0
Steps_Queue_List.Items.RemoveAt(0)
End If
End Sub
Private Sub Location_CheckBox_3_CheckedChanged(sender As Object, e As EventArgs) Handles Location_CheckBox_3.CheckedChanged
If Location_CheckBox_3.Checked Then
Test_Locations(2) = 1
Steps_Queue_List.Items.Add("test for location" & 3, 3)
ElseIf Location_CheckBox_3.Checked = False Then
Test_Locations(2) = 0
Steps_Queue_List.Items.RemoveAt(0)
End If
End Sub
Thanks in advance.
You don't need a loop but you can just handle everything in a single method.
Set the property Tag of your Checkboxes to a progressive value starting from 1 to 8 matching the text value you want to be displayed in the listboxes.
Then setup an event handler that manages all the CheckBoxChanged events for all the CheckBox.
In this event handler retrieve the tag and use it to address the array index and the listbox to update
' Handle all Checkbox changed with the same handler
Private Sub OnCheckBoxChanged(sender As Object, e As EventArgs)
Handles Location_CheckBox_1.CheckedChanged,Location_CheckBox_2.CheckedChanged,
Location_CheckBox_3.CheckedChanged,Location_CheckBox_4.CheckedChanged,
Location_CheckBox_5.CheckedChanged,Location_CheckBox_6.CheckedChanged,
Location_CheckBox_7.CheckedChanged,Location_CheckBox_8.CheckedChanged
' Discover which checkbox has been clicked
Dim chk = DirectCast(sender, CheckBox)
' Now read the value of the Tag property of that checkbox
Dim idx = Convert.ToInt32(chk.Tag)
If chk.Checked Then
Test_Locations(idx - 1) = 1
Steps_Queue_List.Items.Add("test for location" & idx, idx)
Else
Test_Locations(idx - 1) = 0
Steps_Queue_List.Items.RemoveAt(0)
End If
End Sub

delay or stop execution for some time in vb.net

hi i want to delay the execution of code for some time on buton click i cal a func named chance() ..
which gets called after a picture box.image change .. bt the image does not change nd func chance() starts ... i want delay in chance() after the picture is changed ... thus help me ..
code
Private Sub p11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles p11.Click
row = 1
col = 1
chck() 'function which returns hit var value
p11.Enabled = False
If hit = 1 Then
p11.Image = Image.FromFile("G:\visual progs\BATTLESHIP\hit.png")
ElseIf hit = 0 Then
p11.Image = Image.FromFile("G:\visual progs\BATTLESHIP\miss.png")
lblstatus.Text = "COMPUTER's TURN ... PLEASE WAIT ... "
chance() ' func begins
End If
End Sub
Function chance()
***'here i want a pause for 2 sec***
Dim z As Int16 = 1
While z = 1
row = mnw.Next(9) + 1
col = mnw.Next(9) + 1
If c(row, col) = False Then
c(row, col) = True
z = 0
End If
End While
chck1() ' checks for hit or miss for computer
changepic() 'changes pic hit or miss for computer
Return 0
End Function
To get a pause of 2 seconds, you simply need to suspend the thread by doing this:
Thread.Sleep(2000)
Although you can use Thread.Sleep to introduce a delay, it has generally undesirable side-effects, in particular the form and its controls become unresponsive. A better way is to use a timer - that way the form is still responsive (e.g. you can move it around).
Also, you seem to be a little unsure of where to use a Sub and where to use a Function. A Sub does something, and a Function is used to return a value, ideally with no side-effects.
You could try this with a new Windows Forms project and just a PictureBox named p11 and a Label named lblStatus:
Imports System.IO
Public Class Form1
Dim tim As Windows.Forms.Timer
Const GAMEPATH As String = "G:\visual progs\BATTLESHIP\"
Dim HitImgFile As String = Path.Combine(GAMEPATH, "hit.png")
Dim MissImgFile As String = Path.Combine(GAMEPATH, "miss.png")
Private Sub SetUpTimer()
tim = New Timer
tim.Interval = 2000 ' milliseconds
tim.Enabled = False
AddHandler tim.Tick, AddressOf Chance
End Sub
Private Sub Chance(sender As Object, e As EventArgs)
tim.Enabled = False
' your code for the computer's turn goes here
lblStatus.Text = "Your turn"
p11.Enabled = True
End Sub
Private Function IsHitByUser() As Boolean
' placeholder code for the actual check
If Rnd() < 0.5 Then
Return True
End If
Return False
End Function
Private Sub DoComputerTurn()
lblStatus.Text = "COMPUTER's TURN ... PLEASE WAIT ... "
p11.Enabled = False
tim.Enabled = True
End Sub
Private Sub p11_Click(sender As Object, e As EventArgs) Handles p11.Click
If IsHitByUser() Then
p11.Image = Image.FromFile(HitImgFile)
lblStatus.Text = "HIT"
Else
p11.Image = Image.FromFile(MissImgFile)
DoComputerTurn()
End If
End Sub
Private Sub StartGame()
lblStatus.Text = "Your turn"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetUpTimer()
StartGame()
End Sub
End Class

Checkbox returned in Event Handler isn't one on form

I'm creating an array of checkboxes on a form dynamically; the code that creates the array looks like this:-
checkbox_array(count_of_checkboxes) = New CheckBox
if (count_of_checkboxes = 0) Then
checkbox_array(count_of_checkboxes).Top = specimen_checkbox.Top
checkbox_array(count_of_checkboxes).Left = specimen_checkbox.Left
else
checkbox_array(count_of_checkboxes).Top = checkbox_array(count_of_checkboxes - 1).Top + vertical_offset
checkbox_array(count_of_checkboxes).Left = checkbox_array(count_of_checkboxes - 1).Left + horizontal_offset
End If
my_panel.Controls.Add(checkbox_array(count_of_checkboxes))
AddHandler checkbox_array(count_of_checkboxes).MouseClick, cbxSpecimen_CheckedChanged
checkbox_array(count_of_checkboxes).Name = someValue
checkbox_array(count_of_checkboxes).Text = someValue
checkbox_array(count_of_checkboxes).Enabled = true
checkbox_array(count_of_checkboxes).Visible = true
checkbox_array(count_of_checkboxes).Show()
This works fine and dandy on one form. However, I am using the same code on a form which is derived from a base form, and running into a problem, in that the object returned in the sender parameter, although clearly a checkbox with a recognisable name, isn't any of the checkboxes in the array.
I verified this with:-
Private Sub cbxSpecimen_CheckedChanged( sender As System.Object, e As System.EventArgs) Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To checkbox_array.GetUpperBound(0) - 1
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
End Sub
Can anyone shed any light on why this should work on a normal form, but not a derived-class form?
I verified this with:-
Private Sub cbxSpecimen_CheckedChanged( sender As System.Object, e As System.EventArgs)
Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To checkbox_array.GetUpperBound(0) - 1
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
End Sub
Why checkbox_array.GetUpperBound(0) - 1? This will skip the last element in the array. Try:
For i As Integer = 0 To checkbox_array.GetUpperBound(0)
If checkbox_array(i).Equals(sender) Then
// set a breakpoint here
End If
Next i
Or
For i As Integer = 0 To checkbox_array.Length - 1
...
I have managed to get this to work by refilling the array of checkboxes inside the click event:-
Private Sub cbxSpecimen_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles cbxSpecimen.CheckedChanged
For i As Integer = 0 To check_boxes.GetUpperBound(0)
If check_box_array(i).Name = CType(sender, CheckBox).Name And
Not check_box_array(i).Equals(sender) Then
check_box_array(i) = CType(sender, CheckBox)
End If
Next i
' do useful work
End Sub
After the check box on the form has been stuffed back into the array, it remains there (so the second invokation for the same checkbox doesn't insert into the array a second time).
This seems like a ghastly hack to me, but I'll go with it for the time being.

Incrementing Label Value when a button is clicked

I have tried converting using CInt, TryParse, Convert.Int32, etc but nothing worked. I just need to display the total votes of the candidated when a button btnVote is clicked and display the result when I click btnResult. I've tried searching on the net also but still no luck so I came up to this website and hopefully I could solve my problem with my program in VB.net
Below is my code:
Public Class Presidential_Election
Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
If cboCandidates.Text = "NOLI" Then
lblNoli.Text = Integer.Parse(lblNoli.Text) + 1
ElseIf cboCandidates.Text = "VAL" Then
lblVal.Text = (CInt(lblVal.Text) + 1).ToString
ElseIf cboCandidates.Text = "LESTER" Then
lblLester.Text = CInt(lblLester.Text) + 1
ElseIf cboCandidates.Text = "GWAPO" Then
lblGwapo.Text = CInt(lblGwapo.Text) + 1
End If
End Sub
Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
lblNoli.Visible = True
lblVal.Visible = True
lblLester.Visible = True
lblGwapo.Visible = True
End Sub
End Class
I would set an integer counter for each candidate. Set these to 0 and then increment them as they receive a vote. When you display the results just bind the text of the label to its respective counter and make it visible. This way, you don't have to keep referencing label.text.
If you haven't set the label text to be 0 at the beginning, using CInt would definitely throw an error. You have two options :
set all labels text to 0 and keep using CInt, or
use Val()
Example :
lblGwapo.Text = Val(lblGwapo.Text) + 1

Programmatically change selection on DatagridView (.NET)

I'm learning VB.NET.
I've a problem with DataGridView component when trying to set the value of the CurrentCell.
What i'm trying to do is :
I've a DataGridView With values.
I want to make a button in my forms and when clicking on it I want to change the selection from the current row to the next. To explain more, by clicking my Button I want to simulate the effect of a mouse click on a DataGridview.
I hope you can help me,
Thanks!
Maybe something like this:
If DataGridView1.RowCount > 0 Then
Dim MyDesiredIndex As Integer = 0
If DataGridView1.CurrentRow.Index < DataGridView1.RowCount - 1 Then
MyDesiredIndex = DataGridView1.CurrentRow.Index + 1
End If
DataGridView1.ClearSelection()
DataGridView1.CurrentCell = DataGridView1.Rows(MyDesiredIndex).Cells(0)
DataGridView1.Rows(MyDesiredIndex).Selected = True
End If
Note 1: maybe these two lines are not necessary. I havenĀ“t proved it
DataGridView1.ClearSelection()
DataGridView1.CurrentCell = DataGridView1.Rows(MyDesiredIndex).Cells(0)
Note 2: note that if we are in the last row, it goes to first
You need to set the particular row's Selected property to true. I think the VB would be something like this:
someDGV.Rows(index).Selected = True
If your data grid is bound to a BindingSource, it is better to change the position there:
Object key = Convert.ToInt32(cdr["WordList"]);
int itemFound = lexiconNamesBindingSource.Find("ID_Name", key);
lexiconNamesBindingSource.Position = itemFound;
...and you might need to finish it off with:
lexiconNamesBindingSource.ResetBidings();
(This is an old thread, but I found it, so someone else might find this useful)
Just use the BindingSource.MoveNext() and BindingSource.MovePrevious() methods.
You could do it this way:
If DataGridView1.CurrentRow.Index < DataGridView1.Rows.Count Then
DataGridView1.Rows(DataGridView1.CurrentRow.Index + 1).Selected = True
End If
To get the selected row, you should use SelectedRows(0).Index inspite of CurrentRow. Because if you programmaticaly make a row as selected, then next time you will find 0 in CurrentRow.Index. So It would be like :
If DataGridView1.SelectedRows(0).Index < DataGridView1.RowCount - 1 Then
MyDesiredIndex = DataGridView1.SelectedRows(0).Index + 1
End If
DataGridView1.Rows(MyDesiredIndex).Selected = True
expanding on the answer above which is perfect considering I spent at least 4 hours loooking for this.
and assuming that your datagridview is called dgvDevices... this code will handle the event in which you go outbounce as you move back and forward on your rows
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
Try
dgvDevices.ClearSelection()
Dim currentr As Integer = dgvDevices.CurrentCell.RowIndex
dgvDevices.CurrentCell = dgvDevices.Rows(currentr - 1).Cells(0)
dgvDevices.Rows(currentr - 1).Selected = True
Catch ex As Exception
dgvDevices.CurrentCell = dgvDevices.Rows(0).Cells(0)
dgvDevices.Rows(0).Selected = True
End Try
End Sub
Private Sub btnForw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnForw.Click
Try
dgvDevices.ClearSelection()
Dim currentr As Integer = dgvDevices.CurrentCell.RowIndex
dgvDevices.CurrentCell = dgvDevices.Rows(currentr + 1).Cells(0)
dgvDevices.Rows(currentr + 1).Selected = True
Catch ex As Exception
dgvDevices.CurrentCell = dgvDevices.Rows(dgvDevices.RowCount - 1).Cells(0)
dgvDevices.Rows(dgvDevices.RowCount - 1).Selected = True
End Try
End Sub
Besides Javiers correct answer, if you're using BindingSource for your datagridview then it will be better to change selected item from binding source rather than using datagridview.CurrentCell:
' Example Definitions
Dim bsExample As New BindingSource
Dim dgv As New DataGridView
dgv.DataSource = bsExample
' Example code to change current row position
Dim desiredIndex As Integer = 10
bsExample.Position = desiredIndex
Private Sub DGW2_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DGW2.DataBindingComplete
Dim mygrid As DataGridView
mygrid = CType(sender, DataGridView)
mygrid.ClearSelection()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If DataGridView1.Rows.Count - 1 > 0 Then
For i As Integer = 0 To DataGridView1.Rows.Count - 1 Step +1
If DataGridView1.Rows.Count - 1 > 0 Then
DataGridView1.Rows.RemoveAt(0)
End If
Next
Else
End If
End Sub
Easy with Extensions
Imports System.Runtime.CompilerServices
Module Extensions
''' <summary>
''' Select the Row(Index)
''' </summary>
''' <param name="Dgv">The DataGridView</param>
''' <param name="Index">The Row Index (0 First)</param>
''' <param name="ScroolToTop">If True scrool the row to top</param>
''' <returns>True if no error or False if something went wrong</returns>
' USE: if not DataGridView1.GoToRow(30) then MsgBox("Something went wrong!)
<Extension()>
Public Function GoToRow(Dgv As DataGridView, Index As Integer, Optional ScroolToTop As Boolean = True) As Boolean
If Index > -1 And Index < Dgv.Rows.Count - 1 Then
With Dgv
.ClearSelection()
.Rows(Index).Selected = True
If ScroolToTop Then .FirstDisplayedScrollingRowIndex = Index
End With
Return True
End If
Return False
End Function
End Module
I have DataGridView like that :
| ID | Name |
|:---|:-----:|
| 1 | Samir |
I search by 'Name' using this code :
Private Sub GunaButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GunaButton6.Click
Dim temp As Integer = 0
For i As Integer = 0 To Me.Listofclient.RowCount - 2
If Me.Listofclient.Rows(i).Cells(1).Value.ToString = EDSearch.Text Then
Me.Listofclient.ClearSelection()
Me.Listofclient.Rows(i).Selected = True
temp = 1
End If
Next
If temp = 0 Then
MsgBox("Item not found")
End If
End Sub