VB.NET,winform- difficulties in datagridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rekhasc
    New Member
    • Oct 2007
    • 66

    VB.NET,winform- difficulties in datagridview

    hi,
    i was using .net1.1.. now i am using .net2.0 and sqlserver2005
    in my form i have a datagrid and i want view the contents of contents of table how write the code in vb.net 2.0.. i tried the following code but its not working
    plz help me
    Code:
     
    Imports System.Data.SqlClient
    Public Class Form1
     
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
     
    Dim con As New SqlConnection("Data Source=localhost; Initial Catalog=N0C7G6\Databases\company; Integrated Security=SSPI")
    Dim ad As New SqlDataAdapter("select * from emp", con)
    Dim ds As New DataSet
    ad.Fill(ds)
    DataGridView1.DataSource = ds
    DataGridView1.DataBindings()
    End Sub
    Last edited by DrBunchman; May 30 '08, 09:14 AM. Reason: Added code tags - Please use the # button
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    You are filling a DataSet, but you aren't specifying what table in the DataSet is the binding source. Try this:
    Code:
    .
    .
    .
    ad.Fill(ds,"empTable")
    Dim table as System.Data.DataTable
    table = ds.Tables("empTable")
    DataGridView1.DataSource = table
    DataGridView1.Update()
    I'm not sure what the DataGridView1.D ataBindings() is supposed to do, but Update should force the control to refresh itself.

    Comment

    • rekhasc
      New Member
      • Oct 2007
      • 66

      #3
      Thanxs for your reply...
      but its giving error

      Cannot open database "D:\company.mdf " requested by the login. The login failed.
      Login failed for user 'N0C7G6\Rekha'.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Originally posted by rekhasc
        Thanxs for your reply...
        but its giving error

        Cannot open database "D:\company.mdf " requested by the login. The login failed.
        Login failed for user 'N0C7G6\Rekha'.
        Well, that's a permissions error.

        Comment

        Working...