I have a data which is saved in textfile. when form load the data in text file will be load in datagridview. i want delete the selected row from the datagridview. I used this coding to delete.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim row As Integer
Public selRow As New DataGridViewRow
Dim index As Integer
index = DataGridView1.SelectedRows.Item(0).Index
selRow = DataGridView1.Rows.Item(index)
DataGridView1.Rows.Remove(selRow)
row = row - 1
End Sub
When i exucute this code it shows me error "Rows cannot be programmatically removed unless the datagridview is data-bound to an IBindingList that support change notification and allows deletion." I dont understand what is this error and how to solve this? Can any one help me? Thank you |
| kayatri Tuesday, July 28, 2009 1:37 AM |
Hi kayatri, Because List(T) doesn'timplements IBindingList interface so you can't delete it right form the datagridview. Public Class List( Of T) _ Implements IList( Of T), ICollection( Of T), _ IEnumerable( Of T), IList, ICollection, IEnumerable However, if you use bindingsource as the datasource of datagridview, you'll be able to deleteit correctly. 1)Set bindingsource as a global variable. Dim b As BindingSource 2)Set bindingsource's datasource as the list.
l = New List(Of String)
l.Add("tt")
l.Add("tt")
l.Add("tt")
l.Add("tt")
l.Add("tt")
b = New BindingSource
b.DataSource = l
3)Set the datagridview's datasource as the bindingsource.
DataGridView1.DataSource = b
Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.
Best Regards
Yichun Feng
- Marked As Answer bykayatri Friday, July 31, 2009 8:40 AM
-
|
| Yichun_Feng Friday, July 31, 2009 4:37 AM |
Hello Kayatri
First You can not use Public selRow As New DataGridViewRow in button click . you can defain as
Dim selRow As New DataGridViewRow
|
| Navaratan Tuesday, July 28, 2009 5:57 AM |
Thank you for ur reply Navaratan
After i correct.. it still give me the same error. |
| kayatri Tuesday, July 28, 2009 6:18 AM |
Hello Kayatri may be this will be Help you. Plz check in DataGridView | Smart tag (Enable Deleting will be Chaecked) |
| Navaratan Tuesday, July 28, 2009 6:21 AM |
Hello Navaratan
I do everything ready but still same problem. It asking to binding something which i dont understand. Can you help me |
| kayatri Tuesday, July 28, 2009 7:47 AM |
Hello Kayatri
In code where you set data source of DataGridView1
If you use table as Data Source Then use This Line.
DataGridView1.DataSource = Table.Copy()
|
| Navaratan Tuesday, July 28, 2009 9:59 AM |
You can try this (be sure that enable deleting is checked):
Dim rowIndex As Integer
rowIndex = DataGridView1.CurrentCell.RowIndex
DataGridView1.Rows.RemoveAt(rowIndex)
Only performance counts! |
| Sylva Tuesday, July 28, 2009 10:00 AM |
You wil make DELETE command in data atapter smothing like this : DELETE FROM Table WHERE (ID = @ID) the you wil on the button DELETE click event write this :
If grid.SelectedRowsCount = 0 Then Exit Sub If grid.SelectedRowsCount > 0 Then TableAdapter.DELETECOMMAND(grid.GetRowCellValue(grid.FocusedRowHandle, "ID")) End If
I'm using devexpress,but it is simular for windows tools...
|
| TheGodfather_23 Tuesday, July 28, 2009 2:00 PM |
Hi,
May be this is not applicable to you problem but as information.
Your datasource you have bound to your Datagridview needs to be able to fulfill the possibiity to delete rows. As this is not the case you can only view, but not delete items in your grid. You have to understand binding is nothing els then code written by others to get your data loaded into the grid. This bindings can be done in different ways and when you for example do it with a database you willbe asked by the wizzard if you want to be able to add, remove or edit data. If you ack this, then the created code will have this feature. I suggest you to do a samll separate test program which contains nothing then a simple MS Access database with one simple table fill in some data and then bind this data to a grid using the wizzard. Add the remove,and update feature there. Then look what was created. I think you will easily find the differences and being able to handle your code.
Thanks and Regards, Raman Katwal |
| Raman_Katwal Tuesday, July 28, 2009 6:23 PM |
Thanx for all the replys. Im not loading my data from database to the gridview. But im loading from text file. This is my code to loading it.
Dim tfLines() As String = System.IO.File.ReadAllLines("C:\temp.txt")
Dim list = New List(Of Drill)
For Each line As String In tfLines
Dim arr As String() = line.Split("T"c, "C"c, "F"c, "S"c, "H"c)
Dim obj As New Drill() With {.T = arr(1), .C = arr(2), .F = arr(3), .S = arr(4), .H = arr(5)}
list.Add(obj)
Next
DataGridView1.DataSource = list
I am not sure how should i bind the list so that i can perform the delete function? |
| kayatri Wednesday, July 29, 2009 1:04 AM |
To the best of my knowledge how you load(ed) your DataGridView has nothing to do with deleting a row from a DGV. The code I supplied above made no reference to your datasource. Give it a trial:
Dim rowIndex As Integer rowIndex = DataGridView1.CurrentCell.RowIndex DataGridView1.Rows.RemoveAt(rowIndex)
Only performance counts! |
| Sylva Wednesday, July 29, 2009 7:07 AM |
hai Sylva
Thanx for reply. I tryed ur code but the error is still same . The enable deleting alsois checked
|
| kayatri Wednesday, July 29, 2009 7:27 AM |
Hello Kayatri
I try to create same scenario But this line give error message “End of statement expected�/span>
Dim obj As New Drill() With {.T = arr(1), .C = arr(2), .F = arr(3), .S = arr(4), .H = arr(5)}
i have your Drill class
Public
Class Drill
Private _t As Integer
Private _c As Integer
Private _f As Integer
Private _s As Integer
Private _h As Integer
Public Property T() As Integer
Get
Return Me._t
End Get
Set(ByVal value As Integer)
Me._t = value
End Set
End Property
Public Property C() As Integer
Get
Return Me._c
End Get
Set(ByVal value As Integer)
Me._c = value
End Set
End Property
Public Property F() As Integer
Get
Return Me._f
End Get
Set(ByVal value As Integer)
Me._f = value
End Set
End Property
Public Property S() As Integer
Get
Return Me._s
End Get
Set(ByVal value As Integer)
Me._s = value
End Set
End Property
Public Property H() As Integer
Get
Return Me._h
End Get
Set(ByVal value As Integer)
Me._h = value
End Set
End Property
End
Class |
| Navaratan Wednesday, July 29, 2009 8:43 AM |
Hello Navaratan, I am not sure why you geting that error. The Drill class is correct This is my Drill class
Imports System.IO
Public Class Drill
Private _t As Integer
Private _c As Decimal
Private _f As Integer
Private _s As Integer
Private _h As Integer
Public Property T() As Integer
Get
Return Me._t
End Get
Set(ByVal value As Integer)
Me._t = value
End Set
End Property
Public Property C() As Decimal
Get
Return Me._c
End Get
Set(ByVal value As Decimal)
Me._c = value
End Set
End Property
Public Property F() As Integer
Get
Return Me._f
End Get
Set(ByVal value As Integer)
Me._f = value
End Set
End Property
Public Property S() As Integer
Get
Return Me._s
End Get
Set(ByVal value As Integer)
Me._s = value
End Set
End Property
Public Property H() As Integer
Get
Return Me._h
End Get
Set(ByVal value As Integer)
Me._h = value
End Set
End Property
End Class
My text file content look like this
T240C3.175F012S27H2000
T239C0.95F034S55H1000
I break the line according to the T,C,F,S,H and load to datagridview. For me the codes works fine without any error. |
| kayatri Wednesday, July 29, 2009 9:37 AM |
Hello
You have converted your list into a data set or data table
And set as a data set of datagridview.
DataGridView1.DataSource = Table.Copy()
Or
DataGridView1.DataSource =DataSet.Table(0).Copy()
For List to dataset.
http://www.codeproject.com/KB/vb/List2DataSet.aspx |
| Navaratan Thursday, July 30, 2009 5:26 AM |
Hi kayatri, Because List(T) doesn'timplements IBindingList interface so you can't delete it right form the datagridview. Public Class List( Of T) _ Implements IList( Of T), ICollection( Of T), _ IEnumerable( Of T), IList, ICollection, IEnumerable However, if you use bindingsource as the datasource of datagridview, you'll be able to deleteit correctly. 1)Set bindingsource as a global variable. Dim b As BindingSource 2)Set bindingsource's datasource as the list.
l = New List(Of String)
l.Add("tt")
l.Add("tt")
l.Add("tt")
l.Add("tt")
l.Add("tt")
b = New BindingSource
b.DataSource = l
3)Set the datagridview's datasource as the bindingsource.
DataGridView1.DataSource = b
Does this works for you? If you have any questions or concerns, please update the thread and we will have a further discussion.
Best Regards
Yichun Feng
- Marked As Answer bykayatri Friday, July 31, 2009 8:40 AM
-
|
| Yichun_Feng Friday, July 31, 2009 4:37 AM |
Yes Yichun feng.
This methode work for me. I can delete now
Thank you |
| kayatri Friday, July 31, 2009 8:40 AM |
datagridview1.Rows.Remove(datagridview1.CurrentRow)
tose |
| toshemk Friday, November 27, 2009 8:35 AM |