【问题标题】:datagridview not showing the first row vb.netdatagridview 不显示第一行 vb.net
【发布时间】:2017-03-22 01:28:11
【问题描述】:

嗨,我有这个列表,我目前在组合框上使用,这就是为什么我有 idcategoria = 0 和 nomeCategoria = "Select your Category",所以组合框默认项是“选择您的类别”。 这是列表的代码

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 的列表,对此有什么想法吗?谢谢

【问题讨论】:

  • 从已加载的列表中创建另一个列表:Dim newList = lstTodos.Skip(1).ToList().
  • 与问题无关 - 但您可以删除 try ... catch。你不处理抛出的异常 - 所以你可以让你的代码更清晰
  • @Fabio 当我将列表链接到组合框和/或 datagridview 时,我正在处理抛出的异常
  • 我只是没有在这里发布组合框的代码,因为它没有必要
  • 哦,@Fabio 我不知道 .skip 的存在,谢谢你试试看,(ps:你能对这个问题做出一个实际的答案,以便我验证吗?)

标签: vb.net datagridview


【解决方案1】:

从已加载的列表中创建另一个列表

Dim newList = lstTodos.Skip(1).ToList()

Skip 方法将返回没有第一项的新集合。
请注意,只有当- Select your Category - item 是列表中的第一项时,此方法才有效。

或者将您的方法更改为不带- 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 方法中删除副作用。
所以方法的职责将只是从数据库中加载项目

【讨论】:

  • 不,你看我在哪里混音?
  • 将 notSelectedCategory 调暗为具有 { .IdCategoria = 0 .NomeCategoria = "select your category" } 的新类别
  • 那不是c#代码吗?因为 int vb 它不需要 { 对吗?
  • 和这个 Dim forDataGridView = ObterTodosC().ToList()
  • 这是有效的vb.net 代码检查Object Initializers
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多