-
Nov 9th, 2016, 02:08 PM
#1
Thread Starter
Lively Member
How do I remove rows in a datagridview using a Checkbox column?
I am looping backwards through my datagridview and removing any rows where the checkbox column is checked. However, it does not see all the checked checkboxes and therefor is not deleteing all the rows that are checked. I cannot for the life of me figure out why! Here is my code:
Code:
For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
If DataGridView1.Rows(i).Cells(0).Value = True Then
DataGridView1.Rows.RemoveAt(i)
Else
End If
Next i
Last edited by Christhemist; Nov 9th, 2016 at 02:12 PM.
-
Nov 9th, 2016, 03:20 PM
#2
Re: How do I remove rows in a datagridview using a Checkbox column?
Add:to the top of your code and fix the identified problems.
-
Nov 9th, 2016, 04:03 PM
#3
Thread Starter
Lively Member
Re: How do I remove rows in a datagridview using a Checkbox column?
I made the change that "Option Strict On" recommended, this did not fix my issue. Here is the change I made:
Code:
DataGridView1.Rows(i).Cells(0).Value Is "True"
-
Nov 9th, 2016, 04:22 PM
#4
Re: How do I remove rows in a datagridview using a Checkbox column?
All data types derive from type System.Object so an Object type can hold any value. This is why the DataGridviewCell.Value property is type Object. Instead of trying to find some object to erroneously compare to the Cell.Value as an Object, try converting the Cell.Value to a the proper type. Hint: it is not a System.String.
-
Nov 9th, 2016, 04:38 PM
#5
Thread Starter
Lively Member
Re: How do I remove rows in a datagridview using a Checkbox column?
I think you mean boolean, if that's the case then I've already tried that and have had the same result:
Code:
For i As Integer = DataGridView1.Rows.Count - 1 To 0 Step -1
Dim c As Boolean
c = DataGridView1.Rows(i).Cells(0).Value
If c = True Then
DataGridView1.Rows.RemoveAt(i)
Else
End If
Next i
-
Nov 9th, 2016, 04:53 PM
#6
Thread Starter
Lively Member
Re: How do I remove rows in a datagridview using a Checkbox column?
Not sure if this matters but it might be worth noting. I create the datagridview checkbox column manually in the datagridview properties, then i import my data after that.
-
Nov 9th, 2016, 09:39 PM
#7
Re: How do I remove rows in a datagridview using a Checkbox column?
Please do not PM me to respond to your posts. I will reply if I have time and feel I have something to offer.
Without knowing how to column is configured (TriState, TrueValue, FalseValue, IntermediateValue, data bound status) it is hard to say what the issue is.
A bit more detailed implementation would be:
VB.Net Code:
' if DataGridView1.NewRowIndex <> - 1 then the NewRow Exists and should be skipped For i As Int32 = DataGridView1.Rows.Count - If(DataGridView1.NewRowIndex = -1, 1, 2) To 0 Step -1 Dim val As Object = DataGridView1.Rows(i).Cells(0).Value Dim isTrue As Boolean = If(TypeOf val Is Boolean, DirectCast(val, Boolean), False) If isTrue Then DataGridView1.Rows.RemoveAt(i) End If Next
, but this is not significantly different than what you claim to have tried.
Also are you certain that the DataGridViewCheckBoxColumn is at index zero?
Have you set a breakpoint and stepped through the code checking the values? If you do not know how to do this, read: Navigating through Code with the Debugger and View data values in Data Tips in the code editor.
-
Nov 9th, 2016, 10:08 PM
#8
Re: How do I remove rows in a datagridview using a Checkbox column?
-
Nov 10th, 2016, 12:50 PM
#9
Thread Starter
Lively Member
Re: How do I remove rows in a datagridview using a Checkbox column?
@TnTinMN I am using your code and I am getting the same results. I have tried creating a break point and stepped through the code.
Once the break cursor gets to a records with the checkbox column checked "Dim val As Object = DataGridView1.Rows(i).Cells(0).Value" Value says its true, but on this line "Dim istrue As Boolean = If(TypeOf val Is Boolean, DirectCast(val, Boolean), False)" Val says its nothing. Furthermore, on this line "If istrue Then" istrue is reading false. I am so confused by this....
I am sure that the DataGridViewCheckBoxColumn is at index zero because I get the same results when I use the actual column name and not the index.
I am unsure on how to tell how the Column is configured (TriState, TrueValue, FalseValue, IntermediateValue, data bound status)
Sorry for the pm, I did not know that was bad forum etiquette and I am desperate to resolve this issue.
-
Nov 10th, 2016, 01:26 PM
#10
Re: How do I remove rows in a datagridview using a Checkbox column?
Originally Posted by Christhemist
Once the break cursor gets to a records with the checkbox column checked "Dim val As Object = DataGridView1.Rows(i).Cells(0).Value" Value says its true, but on this line "Dim istrue As Boolean = If(TypeOf val Is Boolean, DirectCast(val, Boolean), False)" Val says its nothing. Furthermore, on this line "If istrue Then" istrue is reading false.
Well that is strange! How can it be True in one view and not the other?
Try adding this right after the "For i As ..." statement.
Code:
Dim typeName As String = "Nothing"
If DataGridView1.Rows(i).Cells(0).Value IsNot Nothing Then
typeName = DataGridView1.Rows(i).Cells(0).Value.GetType.FullName
End If
Debug.Print(typeName)
and report back on the value of "typeName".
Also what version of Visual Studio are you using?
-
Nov 10th, 2016, 01:34 PM
#11
Re: How do I remove rows in a datagridview using a Checkbox column?
This seems to work for me.
Code:
For i As Int32 = DataGridView1.Rows.Count - If(DataGridView1.NewRowIndex = -1, 1, 2) To 0 Step -1
If CBool(DirectCast(DataGridView1.Rows(i).Cells(0), DataGridViewCheckBoxCell).Value) = True Then
DataGridView1.Rows.RemoveAt(i)
End If
Next
-
Nov 10th, 2016, 01:40 PM
#12
Thread Starter
Lively Member
Re: How do I remove rows in a datagridview using a Checkbox column?
The value of "typeName" is returning "Nothing" until about row 25 where if finally reads "System.Boolean" it should have read that starting around row 130.
I am using Visual Studio Community 2015 Version 14.0.25421.03 Update 3
-
Nov 10th, 2016, 01:57 PM
#13
Re: How do I remove rows in a datagridview using a Checkbox column?
Well I really do not know what the issue could be other than that you are using VS 2015 that has had many bizarre bugs reported.
The only suggestion I can offer at this point is load all values into a DataTable or other appropriate data structure and bind the DGV to that data source.
You show two columns defined in the image from post #9. Are these the only columns displayed or you also setting the DGV.DataSource property?
How are you loading the DGV?
-
Nov 10th, 2016, 01:59 PM
#14
Thread Starter
Lively Member
Re: How do I remove rows in a datagridview using a Checkbox column?
Topshot, I am getting the same results as described before.
-
Nov 10th, 2016, 02:00 PM
#15
Thread Starter
Lively Member
Re: How do I remove rows in a datagridview using a Checkbox column?
There are many other data fields. The two shown above are just premade columns in the DGV properties. Here is how I load my data into the DGV:
Code:
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Work\Data.xlsx;Extended Properties=Excel 12.0;")
Dim sql = "select * from [Sheet1$]"
Using da As New OleDbDataAdapter(sql, MyConnection)
DtSample = New DataTable()
da.FillSchema(DtSample, SchemaType.Source)
da.Fill(DtSample)
End Using
DataGridView1.DataSource = DtSample
-
Nov 10th, 2016, 02:01 PM
#16
Re: How do I remove rows in a datagridview using a Checkbox column?
Originally Posted by Christhemist
The value of "typeName" is returning "Nothing" until about row 25 where if finally reads "System.Boolean" it should have read that starting around row 130.
??? Are you dynamically creating these checkboxes? Sure looked like it was done at design time. So why wouldn't it tell that it is System.Boolean for ALL rows since the Checkboxes are always there? They are either checked (True) or not (False). What is special about this row 130?
ETA: Are the checkboxes built into this Sheet then? But they don't start until row 130 perhaps?
Last edited by topshot; Nov 10th, 2016 at 02:04 PM.
Reason: Asked about Excel
-
Nov 10th, 2016, 02:06 PM
#17
Thread Starter
Lively Member
Re: How do I remove rows in a datagridview using a Checkbox column?
I may have found the reason why it was doing this, it was how I was checking the checkboxes in the first place:
Code:
Private Sub DataGridView1_RowPrePaint(sender As Object,
e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
If (e.RowIndex < 0 OrElse e.RowIndex = DataGridView1.NewRowIndex) Then Return
Dim row = DataGridView1.Rows(e.RowIndex)
If (String.IsNullOrEmpty(Filter)) Then
row.DefaultCellStyle.ForeColor = Color.Black
Else
Dim data = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem,
DataRowView).Row
If data.Field(Of String)(ColName).ToLower() _
.StartsWith(Filter.ToLower()) Then
row.DefaultCellStyle.ForeColor = Color.Red
DataGridView1(0, row.Index).Value = True
Else
row.DefaultCellStyle.ForeColor = Color.Black
End If
End If
End Sub
-
Nov 12th, 2016, 09:10 AM
#18
Re: How do I remove rows in a datagridview using a Checkbox column?
The following loads a DataGridView with a DataTable. The DataTable has a boolean column used for marking row(s) as ready to process and in a real app we would tack on that column prior to loading the data or after the fact and if after the fact we could deal with the ordinal position in several ways.
Button1 shows how to remove check rows by the underlying data source while Button2 shows via checked rows and does not use the underlying DataTable. The code origins are from my MSDN article on DataGridView CheckBoxes so if you need assistance on how the code below works, look at the projects.
Form code (requires DataGridView and two buttons)
Code:
''' <summary>
''' 1. Populate DataGridView with mocked data.
''' 2. Show how to remove rows from DataGridView by
''' a. DataSource
''' b. List(Of DataGridViewRow) where we don't care about the under
''' lying DataSource so it could be a DataTable or other container
''' or rows added manually
''' </summary>
''' <remarks>
''' Language extension methods are utilized to obtain row data which are
''' checked by DataSource or by DataGridView rows
''' </remarks>
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.DataSource = GetMockedData()
End Sub
''' <summary>
''' If data was coming from a database table we would not have the
''' process column from the database table but instead tack on a column
''' to the DataTable as there is no reason for retaining the checkbox
''' column in the database table.
''' </summary>
''' <returns></returns>
Private Function GetMockedData() As DataTable
Dim dt As New DataTable()
dt.Columns.Add("Process", GetType(Boolean))
dt.Columns.Add("Identifier", GetType(Integer))
dt.Columns.Add("LastName", GetType(String))
dt.Columns.Add("Value1", GetType(Integer))
dt.Columns.Add("Value2", GetType(Integer))
dt.Rows.Add(False, 10, "Smith", 10, 12)
dt.Rows.Add(False, 20, "Jones", 20, 22)
dt.Rows.Add(False, 30, "Payne", 1, 1)
dt.Rows.Add(False, 40, "Gallagher", 4, 5)
dt.Rows.Add(False, 50, "Clime", 20, 56)
Return dt
End Function
''' <summary>
''' Remove rows from DataGridView via the DataSource
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Rows As List(Of DataRow) = DataGridView1.GetRowsChecked("Process")
If Rows.Count > 0 Then
DataGridView1.DataTable.RemoveRows(Rows)
End If
End Sub
''' <summary>
''' Remove rows from DataGridView via List(Of DataGridViewRow)
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
DataGridView1.RemoveRows(DataGridView1.GetCheckedRows("Process"))
End Sub
End Class
Language extension methods (name the file Module1.vb)
Code:
Module Module1
''' <summary>
''' Get checked rows from DataTable
''' </summary>
''' <param name="sender"></param>
''' <param name="columnName"></param>
''' <returns></returns>
<DebuggerStepThrough()>
<Runtime.CompilerServices.Extension()>
Public Function GetRowsChecked(ByVal sender As DataTable, ByVal columnName As String) As List(Of DataRow)
Return sender.AsEnumerable.Where(Function(row) row.Field(Of Boolean)(columnName)).ToList
End Function
''' <summary>
''' Get checked rows from DataGridView where the DataSource is a DataTable
''' </summary>
''' <param name="sender"></param>
''' <param name="columnName"></param>
''' <returns></returns>
<DebuggerStepThrough()>
<Runtime.CompilerServices.Extension()>
Public Function GetRowsChecked(ByVal sender As DataGridView, ByVal columnName As String) As List(Of DataRow)
Return sender.DataTable.AsEnumerable.Where(Function(row) row.Field(Of Boolean)(columnName)).ToList
End Function
''' <summary>
''' Remove rows from DataGridView's DataSource which is a DataTable
''' </summary>
''' <param name="dt"></param>
''' <param name="Rows"></param>
<DebuggerStepThrough()>
<Runtime.CompilerServices.Extension()>
Public Sub RemoveRows(ByVal dt As DataTable, ByVal Rows As List(Of DataRow))
For iIndex As Integer = Rows.Count - 1 To 0 Step -1
dt.Rows.Remove(Rows(iIndex))
Next
End Sub
''' <summary>
''' Cast DataGridView DataSource to DataTable
''' </summary>
''' <param name="sender"></param>
''' <returns></returns>
''' <remarks>
''' An exception is thrown if the data source is not a DataTable
''' </remarks>
<DebuggerStepThrough()>
<Runtime.CompilerServices.Extension()>
Public Function DataTable(ByVal sender As DataGridView) As DataTable
Return CType(sender.DataSource, DataTable)
End Function
''' <summary>
''' Get checked rows from DataGridView
''' </summary>
''' <param name="GridView"></param>
''' <param name="ColumnName"></param>
''' <returns></returns>
<DebuggerStepThrough()>
<Runtime.CompilerServices.Extension()>
Public Function GetCheckedRows(ByVal GridView As DataGridView, ByVal ColumnName As String) As List(Of DataGridViewRow)
Return _
(
From SubRows In
(
From Rows In GridView.Rows.Cast(Of DataGridViewRow)()
Where Not Rows.IsNewRow
).ToList
Where CBool(SubRows.Cells(ColumnName).Value)
).ToList
End Function
''' <summary>
''' Remove rows from DataGridView
''' </summary>
''' <param name="sender"></param>
''' <param name="Rows"></param>
<DebuggerStepThrough()>
<Runtime.CompilerServices.Extension()>
Public Sub RemoveRows(ByVal sender As DataGridView, ByVal Rows As List(Of DataGridViewRow))
For iIndex As Integer = Rows.Count - 1 To 0 Step -1
sender.Rows.Remove(Rows(iIndex))
Next
End Sub
End Module
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|