Load a DataGridView with 100 empty rows with Row number at loading in vb.net - vb.net

I want to show/load 100 empty rows with row number in datagridview of vb.net at run time, so that user can enter the data. If user fill all the 100 rows then datagridview can create new rows as it do usually. I found this so many time but not find any solution how to load empty rows at run time.

'dgv is your DataGridView object.
dgv.Rows.Add(100)
Dim I As Integer = 1
For Each Row As DataGridViewRow In dgv.Rows
Row.Cells(0).Value = I.ToString()
I += 1
Next

Related

Compare DataTable and DataGridView and hightlight matching rows in DataGridView

I have a database table which contains a list of usernames and their current active location, an integer called SEQUENCE.
I get this list of sequence numbers into a datatable with a single column (e.g. "SELECT SEQUENCE FROM TABLE"):
dtUsers = CLS_USERS.GetUsers(User)
It excludes the current user, which is why I parse in User.
What I then do is loop through the datatable and for each number I want to set the matching row in a datagridview (which also has a SEQUENCE column) to a different colour. This is my current code:
For Each row As DataRow In dtUsers.Rows
intSeq = row("SEQUENCE")
For Each dgv_row As DataGridViewRow In dgvCandList.Rows
If dgv_row.Cells("CURRENT_SQ").Value = intSeq Then
dgv_row.DefaultCellStyle.BackColor = Color.Cyan
Else
dgv_row.DefaultCellStyle.BackColor = Color.Grey
End If
Next
Next
However, all rows are highlighted rather than just those where dgv_row.Cells("SV_CURRENT_CAND_SQ").Value = intSeq is True... I've verified that this loop is working correctly, so it must be dgv_row.DefaultCellStyle.BackColor = Color.Cyan which is incorrect?
If so, how should I correctly be assigning the row back colour of specific DataGridRow?
EDIT: This code works correctly, my issue was related to bug outside this loop causing the BackColor to be sent for everything, and then never setback to default if where BackColor wasn't being set back to the default if it wasn't in dtUsers.

Not getting all rows when iterating through DataGridView rows

In a VB.NET WinForms application I have a DataGridView with a checkbox as a non-bound column in the first column. I need to add to a collection of rows each row that has its checkbox checked.
I'm using the below code to iterate through the rows and find the ones with a checked checkbox:
For Each row As DataGridViewRow In dgvEmployees.Rows
Dim chkCell As DataGridViewCheckBoxCell = DirectCast(row.Cells(0), DataGridViewCheckBoxCell)
If Convert.ToBoolean(chkCell.Value) = True Then
Console.WriteLine("This should be the Emp No of the checked row: " & row.Cells(1).Value.ToString())
End If
Next
But it is missing the last row with a checked checkbox. I.e. If I check three checkboxes, in the console output I'm seeing the "Emp No" of the first two checked rows.
Thinking it was acting like an issue with zero indexing, I also tried doing an iteration that used a counter:
For rowNum As Integer = 0 To dgvEmployees.Rows.Count - 1
Dim currentRow As DataGridViewRow = dgvEmployees.Rows(rowNum)
Dim chkCell As DataGridViewCheckBoxCell = DirectCast(currentRow.Cells(0), DataGridViewCheckBoxCell)
If Convert.ToBoolean(chkCell.Value) = True Then
Console.WriteLine("This should be the emp no of the checked row: " & currentRow.Cells(1).Value.ToString())
End If
Next
But I get the same behavior with that code. I tried changing around the Integer = 0 and Rows.Count - 1, etc but that didn't help either.
In case it matters, the DataGridView's SelectionMode needs to be set to CellSelect.
UPDATE
There are 694 records in the table that the datagridview is being populated by.
I added a console output to get the rows.count value:
Console.WriteLine("Num rows: " & dgvEmployees.Rows.Count)
and got 695, which makes sense due to the last row in the datagridview is there to allow entry of a new row. (But I would have thought that the Count - 1 in the second iteration method would account for that.)
I also added
Console.WriteLine("Row index: " & chkcell.RowIndex)
right before the If check for a checked checkbox and, with the first, third and fifth rows checked (indexes 0, 2, 4), here's what was output:
Num rows: 695
Row index: 0
this should be the emp no of the checked row: ACS175
Row index: 1
Row index: 2
this should be the emp no of the checked row: AJAW03
Row index: 3
Row index: 4
Row index: 5
Row index: 6
There should have been a "this should be..." output after the Row index: 4 line.
It appears that the last check box that you check is not being committed by the DataGridView because you aren't moving the focus to another cell after checking it. Try this before running your iteration code:
If dgvEmployees.IsCurrentCellDirty Then
dgvEmployees.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If

