rattlesnake316

how to fill datagrid through code


Re: Visual Basic Express Edition how to fill datagrid through code?

Steve2222

Look up "How to: Manipulate Rows in the Windows Forms DataGridView Control " in the help file and there is example code which shows one method. Exactly how you do it will depend on what sort of data you want to fill with.





Re: Visual Basic Express Edition how to fill datagrid through code?

Riquel Dong ĘC MSFT

Hi Rattlesnake316,

Based on your post, you need to fill a DataGrid control. I recommend you see the code samples from here: http://msdn2.microsoft.com/en-us/data/aa937711.aspx. There is one article about how to retrieve data from the database, display the data in a DataGrid and how to format the appearance of the DataGrid. Hope this helps.

Thanks for your questions.






Re: Visual Basic Express Edition how to fill datagrid through code?

Omar Abid

You need only to know the connection string and the table name

This is the code

Code Snippet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Store a connection string

' Try to make your own data source is the server name and attachdb is file location

'Other settings are optional

Dim connectionstring As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Omar Abid\Mes Documents\data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"

'Create new SQL Connection

Dim sqlconnection As New SqlConnection(connectionstring)

Using sqlconnection

Dim sqlcommand As SqlCommand = sqlconnection.CreateCommand

Using sqlcommand

sqlcommand.CommandType = CommandType.Text

sqlcommand.CommandText = "select * from table1" ' Replace table1 with your table !

Dim adapter As New SqlDataAdapter(sqlcommand)

Using adapter

Dim datatable As New DataTable("user")

Using datatable

adapter.Fill(datatable)

DataGridView.DataSource = datatable

End Using

End Using

End Using

End Using

End Sub






Re: Visual Basic Express Edition how to fill datagrid through code?

pinoyz

Hi rattlesnake,

try this

Dim conn as new oledb.oledbconnection("Connectionstrings")

Dim cmd as new oledb.oledbcommand("Select * from Table1", conn)

Dim da as new oledb.oledbdataadapter(cmd)

dim ds as new dataset

cmd.connection.open

da.fill(ds)

cmd.connection.close

datagridview1.datasource = ds.tables(0).defaultview






Re: Visual Basic Express Edition how to fill datagrid through code?

slonkshank

pinoyz wrote:

Hi rattlesnake,

try this

Dim conn as new oledb.oledbconnection("Connectionstrings")

Dim cmd as new oledb.oledbcommand("Select * from Table1", conn)

Dim da as new oledb.oledbdataadapter(cmd)

dim ds as new dataset

cmd.connection.open

da.fill(ds)

cmd.connection.close

datagridview1.datasource = ds.tables(0).defaultview



i read your post it help me alot thanks!!!