Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

VB.NET,winform- difficulties in datagridview

RekhascRekhasc Posts: 7Member
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

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=N0C7G6Databasescompany; 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

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    Whats it saying is wrong with that code?

    One thing I noticed is that your recreating a connection and apparently rebinding the dataview on the Cell_Click event. Maybe you want it to create a connection and refresh on Load, so it only does it once. Or maybe on a refresh command that you call from a button or menu item.

    Another thing I thought you could do is add a Statement like this:

    [code]
    If Con.ConnectionState = ConnectionState.Closed Then 'I am writing this without Visual studio so it may be a bit different syntax

    MsgBox("Broken")
    'Stop

    End If
    [/code]

    The Stop command is like adding a break line in your code. It will stop the execution of the program and allow you to look at variables in there current state inside the program. More importantly, it can be called inside a condition so your program won't pause in all cases, just in the ones you specify. I have found that debugging line for line is a great help in solving issues.

    Post some more info, such as an error message or syntax message on whats causing that not to work. At first glance I would suspect your connection is not getting established, but that is just because I use a connection string that looks different from yours. Also, what version of .Net (or VS) are you using?

    -firesickle.com
Sign In or Register to comment.