嗨所以我有这个列表,我目前正在使用组合框,这就是为什么我idcategoria = 0
与nomeCategoria = "Select your Category"
的原因,因此组合框默认项目将是"选择你的类别&#34 ;.
这是列表的代码
Public Shared Function ObterTodosC() As List(Of Ccategoria)
Dim lstTodos As List(Of Ccategoria) = New List(Of Ccategoria)
Dim p As Ccategoria = New Ccategoria()
p.IdCategoria = 0
p.NomeCategoria = "select your category"
lstTodos.Add(p)
Try
Using con As SqlConnection = New SqlConnection()
con.ConnectionString = myDAC._connectionString
Using cmd As SqlCommand = con.CreateCommand()
cmd.CommandText = "select * from Categoria"
con.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
Dim p As Ccategoria = New Ccategoria()
p.IdCategoria = dr.GetInt32(0)
p.NomeCategoria = dr.GetString(1)
lstTodos.Add(p)
End While
End Using
End Using
Catch ex As SqlException
Throw ex
Catch ex As Exception
Throw ex
End Try
Return lstTodos
End Function
现在我想在datagridview上使用相同的列表,我想知道是否有一种方法不在datagridview上显示id = 0,或者我是否必须为datagridview创建另一个没有idCategorie = 0的列表,对此有何看法?感谢
答案 0 :(得分:1)
从已加载的
创建另一个列表Dim newList = lstTodos.Skip(1).ToList()
Skip
方法将返回没有第一项的新集合
请注意,只有- Select your Category -
项是列表中的第一项时,此方法才有效。
或者将您的方法更改为不带- Select your Category -
项目的返回列表,并仅在需要时添加。
Public Shared Iterator Function ObterTodosC() As IEnumerable(Of Ccategoria)
Using con As SqlConnection = New SqlConnection()
con.ConnectionString = myDAC._connectionString
Using cmd As SqlCommand = con.CreateCommand()
cmd.CommandText = "select * from Categoria"
con.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
Yield New Ccategoria With
{
.IdCategoria = reader.GetInt32(0),
.NomeCategoria = reader.GetString(1)
}
End While
End Using
End Using
End Function
然后,您可以为datagridview
创建类别列表Dim forDataGridView = ObterTodosC().ToList()
Dim notSelectedCategory As New Ccategoria With
{
.IdCategoria = 0,
.NomeCategoria = "select your category"
}
Dim forComboBox = forDataGridView.ToList()
forComboBox.Insert(0, notSelectedCategory)
使用此方法可以从ObterTodosC
方法中删除副作用
所以方法责任只是从数据库加载项目