M
M
matros972017-11-13 08:00:14
.NET
matros97, 2017-11-13 08:00:14

Datagridview filter alphabetically - C#?

Good morning, I’m doing a database test and everything seems to be fine, but a couple of questions arose that I can’t do correctly, to be honest, I have never worked with a database and C # only with PHP + MySQL, but MSDN is good.
I have a database, and the application has buttons that should filter by date of birth and by city, I did it like this

private void 18летToolStripMenuItem_Click(object sender, EventArgs e)
        {
 
            try
            {
                DataView dv = dt.DefaultView;
                int date = year -18;
                dv.RowFilter = String.Format("date > '{0}'", date);
                dataGridView_Main.DataSource = dv;
 
 
                
            }
 
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
        }

Everything works cool)) but there is a problem, I need to sort this sort by those who are 18 years old alphabetically, and the user must press the button to sort alphabetically
, I also wrote the code
private void аДоЯToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataView dv = dt.DefaultView;
         
            dv.RowFilter = String.Format("year = {0}", year);
            dataGridView_Main.DataSource = dv;
            this.dataGridView_Main.Sort(this.dataGridView_Main.Columns["name"], ListSortDirection.Ascending);
 
        }

But the problem is that the entire database is sorted in the datagridview and not what I sorted by age.
I decided to write a method for sorting data selection through the database
, here is the code
//Сортировка с 1 парараметром
        public DataTable Sort(int date)
        {
 
            int rokiv = this.year - date; 
 
 
            DataTable dt = new DataTable("table");
 
            SQLiteConnection connection = new SQLiteConnection(connect_db);
 
            string sql = String.Format("SELECT id, year, number, name, date, vidil, vpo, invalde FROM '{0}' WHERE date > {1} AND year = {2}", tablename, date, year);
 
            SQLiteCommand command = new SQLiteCommand(sql, connection);
 
            connection.Open();
 
            dAdpt = new SQLiteDataAdapter(command);
 
            dAdpt.Fill(dt);
 
 
 
            connection.Close();
 
 
            return dt;
 
 
        }

and this is how I tried to populate the datagridview table
private void до7РоківToolStripMenuItem_Click(object sender, EventArgs e)
        {
 
            try
            {
               /* DataView dv = dt.DefaultView;
                int date = year -7;
                dv.RowFilter = String.Format("date > '{0}' AND year = {1}", date, year);
                dataGridView_Main.DataSource = dv;*/
 
              dt = db.Sort(18);
 
 
 
            dataGridView_Main.DataSource = dt;
 
           
 
            DataView dv = dt.DefaultView;
            
  
            dataGridView_Main.DataSource = dv;
 
 
                
            }
 
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
        }

But nothing happened, as there was a general table and it is.
Maybe tell
me something or help me find an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Evseev, 2017-11-23
@alex1t

Why this property (DataView.Sort) is not suitable:
https://msdn.microsoft.com/en-us/library/system.da...
Since you are already using the DataView.Filter property

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question