Control marking of a checkbox in datagridview. vbnet 2015 - checkbox

Best regard. I'm trying to do a validation before marking a check box in a cell of a datagridview. If a condition is met, the check must be marked, otherwise not.
The event in the grid that I am validating is CellContentClick and the code is this
Private Sub dgvDetalle_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvDetalle.CellContentClick
If dgvDetalle.RowCount > 0 Then
Dim DifMinutes As Double = 0
DifMinutes = DateDiff("n", CDate(dgvDetal(20, e.RowIndex).Value), Date.Now)
If DifMinutes > 60 Then
If dgvDetal.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = True Then
TotalSP = TotalSP + 1
Else
TotalSP = TotalSP - 1
End If
else
'Here should go the code so that the check of the datagridview is not marked
End If
End If
End Sub
The language I use is VB.NET from VS 2015
Thank you very much for your help.
The event in the grid that I am validating is CellContentClick but the check of the datagridview is always selected. I have not found the instruction to cancel the event.

I solved adding the following instructions
dgvDetal.EndEdit()
dgvDetal.CurrentRow.Cells(23).Value = 0
dgvDetal.RefreshEdit()

Related

Connecting and Updating to Database through VB.NET "Select command property has not been ... "

I am using this guide to update a value in a database through VB.NET and really struggling to get it to work.
I am trying to add 1 to a particular cell in the database. The grid that I am using updates with the value, but the database itself doesn't change. Using different tutorials (and loads of other StackOverflow questions) I have changed the code from the original but nothing seems to work.
Each time I click the button that should trigger the addition, I get this error message.
"The select command property has not been initialized before calling fill"
If anyone could tell me what is going wrong or point me in the direction that I should go in, it would be much appreciated. Thanks.
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim senderGrid = DirectCast(sender, DataGridView)
Dim OleDbDataAdapter As New OleDb.OleDbDataAdapter
If TypeOf senderGrid.Columns(e.ColumnIndex) Is DataGridViewButtonColumn AndAlso
e.RowIndex >= 0 Then
' If addition
If e.ColumnIndex = 3 Then
Try
'from microsoft tut
ProjectDatabaseDataSet.Tables(0).Rows(e.RowIndex).Item("ProductStock") = ProjectDatabaseDataSet.Tables(0).Rows(e.RowIndex).Item("ProductStock") + 1
OleDbDataAdapter.Fill(ProjectDatabaseDataSet.Product)
OleDbDataAdapter.Update(ProjectDatabaseDataSet.Product)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If

Changing control (that is in an array) from a timer: Not working

I'm trying to make a picture's visibility toggle on a timer. The picture I am trying to change is underscore(i). Here is that code:
Dim DigitSelected As Integer = 1
Public Underscores(3) As PictureBox
....
Private Sub CursorTimer_Tick(sender As System.Object, e As System.EventArgs) Handles CursorTimer.Tick
Me.Underscores(DigitSelected).Visible = Not (Me.Underscores(DigitSelected).Visible)
End Sub
This code above was previously working, but recently I moved where I was creating the pictureboxes and their stuff to a module on another page:
With Initials
For i As Byte = 1 To 3
.Underscores(i) = New PictureBox
With .Underscores(i)
.Height = 60
.Width = 144
.ImageLocation = "Underscore.png"
.BackColor = Color.Transparent
End With
.Controls.Add(.Underscores(i))
Next
end with
Now when I use the top-most snippet of code, it throws no errors, and changes nothing. I'm pretty sure I'm just missing something small.
Any ideas? Thanks!
I don't exactly know what you are trying to achieve here, but here is what happens :
Everytime you run that loop, you create three transparent PictureBox that you add to your Main Form. You don't remove anything, nor hide anything !
If you want to toggle visibility, that is no way of doing so !
The best would be for you to know the PictureBoxes names, so you can do :
Dim pb1 = CType(Initials.FindControl("MyPictureBox", true), PictureBox)
If Not IsNothing(pb1) Then
pb1.Visible = Not pb1.Visible
End If

Update a data bound list box

