using System;
using System.Diagnostics;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Akadia.DataView
{
public class FilterOrder : System.Windows.Forms.Form
{
....
private SqlConnection cn;
private SqlCommand cmd;
private SqlDataAdapter da;
private DataSet ds;
public FilterOrder()
{
try
{
InitializeComponent();
// Initializing
cn = new SqlConnection("
server=xeon;database=northwind;uid=sa;pwd=manager");
cmd = new SqlCommand("SELECT * FROM orders",cn);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
// Load initial Data
RetrieveData();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine();
}
}
// Retrieve Orders from the DB into the DataGrid
private void RetrieveData()
{
try
{
da.Fill(ds,"Orders");
DataGrid.DataSource = ds.Tables[0];
// Fill Combobx with Column Names
FillSortCriteria();
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
MessageBox.Show(ex.ToString());
}
}
// Fill Combobx with Column Names
private void FillSortCriteria()
{
try
{
// IF Combobox are already filled return ...
if (cmbSortArg.Items.Count > 0)
{
return;
}
// ... else fill Comobox with Column Names
foreach (DataColumn dc in ds.Tables[0].Columns)
{
cmbSortArg.Items.Add(dc.Caption); // Sort Combobox
cmbFields.Items.Add(dc.Caption); // Filter on Column Combobox
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine();
}
}
// Setup the expression used to filter the rows which
// are viewed by the DefaultView
private void SetFilter(string strFilterExpression)
{
try
{
// Apply Filter Expression
ds.Tables[0].DefaultView.RowFilter = strFilterExpression;
// Gets the number of records in the DataView after
// RowFilter and RowStateFilter have been applied.
if (ds.Tables[0].DefaultView.Count > 0)
{
DataGrid.DataSource = ds.Tables[0].DefaultView;
}
else
{
MessageBox.Show("Filter criteria does not meet criteria");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine();
}
}
// Remove any existing Filter and ResultSet from a previous query
private void btnQuery_Click(object sender, System.EventArgs e)
{
try
{
// Clear DataSet
ds.Clear();
// Clear Filter
ds.Tables[0].DefaultView.RowFilter = "";
// Re-Retrieve Data
RetrieveData();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine();
}
}
// Sets the sort column or columns, and sort order for the DataView
private void btnSort_Click(object sender, System.EventArgs e)
{
try
{
string strSort;
// IF Radiobox "Ascending" is checked, then
// sort ascending ...
if (rbAsc.Checked)
{
strSort = cmbSortArg.Text + " ASC"; // Note space after "
}
// ... else descending
else
{
strSort = cmbSortArg.Text + " DESC"; // Note space after "
}
// Apply Sort Criteria to the DataView
ds.Tables[0].DefaultView.Sort = strSort;
DataGrid.DataSource = ds.Tables[0].DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine();
}
}
// Setup specific Filter: "CutomerID LIKE 'xxx'"
private void btnFilterTitle_Click(object sender, System.EventArgs e)
{
try
{
SetFilter("CustomerID like '" + txtFilter.Text + "'");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine();
}
}
// Setup the general Filter entered by the User
private void btnGeneralFilter_Click(object sender, System.EventArgs e)
{
try
{
SetFilter(txtGeneralFilter.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine();
}
}
// Setup the Column Filter entered by the User
private void btnFilteronColumn_Click(object sender, System.EventArgs e)
{
try
{
SetFilter(cmbFields.Text + " " + txtFilterColumn.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
Console.WriteLine();
}
}
...
[STAThread]
static void Main()
{
Application.Run(new FilterOrder());
}
}
}
Filtering and Sorting in ADO.NET
- Overview
- Filtering and Sorting with the DataTable Select Me
- Filtering and Sorting with the DataView Object
- Filtering and Sorting with the DataView Manager
Filtering and Sorting with the DataView Object
You might also like...
ADO.NET books
-
Expert WCF 4: SOA 2.0 with Windows Communication Foundation 4
Windows Communication Foundation has become an integral part of many .NET based solutions, enabling highly customizable messaging across distributed environments. In Expert WCF 4, you will cover scenarios that include designing, implementing, consumi...
ADO.NET forum discussion
-
edmonton female escort services near me
by canadapleasure (0 replies)
-
USB Drive Activity Logger!
by coque0912 (7 replies)
-
Required Every quarter data from sql automatically in below sql format
by jain.piyush831 (0 replies)
-
Bagaimana memenangkan $ 1,54 miliar dalam Mega Jutaan
by gametogelan (0 replies)
-
input integer from text file and output text file
by shmilon (0 replies)
ADO.NET podcasts
-
Visual Studio Talk Show (en français): Louis-Philippe Pinsonneault
Published 7 years ago, running time 1h12m
20 mars 2013 (Ãmission #0157) ::.Louis-Philippe Pinsonneault: Le "App store" de Windows 8Nous discutons avec Louis-Philippe Pinsonneault du "App store" de Windows 8. Nous essaieront de couvrir tout ce quâil y a à savoir sur le "App store" : les types de licences, les modèles de reven.
ADO.NET jobs
-
Web Systems Developer
Red Gate Software in Cambridge, United Kingdom
45,000 -
Web Application Developer
Red Gate Software in Cambridge, United Kingdom
£35,000-45,000 GBP per year -
Senior Software Engineer
@ One Limited in London, United Kingdom
-
Jr. .NET Developer
T-Symmetry in Lakewood, United States
Comments