Datagridview Check Box Error Default - vb.net

I have a Datagridview in which a user will input data for shifts they have worked. I have a Checkbox to tick whether they require an on call payment for the shift. It's unchecked Value is "N" and when checked the value is "Y". This works well.
However when the user leaves the check box at it's default state from load, when i come to send it to the database i get an exception. How can I get the default value to always be "N" even if the user hasn't altered the box?
Error Message: Object Reference Not set to an instance of an object.

You need declare the checkbox in the datagrid item_databound
Private Sub dataGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dataGrid.ItemDataBound
If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
dim checkbox as checkbox = e.item.findControl("yourCheckboxID")
end if
end sub

Related

Validating DataGridView Cells

I have a datagridview on my form currently. The idea that I'm trying to do is to catch a possible double entry issue when a user edits a cell in the datagridview. I'm supposed to check the field for a duplicate value (which CheckContestantList already does by looping through each row and a possible match). On a match, the previous value is supposed to be restored and a messagebox should pop up and warn the user while resetting that field to edit mode.
However, when a user presses the Enter key... the current row indicator moves down by one... and throws everything off. I've already done a bit of reading on the matter but I can't seem to keep the current cell active even if someone presses Enter.
How do I allow for the Enter key to still confirm the enter press but not move down by one row?
Private Sub dgvContestantPool_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles dgvContestantPool.CellEndEdit
' On the editing of the field, check to see if the value already exists.
If CheckContestantList(dgvContestantPool.CurrentCell.Value) = True Then
' Reset the value to the original value...
dgvContestantPool.ClearSelection()
dgvContestantPool.CurrentCell = dgvContestantPool.Rows(intCurrentRow).Cells("ContestantName")
dgvContestantPool.Rows(intCurrentRow).Selected = True
dgvContestantPool.CurrentCell.Value = strOldValue
dgvContestantPool.BeginEdit(True)
' Alert the user to try again...
MessageBox.Show("Entered value already exists in the list, try again or delete the entry.", "INVALID DATA - Already Exists", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
' Go ahead and let the edit persists.
End If
End Sub
Private Sub dgvContestantPool_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgvContestantPool.CellBeginEdit
strOldValue = dgvContestantPool.CurrentCell.Value
intCurrentRow = dgvContestantPool.CurrentRow.Index
End Sub
You are using the wrong datagridview event, if you're gonna validate an input, you can use the _CellValidating event.
Here's an example:
Private Sub dgvContestantPool_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles dgvContestantPool.CellValidating
If CheckContestantList(dgvContestantPool.Rows(e.RowIndex).Cells(e.ColumnIndex).EditedFormattedValue) = True Then
'Your code goes here
End If
...
Now, for example the cell value is invalid, you can call the
e.Cancel = True in the cell_validating event so that the user cannot go to other cells unless the cell value is valid or press the ESC key. This will exit the cell and return to its previous/original value.

Datagridview won't change checkbox of first row (vb.net)

I have a datagridview (here called cameraTable). One column of the datagridview is purely checkboxes. I have a button that when clicked will check all the boxes in the column, aka select all. Here's the code:
Private Sub btnSelectAll_Click(sender As Object, e As EventArgs) Handles btnSelectAll.Click
For Each row As DataGridViewRow In cameraTable.Rows
DirectCast(row.Cells("CheckBox"), DataGridViewCheckBoxCell).Value = _
True
Next
End Sub
The problem I'm having is that the initial box (i.e. row zero) never changes to checked. I tried these:
DirectCast(cameraTable(0, 0), DataGridViewCheckBoxCell).Value = True ' doesn't work
cameraTable.Rows(0).Cells(0).Value = True ' doesn't work
Neither change the box to checked, but the other rows within the column change without a problem. I even did a msgbox that prints out the value, and turns out to be "True". So the value of the checkbox has changed, but the check box in the datagridview is still unchecked. Any ideas?
Thanks for all the help!
-K
The datagrid will not get INotifyPropertyChanged events (or will not process them) for the row/cell that is currently selected. If you have other columns that can be edited, move the focused column to it.
If all else fails, reload the grid after seting the values by setting the datasource = nothing and then back to what it was.
Steve is right. What I did was when the check box is clicked, I set the focus to another cell on that row (my dgv is set to FullRowSelect):
Private Sub dgv_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellContentClick
If e.ColumnIndex = iCBColIDX Then
dgv.CurrentCell = dgv.Rows(e.RowIndex).Cells("sts")
End If
End Sub
Make your checkbox column width=20 instead of 18.
No, I don't know why, but it helps.
Thanks this man.
PS. It isn't a joke, it's a bug.

Value of global variable won't change

I have a variable whose scope needs to be global, because it needs to be called in a function as well as in a button press. So I declared the variable in a Module so it would be global.
The problem is that the value of this variable needs to be equal to the value of the text property of a textbox in the form.
Here you can download the VB.net demonstration of my problem: http://db.tt/DDxQJDXl
Below is an explanation of what happens
You enter a string into the textbox, in this case I entered "Hello". Then you click the button and it displays what you wrote.
You click OK in that message box and change the value in the textbox. In this case I changed it to "Goodbye". Then I hit the button again, but the variable did not change values and the messagebox displays "Hello" again.
Here is the entire source code:
Module Module1
Public strDataValue = frmTest.txtDataValue.Text
End Module
Public Class frmTest
Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
MsgBox(strDataValue)
End Sub
End Class
Note: This is just a demonstration of a problem I'm having in a much larger program so the variable does have to be global.
You need to set the value of the field to the new value in the TextBox:
Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click
strDataValue = txtDataValue.Text
MsgBox(strDataValue)
End Sub
The field will not change values by itself.
The value of strDataValue won't automatically change when txtDataValue.Text changes. You need to update strDataValue manually, either when the textbox loses focus, or when you click the Test button.
You could also have a public property, which automatically returns the actual value as long as the form is open.
Public ReadOnly Property DataValue() As String
Get
Return frmTest.txtDataValue.Text
End Get
End Property
You need to assign value of txtDataValue.Text to your variable strDataValue in better performance
Using of Timer object or TextChenged event not recommended
Any change of txtDataValue.Text can assign after end of editing action
for me best solution: Leave or LostFocus events
Private Sub txtDataValue_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtDataValue.Leave
strDataValue = txtDataValue.Text
End Sub

Issue detecting checkbox state in DataGridView

I have a DataGridView control in a .Net application that contains a checkbox column. I would like for the user to be able to edit the checkboxes. The issue that I am running into is that I cannot detect the state of the checkbox after the user checks it.
If the checkbox was originally checked, then it will return checked as soon as the DataGridViewCheckBoxCell gets focus. But, if I click on the checkbox again and unchecks it, then it still returns checked. From that point on, it will always return checked regardless of the actual state of the checkbox until it looses focus and gains it again.
Likewise, if the checkbox was originally unchecked, then when it gets focus it will return unchecked in the click event regardless of what the state of the checkbox actually is.
Here is my code.
Private Sub grdTemplates_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdTemplates.CellContentClick
Dim strValue As String = ""
Try
If Me.grdTemplates.Columns(e.ColumnIndex).Name = "colCurrentTemplate" Then
'The user clicked on the checkbox column
strValue = Me.grdTemplates.Item(e.ColumnIndex, e.RowIndex).Value
'THIS VALUE NEVER CHANGES WHILE THE DataGridViewCheckBoxCell HAS FOCUS
Me.lblTemplates.Text = strValue
End If
Catch ex As Exception
HandleError(ex.ToString)
End Try
End Sub
Thanks in advance,
Mike
Include this in your code:
Sub dataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dataGridView1.CurrentCellDirtyStateChanged
If dataGridView1.IsCurrentCellDirty Then
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx

WinForm CheckListBox problem with ItemCheck event

When the checked state of a check box change, I would like to know what the new value is.
his is what I am doing:
Friend WithEvents clstTask As System.Windows.Forms.CheckedListBox
Private Sub clstTask_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles clstTask.ItemCheck
Dim SelectedCheckState As CheckState = clstTask.GetItemCheckState(clstTask.SelectedIndex)
End Sub
However, the value of the SelectedCheckState variable is not accuratley reflecting the new state. I think it is showing the current state before the click, as if this were a "Before_CheckChanged" event handler.
The Check box is a 3 state check box, (Checked, UnChecked, Undetermined). Do I have to write ugly code that assumes that if the CheckState returned is state "X" that the CURRENT state must be Y?
The ItemCheckEventArgs exposes properties CurrentValue and NewValue.
Check out ItemCheckEventArgs.NewValue and ItemCheckEventArgs.CurrentValue. This is why that parameter is there ;)