C# DataGridView Sorting and Filtering


CSharp.Net-Informations.com
SiteMap

The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. A DataView provides a means to filter and sort data within a DataTable. The following C# program shows how to filter and sort a DataGridView by using a DataView.

dv = new DataView(ds.Tables[0], "Price > 19", "Price Desc", DataViewRowState.CurrentRows);

dataGridView1.DataSource = dv;


using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True";
            string sql = "SELECT * FROM Titles";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
            DataSet ds = new DataSet();
            connection.Open();
            dataadapter.Fill(ds, "Titles_table");
            connection.Close();

            DataView dv;
            dv = new DataView(ds.Tables[0], "Price > 19", "Price Desc", DataViewRowState.CurrentRows);
            dataGridView1.DataSource = dv;
        }
    }
}







More Source Code :
|  Home  |   VB.NET  |   C#  |   ASP.NET  |   SiteMap  |   Terms of Use  |   About  |