vb.net 从数据集中删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28087038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove row from dataset
提问by Nihilo
I would like to copy an excel data table into my datagridviewbut I want to start from a specific row where my table resides (row 9) (there are title, comments, etc.. before that that are not part of the table.)
I am using the following code but it doesn't delete the row from the dataset.
我想将一个 excel 数据表复制到我的表中,datagridview但我想从我的表所在的特定行(第 9 行)开始(在此之前有标题、评论等,它们不是表的一部分。)我我正在使用以下代码,但它不会从dataset.
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
DtSet.Tables(0).Rows(3).Delete()
DtSet.Tables(0).Rows(3).AcceptChanges()
dataGridArray(selectedTab).DataSource = DtSet.Tables(0)
'MsgBox("number of Row(s) - " & DtSet.Tables(0).Rows.Count)
MyConnection.Close()
after delete()and acceptChanges, I can still see the title.
后delete()和acceptChanges,我仍然可以看到标题。
Does anyone can see where am I wrong? Thank you.
有谁可以看到我错在哪里?谢谢你。
回答by Tim Schmelter
You don't want to deletethe row(in excel) but you want to removeit from the table.
您不想删除该行(在 excel 中),但您想将其从表中删除。
So instead of:
所以而不是:
DtSet.Tables(0).Rows(3).Delete()
use:
用:
DtSet.Tables(0).Rows.RemoveAt(3)
But since you don't want to remove a single row as your code suggests but to remove all rows before the 9th row, use:
但是由于您不想按照代码的建议删除一行,而是要删除第 9 行之前的所有行,请使用:
For i As Int32 = 1 To 8
DtSet.Tables(0).Rows.RemoveAt(0)
Next
回答by Nihilo
I solved it with the following code I don't know If it is the best elegant solution, but it works :
我用下面的代码解决了它我不知道它是否是最好的优雅解决方案,但它有效:
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=""Excel 12.0;IMEX=1""")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
' MyCommand.TableMappings.Add("Table", "Net-informations.com")
DtSet = New System.Data.DataSet
MyCommand.Fill(DtSet)
'find row with the second "item", there are 2. Its where my table starts
For r = 0 To DtSet.Tables(0).Rows.Count - 1
If DtSet.Tables(0).Rows(r).Item(0).ToString = "Item" Then
headerRowSearched = r
End If
Next
'correct name header with the right one.
For c As Integer = 0 To DtSet.Tables(0).Columns.Count - 1
DtSet.Tables(0).Columns(c).ColumnName = DtSet.Tables(0).Rows(headerRowSearched)(c)
Next
'remove all the junk rows up to my header
For i As Int32 = 0 To headerRowSearched
DtSet.Tables(0).Rows.RemoveAt(0)
Next
dataGridArray(selectedTab).DataSource = DtSet.Tables(0)
MyConnection.Close()
回答by Nihilo
What I would do is just delete the column headers and replace them with data in row 9.
我要做的就是删除列标题并用第 9 行中的数据替换它们。

