How to retrieve the checked status of a cell of DataGridViewChechboxColumn type? - vb.net

I have datagridview and one of the columns is a DataGridViewCheckboxColumn.
I would like to trigger an action when the user check or uncheck the box.
I use the CellContentClick event of the datagrid but it return me the value before and not the current check status.
below is my code:
Private Sub MyDataGrid_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles MyDataGrid.CellContentClick
Dim headerText As String = MyDataGrid.Columns(e.ColumnIndex).HeaderText
If Not headerText.Equals("Signature") Then Return
Dim Status As Boolean
Status = NotesDataGridView.CurrentRow.Cells("Signature").Value
MsgBox(Status)
The value in Status is not the checked status after the click but the status before.
I use the CellContentClick event of the datagrid but it return me the value before and not the current check status.
below is my code:
Private Sub MyDataGrid_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles MyDataGrid.CellContentClick
Dim headerText As String = MyDataGrid.Columns(e.ColumnIndex).HeaderText
If Not headerText.Equals("Signature") Then Return
Dim Status As Boolean
Status = NotesDataGridView.CurrentRow.Cells("Signature").Value
MsgBox(Status)
The value in Status is not the checked status after the click but the status before.
I am expecting the check status after the click.

Related

index statement code | ms-access and vb.net

i have problem with my code (ms-access and vb.net) :
i want to use this code :
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Try
Dim indx As Integer = Convert.ToInt32(DataGridView1(1,e.RowIndex).Value)
in this case :
Private Sub FlatButton4_Click(sender As Object, e As EventArgs) Handles FlatButton4.Click
Try
Dim indx As Integer = Convert.ToInt32(DataGridView1(1,e.RowIndex).Value)
but i have a error in : [e.RowIndex]
how to resolve it ?
As you can see, in the Click event handler of your button, e is type EventArgs while in the CellClick event handler of the DataGridView it is type DataGridViewCellEventArgs. It makes sense that RowIndex would be a member of that second type, which is specific to a DataGridView, while not of the first type, which knows nothing about the grid.
If what you want is to access a cell in the currently selected row then use this:
Dim indx As Integer = Convert.ToInt32(DataGridView1.CurrentRow.Cells(1).Value)

VB.net Windows form (Working with Access Database) displaying a selected combobox item in a textbox

