PC Review


Reply
Thread Tools Rate Thread

Creating on-the-fly DataGridViews from SQL queries

 
 
=?Utf-8?B?bWljaGFlbCBzb3JlbnM=?=
Guest
Posts: n/a
 
      23rd Feb 2007
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...?

 
Reply With Quote
 
 
 
 
the_grove_man@yahoo.com
Guest
Posts: n/a
 
      23rd Feb 2007

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();
}

 
Reply With Quote
 
RobinS
Guest
Posts: n/a
 
      24th Feb 2007

"michael sorens" <(E-Mail Removed)> wrote in message
news:69C599C6-4F07-4F2F-B43F-(E-Mail Removed)...
>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.


 
Reply With Quote
 
=?Utf-8?B?bWljaGFlbCBzb3JlbnM=?=
Guest
Posts: n/a
 
      26th Feb 2007
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;
====================

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagridviews and hyperlinks Dom Microsoft C# .NET 1 22nd Oct 2010 01:36 PM
3 Datagridviews bound =?Utf-8?B?QWRl?= Microsoft C# .NET 1 7th Nov 2007 07:17 PM
Help with Datagridviews Mike Fellows Microsoft VB .NET 2 28th Jun 2006 11:25 AM
link two datagridviews? Freddy Microsoft Dot NET Framework Forms 0 5th Jun 2006 07:53 PM
RE: Editing Multiple DataGridViews =?Utf-8?B?S2VuIFR1Y2tlciBbTVZQXQ==?= Microsoft VB .NET 0 20th Apr 2006 07:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:47 PM.