For Loop to count checked items on datagrid not counting correctly

I worked out the following loop to count and display how many rows on my datagrid are checked. However, the loop is ignoring my first checked row. The count does not start at 1 until I have checked the second row. The same happens when I uncheck, the values are off by one.
Dim chkRowCount As Integer = 0
For Each row As DataGridViewRow In dgvAssignGridView.Rows
If row.Cells(6).Value = True Then
chkRowCount += 1
Else
chkRowCount += 0
End If
Next
lblChkCount.Text = chkRowCount.ToString
I have tried setting the variable to 1 instead of 0, but that had some unwanted results.
I am guessing you have this code in CellContentClick. The problem is that the code in that routine fires before the value of the checkbox is actually changed. However, you can basically force the DataGridView to verify itself first by putting the following line right before your code.
dgvAssignGridView.EndEdit()
That forces the cell click to register before you do your count.

How to search in and edit Table by using DataGridView

I have a SQL database with a table "Employees" in it (with large number of rows). By using DataGridView, I want to search for specific "Employee's Name" and change it's "Job". How can I achieve that. I'm using VB.net. Please Help Me.
Not sure if this will help but write a loop that goes through all the values if it finds a match its true if not it is false ,if found the item can be displayed in a textbox and edited
if not a message is displayed saying "no match found"
the editing part can be done using a procedure that will update the value in your grid with what is entered
i can supply code for this if need be but i am unsure if this is what you wish
and there is most likely a better way of doing it
you can loop through your grid, and check if the data you wish to edit exists using the For loop:
Supposing you are using a textbox as the input, and you use a label to hold the employee ID:
Dim EmpIDColumn as Integer = 'array number of your EmpID Column
Dim EmpNameColumn as Integer = 'The array number of the column where your EmpName is
Dim JobColumn as Integer = 'The array number of your job column
For each dr as Datagridviewrow in Datagridview1.Rows
If dr.cells(EmpNameColumn).value = TxtSearchBox.text Then
txtEmpJob.text = dr.cells(JobColumn).value
lblEmpID.Text = dr.cells(EmpIDColumn).value
End if
Next
Okay, so you've searched the record successfully. Next step (after editing the job, and even other details like the name) would be to update the record in the grid. Remember you set the lblEmpID's text to empID in the column? Use it to find the
Record you wish to change in the grid using the same technique above!
Dim EmpIDColumn as Integer = 'array number of your EmpID Column
Dim EmpNameColumn as Integer = 'The array number of the column where your EmpName is
Dim JobColumn as Integer = 'The array number of your job column
For each dr as Datagridviewrow in Datagridview1.Rows
If dr.cells(EmpIDColumn).value = lblEmpID.text Then
dr.cells(JobColumn).value = txtEmpJob.text
dr.cells(EmpNameColumn).value = txtEmpName.text
'then, type in here your SQL Query Update!
End if
Next

How to copy rows between a DataGridView bound with a DataTable

I would like to know how can I copy a row in another DataGridView. They are bound with a datatable. I'm trying this but it don't works!
For k As Integer = 0 To gridA.SelectedRows.Count
Dim row As DataRow = dtA.Rows(gridA.SelectedRows(k).Index)
dtB.ImportRow(row)
Next
Then delete rows from gridA:
For k As Integer = 0 To gridA.SelectedRows.Count
dtA.Rows.RemoveAt(gridA.SelectedRows(k).Index)
Next
Using the index of the data grid can be a problem since the rows might be sorted in a different order than the DataTable.
I re-worked your example to use the actual row objects to move and delete:
Dim rows As DataGridViewSelectedRowCollection = gridA.SelectedRows
For Each row As DataGridViewRow In rows
dtB.ImportRow(DirectCast(row.DataBoundItem, DataRowView).Row)
Next
For Each row As DataGridViewRow In rows
dtA.Rows.Remove(DirectCast(row.DataBoundItem, DataRowView).Row)
Next