473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Creating on-the-fly DataGridViews from SQL queries

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...?

Feb 23 '07 #1
3 1793

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

Feb 23 '07 #2

"michael sorens" <mi***********@discussions.microsoft.comwrote in message
news:69**********************************@microsof t.com...
>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.
Feb 24 '07 #3
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;
====================

Feb 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Inpreet | last post by:
Hello I am using: ************************************************************************* header("Content-Type:application/force-download",false); header("Content-Disposition: attachment;...
1
by: Vannela | last post by:
In my unmanaged there is code i n this way Class1 c1= new Class1(); // Class1 is one object and creating a instance of it\ Class1 c2= new Class2();// Class2 is another object a...
3
by: Philip Carnstam | last post by:
Hi, Can someone lead me through creating a user object in AD. I have tried creating one through LDAP and ADSI (WinNT://) but nothing happens. DirectoryEntry DE = new...
1
by: M K | last post by:
Can I create code so that when my users create a username in an Intranet form (Using Integrated Windows security) that user gets added to AD under a certain group? Where can I get info on this?
2
by: maykut | last post by:
Hi everybody! I have a problem with creating classes in a java source code.I don't know is it possible. creating an non-existing class and filling it with its constructor java source code?After all...
1
by: maykut | last post by:
Hi everybody! I have a problem with creating classes in a java source code.I don't know is it possible. creating an non-existing class and filling it with its constructor in another java source...
1
by: vijay | last post by:
HI all, I have done a C#.net windows application and used Microsoft Excel 11.0 object library.My problem is while creating setup of my project it include Excel.exe which has big size.But...
0
by: =?Utf-8?B?TWFyaw==?= | last post by:
Users of an in-house application we have written randomly get an Error creating window handle exception, and we've not been able to determine why this happens. A typical callstack is as follows: ...
4
by: reyalberto | last post by:
I am interested in creating an online appointment page where my clients can login and set an appointment with me. I'm somewhat familiar with PHP, but would like to know how should I go for creating...
5
by: prakashsakthivel | last post by:
Hi Everyone Is any way to create self creating .exe in visual basic that means, we can create .txt file can we create .exe also same like that creating .txt file Hope I will get solution. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.