| List for WinForms Tutorials > Tutorial 5 - Using C1List with SQL Query Results |
In this tutorial, you will learn how to use C1List to display the results of ad-hoc SQL queries. In addition, it will also outline how to set up a connection to a DataSet at run-time. Note that in order for the list to automatically respond to field layout changes, you must not have defined any column properties at design time. If a layout is already defined, use the list's Clear Fields context menu command to remove it. This will cause the list to configure itself automatically at run time.

To write code in Visual Basic
| Visual Basic |
Copy Code
|
|---|---|
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet()
Dim sqlStr As String
Dim da As OleDb.OleDbDataAdapter
sqlStr = Me.TextBox1.Text.Trim()
da = New OleDb.OleDbDataAdapter(sqlStr, Me.ComposerTableAdapter.Connection)
Try
da.Fill(ds, "TestSQL")
Me.C1List1.DataSource = Nothing
Me.C1List1.ClearFields()
Me.C1List1.DataSource = ds.Tables("TestSQL")
Catch x As Exception
MsgBox("Invalid SQL statement.")
End Try
End Sub
|
|
To write code in C#
| C# |
Copy Code
|
|---|---|
private void Button1_Click( System.object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
string sqlStr;
OleDb.OleDbDataAdapter da;
sqlStr = this.TextBox1.Text.Trim();
da = new OleDb.OleDbDataAdapter(sqlStr, this.ComposerTableAdapter.Connection);
try
{
da.Fill(ds, "TestSQL");
this.c1List1.DataSource = null;
this.c1List1.ClearFields();
this.c1List1.DataSource = ds.Tables("TestSQL");
}
catch( Exception x)
{
MsgBox("Invalid SQL statement.");
}
}
|
|
As in Tutorial 1 - Binding C1List to a DataSet, C1List retrieves the database schema information from the DataSet and automatically configures itself to display the data for all fields in the database table. Note that the field names are used as the default column headings.
Select * from ComposerSelect Country from ComposerSelect Last, Country from ComposerSelect Count(*) from ComposerSelect UCase(Last) as ULAST, UCase(First) AS UFIRST from ComposerSELECT * FROM Composer WHERE First = "Edward"In the TextBox control, enter the following SQL statement: SELECT * FROM Composer ORDER BY LastThis concludes the tutorial.