Can be done?



Hello all, ok i am a newbie yadda yadda and I have 4 tables that I want
to be loaded into one datagrid depending on the users selection in a
combobox. I have set AutoGenerateColumns="True" so that the datagrid
can format the return data however it wants to. I have created a stored
procedure that I want to take the value of the the combobox so that it
knows the right data to return to the datagrid. Can all of this be
done? I have am stopped at the point of when I click on the combobox i
want it to call the SP and fill datagrid with dynamcic data. Every
example i see shows you have to define the columns. i need to also be
able to edit/delete/add this data as well. An good sites on this or is
my structure/idea just way out of wack?? TIA



Rich P

Here is a general purpose routine for populating a datagrid from a sql
server stored procedure:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Dim conn As SqlConnection, da As SqlDataAdapter
Dim ds As Dataset

Public Sub Form1_Load(...)

conn = New SqlConnection
conn.ConnectionString = "Data Source=YourSvr;Initial
Catalog=Northwind;Integrated Security=True"

da = New SqlDataAdapter
da.SelectCommand = New SqlCommand
da.SelectCommand.Connection = conn
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.SelectCommand.CommandText = "yourSP"
da.SelectCommand.Parameters.Add("@yourSPparam", SqlDbType.VarChar, 50,
da.SelectCommand.Parameters("@yourSPparam").Value = "Test"

ds = New Dataset

da.Fill(ds, "onTheFlyTable")

DataGridView1.DataSource = ds.Tables("onTheFlyTable")

End Sub

No need to create any tables or columns. The SqlDataAdapter does that
for you automatically and stores it in the dataset which you will use as
the datasource for your DataGridView.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question