DATAGRIDVIEW SORTING AND FILTERING C#.

Simons Hood

Total Post:70

Points:494
Posted by  Simons Hood
C# 
C#
 3270  View(s)
Ratings:
Rate this:
Hi developers,

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

    Post:378

    Points:2694
    Re: DataGridView Sorting and Filtering C#.

    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;

            }

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!