Hello All this is somewhat urgent as this assignment is due Sunday 11/12 at midnight
I have attached what I need help with it is selecting a combobox item then getting the data to display in the textboxes below
I really don't know how to approach it I double clicked the combobox and this is what I started with to try to just get the ID to display. I haven't attempted anything else. I commented out what I tried because it didn't work.
Public Class AppointmentsForm
Private aAppointments As New Appointments
'Instance of customers
Private aCustomers As New Customers
Private Sub AppointmentsForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Combobox must always have a DataSource, Display Member, and Value Member
'Load the ComboBox with customer name
cboCustomer.DataSource = aCustomers.Items
cboCustomer.DisplayMember = "CustName"
cboCustomer.ValueMember = "CustId"
cboCustomer.SelectedIndex = -1 'no items selected
' load the datagridview
ApptDataGridView.DataSource = aAppointments.Items
'do not show TypeID
ApptDataGridView.Columns(1).Visible = False
'.Columns(1) TypeID has index of 1 as it is Column 2 in the data sources
End Sub
Private Sub btnDelete_Click(sender As System.Object, e As System.EventArgs) Handles btnDelete.Click
'make sure that a record is selected first
If ApptDataGridView.SelectedRows.Count > 0 Then
Dim apptid As Integer
apptid = ApptDataGridView.SelectedRows(0).Cells(0).Value
'Delete selected record by calling the delte function
aAppointments.Delete(apptid)
End If
End Sub
Private Sub btnEdit_Click(sender As System.Object, e As System.EventArgs) Handles btnEdit.Click
'make sure row is selected get that row from the table ..... need to find by ID
'This is a query which you will create in the class
'Transfer information from that row to the form, display the form
If ApptDataGridView.SelectedRows.Count > 0 Then
modAppointmentsForm.ApptID = ApptDataGridView.SelectedRows(0).Cells(0).Value
modAppointmentsForm.ShowDialog()
End If
End Sub
End Class
HERE IS THE CLASS FOR APPOINTMENTS:
Public Class Appointments
Public adapter As New CompanyDataSetTableAdapters.SalesStaffTableAdapter
'error variable
Public Shared Property LastError As String
Public ReadOnly Property Items() As DataTable
Get
Dim table As DataTable = adapter.GetData
'sorted by Appointment id
table.DefaultView.Sort = "ID"
Return table
End Get
End Property
'create a function to combine date and time
Public Shared Function CombinedDateTime(aDate As DateTime, aTime As DateTime) As DateTime
'declare timespan variable
Dim tsDate As New TimeSpan(aTime.Hour, aTime.Minute, 0)
Return aDate.Add(tsDate)
End Function
HERE IS THE CLASS FOR CUSTOMERS:
Public Class Customers
'create a object variable (tableadapter)
'this will be used to creat instances of this class in the form
Private adapter As New CompanyDataSetTableAdapters.SalesStaffTableAdapter
'property to return a table using the GetData method
Public ReadOnly Property Items() As DataTable
Get
Dim table As DataTable = adapter.GetData
table.DefaultView.Sort = "First_Name"
Return table
End Get
End Property
'create a function to filter the customers by custid
Public Function GetByCustomerId(custid As Short) As DataTable
Dim table As DataTable = adapter.GetData 'entire table
'filter to get desired row(s)
table.DefaultView.RowFilter = " ID = " & custid
Return table
End Function
End Class
HERE IS THE CODE FOR THE FORM WITH THE COMBOBOX AND THE TEXTBOX'S SUCH AS ID, LASTNAME, FIRSTNAME, ETC. THAT NEED TO HAVE THE DATA DISPLAYED WHEN THE COMBOBOX ITEM IS SELECTED. AGAIN THE ID IS DISPLAYING WHEN I WRITE: IDTextBox.Text = SalesStaffComboBox.SelectedValue.ToString
BUT IF I DO THE SAME FOR THE OTHER TEXT BOXES (LastName.Text = SalesStaffComboBox.SelectedValue.ToString _____ FirstName.Text = SalesStaffComboBox.SelectedValue.ToString ) THE ID NUMBER IS DISPLAYED IN THEM AS WELL.
Public Class frmUpdateSalary
'instances of the required classes
Private aCustomers As New Customers
Private aAppointments As New Appointments
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Private Sub frmUpdateSalary_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
SalesStaffComboBox.DataSource = aCustomers.Items
SalesStaffComboBox.DisplayMember = "First_Name"
SalesStaffComboBox.ValueMember = "ID"
End Sub
Private Sub SalesStaffComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalesStaffComboBox.SelectedIndexChanged
'fill the textboxes
'Populate ID textbox with selected customer
IDTextBox.Text = SalesStaffComboBox.SelectedValue.ToString
End Sub
To select a value form a combobox, listbox or dropdownlist.
If cmbBox.selectedIndex <> -1 Then
comboIndex = cmbBox.selectedIndex
End Else
cmbBox.Items.removeAt(x)
to remove the item at the selected index
If cmbBox.selectedIndex <> -1 Then
comboItem= cmbBox.SelectedItem()
Without VS10 to check if those are the correct method names or caps But its essentially it
Your combobox has two data variables for each item, the value and the display.
So for each customer you have the name and his ID (You don't specify if it is the last or the first name).
If in its textbox you say:
txtID.Text=cbo.SelectedValue.ToString
txtLastName.Text=cbo.SelectedValue.ToString
txtFirstName.Text=cbo.SelectedValue.ToString
You will get the same result for each textbox, since the left side is always the same.
If in your Combobox the Display Value is a combination of LastName and FirstName you can have:
txtID.Text=cbo.SelectedValue.ToString
txtName.Text=cbo.SelectedText.ToString
I am writing without visual studio so, sorry if I have any minor errors
You don't explain what is aCustomers but I imagine that the whole information for the customers are there. So you can retrieve the data with the id that you already have.
If you give as more info we can help.

Reset DataGridView blank row if user clicks in that row and takes focus off that row

If a user clicks the blank row at the bottom of a DataGridView and moves focus away from the DataGridView, the row click in now is in a state that indicates a change is made to that row.
Is it possible to tell the DataGridView to un-mark this row as being changed?
Is it possible to reset this row when focus is off the DataGridView?
We are using the following event handler to alert the user if the Invoiced On is left blank:
Private Sub dataGridViewPayments_CellValidating(ByVal sender As Object, _
ByVal e As DataGridViewCellValidatingEventArgs) _
Handles DataGridViewPayments.CellValidating
Dim headerText As String = _
DataGridViewPayments.Columns(e.ColumnIndex).HeaderText
' Validate the Invoiced On cell and display the error if it's empty.
'-------------------------------------------------------------------
If (String.IsNullOrEmpty(e.FormattedValue.ToString()) And
headerText.Equals("Invoiced On")) Then
DataGridViewPayments.Rows(e.RowIndex).ErrorText = _
"Please enter an Inoiced On date."
e.Cancel = True
End If
End Sub
Looks like we need a way to stop this from executing if the user simply clicks in the grid then clicks somewhere else in the form.
You could try something like this:
Private Sub dg_CellValidating(ByVal sender As Object, ByVal e As DataGridViewCellValidatingEventArgs) Handles dg.CellValidating
Dim headerText As String = dg.Columns(e.ColumnIndex).HeaderText
'Try this --------------------------------------------------------------
Dim vClicked As Boolean = False
If (Control.MouseButtons And MouseButtons.Left) = MouseButtons.Left Then
vClicked = True
End If
'-----------------------------------------------------------------------
'Validate the Invoiced On cell and display the error if it's empty.
'And put vClicked here
If Not vClicked AndAlso (String.IsNullOrEmpty(e.FormattedValue.ToString()) And headerText.Equals("Invoiced On")) Then
dg.Rows(e.RowIndex).ErrorText = "Please enter an Inoiced On date."
e.Cancel = True
End If
End Sub
Please, let me know if it helped. =)