I have a section of code that fires when a check box is checked/unchecked.
The check box is going to be used to narrow down a listbox that is data bound. One item in the data is a link to an png image which shows correctly when the application first initializes.
In the code below, I get a "Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception" exception every time.
Private Sub chkShowCurrent_Click(sender As Object, e As RoutedEventArgs) Handles chkShowCurrent.Click
Dim o As ObjectDataProvider
Dim xDoc = XDocument.Load(sFileName)
If chkShowCurrent.IsChecked = False Then
'MsgBox("false")
End If
If chkShowCurrent.IsChecked = True Then
'MsgBox("true")
Dim OnlyCurrent = From job In xDoc.Root.Descendants("Job")
Where job.Element("Status").Value = "a-current"
Order By job.Element("Status").Value, job.Element("Rush").Value, Convert.ToDateTime(job.Element("DateOut").Value)
Select job
lstJobs.ItemsSource = OnlyCurrent
End If
lstJobs.Items.Refresh()
o = FindResource("jobs")
o.Refresh()
End Sub
I had originally thought that the lstjobs.items.refresh was the issue, but the exception doesn't popup until after End Sub has been reached. I have tried adding a breakpoint to the code at the checkbox code and step through the code, but the answer still eludes me. Can anyone tell me where my error is?
**** Edit for more info below ****
Public Property Artwork() As String
Get
Return _Artwork
End Get
Set(ByVal value As String)
If value = String.Empty Then value = "images\Untitled.png"
If My.Computer.FileSystem.FileExists("\\ARTSTATION\Users\Public\XML Job Board\" & value) Then
value = "\\ARTSTATION\Users\Public\XML Job Board\" & value
Else
value = "\\ARTSTATION\Users\Public\XML Job Board\images\Untitled.png"
End If
_Artwork = value
End Set
The xml element is formated as image\GUID.png, thus necessitating the need to add the full path at runtime.
I had originally explored this as the issue and changed the XML to contain the full path and commented out the need for adding the full path in the properties, but this yielded the same results.

Check state of checkboxes in an array - runtime error

I want to run one block of code when any of the 197 checkboxes on the form are clicked. I have all of the checkboxes in an array which I used to check the availability of each seat on the forms load (this program is a seat booking system). Each seat is a checkbox displayed as a button, and I would like the "Seats selected" label to show the seat selected once it has been checked. I used the same array in the code below to check whether they were checked, and if they were the label would be updated. The code below is giving me an error described as "Object reference not set to an instance of an object" when I click one of the checkboxes. I'm not sure why this is and I can't really find a solution. I am only coding the first three checkboxes at the moment, just to see if it works. Ideally I would like to list all 197 checkboxes as handles to this procedure, so when any are clicked every seats checkstate will be checked.
If anyone has a solution that would be great! Thanks.
Public Class frmSeatPlan
Dim seat(11, 20) As Control
Private Sub A1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.CheckedChanged, A2.CheckedChanged, A3.CheckedChanged
seat(1, 1) = A1
seat(1, 2) = A2
seat(1, 3) = A3
For y = 1 To 1
For x = 1 To 3
Dim seat(y, x) As CheckBox
If seat(y, x).Checked = True Then
selectedSeats = selectedSeats & seat(y, x).Name & ", "
MsgBox(selectedSeats)
lblSelected.Text = selectedSeats
End If
Next x
Next y
End Sub
Are you going to write handles clause 197 times? Can you be sure by the time you finished you did not make a mistake? What if you add a seat #198 between #156 and #157 - how would you go around it? You are gradually planning a maintenance nightmare for yourself. Instead, you should bind events in code, where you would also create those checkboxes. This way you will never have the problem you were getting.
For y = 1 to 3
For x = 1 to 3
Dim chk As New CheckBox
'set positioning on the screen
chk.X = x
chk.Y = y
AddHandler chk.CheckedChanged, AddressOf Any_CheckedChanged
Next
Next
Ideally, you will not need your array as well, i.e. seat(y, x), because every seat can be assigned X and Y when they are created and pass those as event arguments (so you probably will need a custom event).

Infragistics UltraWinGrid EmptyDataText Equivalent?

We're using Infragistics UltraWinGrid as a base class for customized controls. One of the projects that will use this control to display search results has a requirement to display a user friendly message when no matches are located.
We'd like to encapsulate that functionality into the derived control - so no customization beyond setting the message to display is required by the programmer who uses the control. This would have to be done in generic fashion - one size fits all datasets.
Is there allowance in the UltraWinGrid for this type of usage already? If so, where would I find it hidden. :-)
If this functionality needs to be coded, I can think of an algorithm which would add a blank record to whatever recordset was set and place that into the grid. In your opinion, is this the best way to handle the solution?
I don't know if this will help, but here's to finishing up the thread. I didn't find a built in way, so I solved this problem as follows: In my class which inherits UltraGrid
Public Class MyGridPlain
Inherits Infragistics.Win.UltraWinGrid.UltraGrid
I added two properties, one to specify what the developer wants to say in the empty data case, and another to enable the developer to place their message where they want it
Private mEmptyDataText As String = String.Empty
Private mEmptyDataTextLocation As Point = New Point(30, 30)Public Shadows Property EmptyDataTextLocation() As Point
Get
Return mEmptyDataTextLocation
End Get
Set(ByVal value As Point)
mEmptyDataTextLocation = value
setEmptyMessageIfRequired()
End Set
End Property
Public Shadows Property EmptyDataText() As String
Get
Return mEmptyDataText
End Get
Set(ByVal value As String)
mEmptyDataText = value
setEmptyMessageIfRequired()
End Set
End Property
I added a method which will check for empty data and set the message if so. And another method which will remove the existing empty message.
Private Sub setEmptyMessageIfRequired()
removeExistingEmptyData()
'if there are no rows, and if there is an EmptyDataText message, display it now.
If EmptyDataText.Length > 0 AndAlso Rows.Count = 0 Then
Dim lbl As Label = New Label(EmptyDataText)
lbl.Name = "EmptyDataLabel"
lbl.Size = New Size(Width, 25)
lbl.Location = EmptyDataTextLocation
ControlUIElement.Control.Controls.Add(lbl)
End If
End SubPrivate Sub removeExistingEmptyData()
'any previous empty data messages?
Dim lblempty() As Control = Controls.Find("EmptyDataLabel", True)
If lblempty.Length > 0 Then
Controls.Remove(lblempty(0))
End If
End Sub
Last - I added a check for empty data to the grid's InitializeLayout event.
Private Sub grid_InitializeLayout(ByVal sender As Object, _
ByVal e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) _
Handles MyBase.InitializeLayout
setEmptyMessageIfRequired()
End Sub

Resources