Creating on-the-fly DataGridViews from SQL queries

G

Guest

I am enamored over the visual designer's capability in VS2005 to create all
the controls and customized code to fill and display a DataGridView just by
dragging a data source onto the designer surface and then specifying a SQL
query in the wizard. What I would like to know is how to do this
programmatically?

Specifically, I have a set of arbitrary SQL queries. I want to build a
Windows Form that allows the user to select one of these queries and for the
program then to create a new DataGridView on the current form, where the
columns and datatypes are derived from the results of the query (just as the
visual designer does). How would one approach this task...?
 
T

the_grove_man

How about passing the SQL statement to the new forms constructors and
having a method handle the grid. This is just an
example.........................

private void LoadMainGrid(string SQL)
{
try
{
DataTable table = fillTable(SQL);
foreach (DataRow row in table.Rows)
{
if (row["SimFile"].ToString().Length != 0)
populateGrid(yourDataGridName,
row["aFieldYouWantDisplayed"].ToString(),
row["aFieldYouWantDisplayed"].ToString(),
row["aFieldYouWantDisplayed"].ToString());

}
}
catch (Exception e) { }
finally { cn.Close(); }
}

private void populateGrid(DataGridView dg, string row1, string
row2, string row3)
{
dg.Rows.Add((row1 + ";" + row2 + ";" + row3).Split(';'));
dg.Columns[2].Width = 250;
}

public DataTable FillTable(string SQL)
{
DataConnection();
DataTable table = new DataTable();
using (OleDbDataAdapter da = new OleDbDataAdapter(SQL,
cn))
{
da.Fill(table);
}
cn.Close();
return table;
}


public void DataConnection()
{
if (cn.State == ConnectionState.Open)
cn.Close();

cn.ConnectionString = "YourConnectionString"
cn.Open();
}
 
R

RobinS

michael sorens said:
I am enamored over the visual designer's capability in VS2005 to create
all
the controls and customized code to fill and display a DataGridView just
by
dragging a data source onto the designer surface and then specifying a
SQL
query in the wizard. What I would like to know is how to do this
programmatically?

Specifically, I have a set of arbitrary SQL queries. I want to build a
Windows Form that allows the user to select one of these queries and for
the
program then to create a new DataGridView on the current form, where the
columns and datatypes are derived from the results of the query (just as
the
visual designer does). How would one approach this task...?

Do you know how to run the query and get back a dataset? If so, you can do
that, and then just bind the information to a grid.

myDataGridView.DataSource = myDataTable

You can create the DGV on the fly. Check out the designer code behing the
forms you are already using.

Robin S.
 
G

Guest

That suggestion pointed me in the right direction. For anyone else who might
be interested, here is the short solution I came up with:
====================
string queryString = "...";
string connectionString = "...";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
myDataGridView.DataSource = dataTable;
====================
 

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

Top