Answer the question
In order to leave comments, you need to log in
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);
}
}
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);
}
//Сортировка с 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;
}
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);
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question