Home > DeveloperSection > Forums > DataGridView Sorting and Filtering C#.
Simons Hood
Simons Hood

Total Post:70

Points:494
Posted on    June-15-2013 4:48 AM

 C# C# 
Ratings:


 1 Reply(s)
 2354  View(s)
Rate this:
Hi developers,

How to set Sorting and Filtering in datagridview using C#.


Sumit Kesarwani

Total Post:378

Points:2694
Posted on    June-15-2013 7:41 AM

Hi Samuel,

You can use this code:

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;

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick