新表在datagridview中不可用作绑定源 [英] new table not available as bindingsource in datagridview

查看:60
本文介绍了新表在datagridview中不可用作绑定源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SQLExpress 2012,Visual Studio2013.我正在使用linqtosql和Visual Basic.我将表添加到sql数据库,然后添加到.dbml模式.我在窗体上使用了datagridview,并为datagridview使用了绑定源.不管出于什么原因 当我为绑定源选择数据源时,新表不会显示在选择列表中.

I am using SQLExpress 2012, Visual Studio 2013. I am using linqtosql and Visual Basic. I added a table to the sql database and then added to the .dbml schema. I am using a datagridview on a form and a bindingsource for the datagridview. For whatever reason, the new table does NOT show up in the selection list when I go to select the datasource for the bindingsource.

SQL不是我的强项,但这不应该是自动的吗?我已经刷新,删除了整个.dbml模式,并将其放回原处,杀死了表并重新创建了它.只是不确定从这里要去哪里.

SQL is not my strong set, but shouldn't this be automatic? I have refreshed, deleted the entire .dbml schema and drug it back over, killed the table and recreated. Just not sure where to go from here.

在此先感谢您的帮助!

推荐答案

您好,

使用Entity Framework,可以刷新有问题的对象,但不是自动的.使用强类型的类,您可以通过一个过程生成这些类,然后除了手动进行操作之外,没有其他更新对象的方法.

With Entity Framework one can refresh the objects in question but is not automatic. Using strong typed classes you generate the classes through a process and afterwards there is nothing to update the objects other than you manually doing so.

现在,SQL-Linq与使用数据向导没什么不同(是的,它们不同,数据向导是从SQL到LINQ生成的,相对于SQL而言),要生成类,必须手动执行此操作,在某些情况下可能会破坏现有的表单中的代码.的是 使用数据向导生成数据类的确具有优势,但如您所见,也有缺点.

Now SQL-Linq is no different than using data wizards (yes they are different, data wizard generated vs SQL to LINQ) to generate your classes, one must do this manually which in some cases may very well break existing code in your forms. The are indeed advantages of using data wizards to generate your data classes but as you can see there are downsides also.

关于Entity Framework的一个很酷的事情是我可以使用数据上下文(SQL到LINQ)如下,其中的关键是db.Database.SqkQuery,它使我可以突破为我生成的强类型类并创建一些需要的类 (请参阅第二个代码块).现在,它说并不能解决所有可能出现的问题,但是如果您拥有(并且我不使用强类型的类在窗体上放置控件),那么很多与生成的代码链接的窗体都担心会破坏很多东西 在某些情况下这可能会有所帮助.

One of the cool things about Entity Framework is I can use a Data Context (SQL to LINQ) is as follows where the key is db.Database.SqkQuery which allows me to break out of strong typed classes that were generated for me and create some what desirable classes (see second code block). Now with that said it does not solve all issues that might crop up but if you have (and I don't use strongly typed classes to drop controls on a form) a lot of forms linked to generated code and are in fear of breaking a lot of stuff this could be helpful in some situations. 

Using db As New Northwind_1Entities
    Dim UK_Query =
        db.Database.SqlQuery(Of CustomerSubSet) _
            (
                <SQL>
                    SELECT 
                        CustomerID, 
                        CompanyName, 
                        Country, 
                        PostalCode
                    FROM 
                        Customers 
                    WHERE country=@Country                            
                </SQL>.Value,
                New SqlClient.SqlParameter With
                {
                    .ParameterName = "@Country",
                    .DbType = DbType.String,
                    .Value = "UK"
                }
            ).ToList

    Console.WriteLine("Begin")
    For Each cust In UK_Query
        Console.WriteLine(cust.CountryPostalCode)
    Next
    Console.WriteLine("end")


    Dim MexicoQuerySubSet =
        db.Database.SqlQuery(Of CustomerSubSet) _
            (
                <SQL>
                    SELECT 
                        CustomerID, 
                        CompanyName, 
                        Country 
                    FROM 
                        Customers 
                    WHERE country=@Country                            
                </SQL>.Value,
                New SqlClient.SqlParameter With
                {
                    .ParameterName = "@Country",
                    .DbType = DbType.String,
                    .Value = "Mexico"
                }
            )

    Console.WriteLine()

    For Each c In MexicoQuerySubSet
        Console.WriteLine(c.ToString)
    Next

    Console.WriteLine()


    Console.ReadLine()

End Using

以上的自定义类

<Serializable()> _
Public Class CustomerSubSet
    Public Property CustomerID As String
    ''' <summary>
    ''' Demoing aliasing in a funky way
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ID As String
        Get
            Return CustomerID
        End Get
    End Property
    Public Property CompanyName As String
    Public Property PostalCode As String
    Public Property Country As String
    <DatabaseGenerated(DatabaseGeneratedOption.Computed)> _
    Public Property CountryPostalCode As String
        Set(value As String)

        End Set
        Get
            Return Country & " : " & PostalCode
        End Get
    End Property
    Public Sub New()
    End Sub
    Public Overrides Function ToString() As String
        Return String.Format("{0,-8}{1,-40},{2}", ID, CompanyName, Country)
    End Function
End Class
Partial Class Customer
    <DatabaseGenerated(DatabaseGeneratedOption.Computed)> _
    Public Property CountryPostalCode As String
        Set(value As String)

        End Set
        Get
            Return Country & " : " & PostalCode
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return String.Format("{0,-8}{1,-40},{2}", CustomerID, CompanyName, Country)
    End Function
End Class

如果我们现在有以下SQL到LINQ

If we now have the following SQL to LINQ

Public Class Form1
    WithEvents bsCustomers As New BindingSource
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim nwContext As New NorthWind1DataContext
        Dim UK_Customers As List(Of Customer) = (From C In nwContext.Customers Where C.Country = "UK").ToList
        bsCustomers.DataSource = UK_Customers
        DataGridView1.DataSource = bsCustomers
    End Sub
End Class
Public Class CustomerSmall
    Public Property Identifier As Integer
    Public Property Name As String
    Public Property Contact As String

    Public Sub New()
    End Sub
End Class

并添加了其他字段,我们没有运气就无法重生.添加新表也是如此.最后,这里没有自动的东西.

and have added additional fields we are out of luck w/o regening. Same goes for adding a new table. In the end there is nothing that is automatic here.


这篇关于新表在datagridview中不可用作绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