Double-click DataGridView row?

I am using vb.net and DataGridView on a winform.
When a user double-clicks on a row I want to do something with this row. But how can I know whether user clicked on a row or just anywhere in the grid? If I use DataGridView.CurrentRow then if a row is selected and user clicked anywhere on the grid the current row will show the selected and not where the user clicked (which in this case would be not on a row and I would want to ignore it).
Try the CellMouseDoubleClick event...
Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick
If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
Dim selectedRow = DataGridView1.Rows(e.RowIndex)
End If
End Sub
This will only fire if the user is actually over a cell in the grid. The If check filters out double clicks on the row selectors and headers.
Use Datagridview DoubleClick Evenet and then Datagrdiview1.selectedrows[0].cell["CellName"] to get value and process.
Below example shows clients record upon double click on selected row.
private void dgvClientsUsage_DoubleClick(object sender, EventArgs e)
{
if (dgvClientsUsage.SelectedRows.Count < 1)
{
MessageBox.Show("Please select a client");
return;
}
else
{
string clientName = dgvClientsUsage.SelectedRows[0].Cells["ClientName"].Value.ToString();
// show selected client Details
ClientDetails clients = new ClientDetails(clientName);
clients.ShowDialog();
}
}
Use DataGridView.HitTest in the double-click handler to find out where the click happened.
I would use the DoubleClick event of the DataGridView. This will at least only fire when the user double clicks in the data grid - you can use the MousePosition to determine what row (if any) the user double clicked on.
You could try something like this.
Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
For index As Integer = 0 To DataGridView1.Rows.Count
If DataGridView1.Rows(index).Selected = True Then
'it is selected
Else
'is is not selected
End If
Next
End Sub
Keep in mind i could not test this because i diddent have any data to populate my DataGridView.
You can try this:
Private Sub grdview_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdview.CellDoubleClick
For index As Integer = 0 To grdview.Rows.Count - 1
If e.RowIndex = index AndAlso e.ColumnIndex = 1 AndAlso grdview.Rows(index).Cells(1).Value = "" Then
MsgBox("Double Click Message")
End If
Next
End Sub

GridView1.SelectedRow returning nothing

My problem is that in the below code, the SelectedRow property returns nothing. I manually bind items to GridView at runtime and autogeneratecolumns and autogenerateselectbutton properties are true.
I think the problem is about having a PostBack when the Select command is clicked.
Thanks a lot.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Session("ContactID") = GridView1.SelectedRow.Cells(0).Text()
Response.Redirect("~/ContactAddress.aspx")
End Sub
The row you want is accessible via e (the GridViewCommandEventArgs parameter), specifically the value of e.CommandArgument which will have the row index :
From here :
To determine the index of the row that
raised the event, use the
CommandArgument property of the event
argument that is passed to the event.
The ButtonField class automatically
populates the CommandArgument property
with the appropriate index value. For
other command buttons, you must
manually set the CommandArgument
property of the command button. For
example, you can set the
CommandArgument to <%#
Container.DataItemIndex %> when the
GridView control has no paging
enabled.
The selected row is in the GridViewCommandEventArgs parameter.
Besides adding the CommandArgument, you need to change your code to the following.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim rowNumber As Integer = e.CommandArgument
Dim ContactId As Integer = GridView1.Rows(rowNumber).Cells(0).Text
Session("ContactID") = ContactId
Response.Redirect("~/ContactAddress.aspx")
End Sub