Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The code works when I filter the datagridview with research key but I couldn't find the correct format code to show the last 5 rows only.

What I have tried:

I tried this code but it doesn't work :

bs.Filter = "Convert(researchkey,'System.String') like '" + textBox.Text + "'   and  ORDER BY (id) DESC(5)" ;
Posted
Updated 13-Mar-17 22:53pm
Comments
Richard Deeming 17-Mar-17 10:31am    
REPOST
You have already posted this question:
https://www.codeproject.com/Questions/1175161/Filter-datagridview-with-matricule-number-and-only[^]

Since this copy has a solution, you should delete the earlier copy.

1 solution

Firstly never ever use string concatenation to create your SQL statements like this. Research Parameters and use them.

To get the last 5 rows try this query
SQL
SELECT * FROM (
    SELECT * FROM yourTtable ORDER BY (id) DESC LIMIT 5
    WHERE Convert(researchkey,'System.String') like @filter
) subq
ORDER BY (id) ASC


[Edit] - This solution only works when populating the DataGridView and not when filtering. I am attempting to find the correct solution and will revisit this solution as soon as possible.

[Edit2]. I've played around with this for some time and can only limit the display to the first 5 rows which fit the filter - by implementing paging on the datagridview
(These articles give ideas how to do that:
DataGridView With Paging (UserControl)[^] or
A Simple Way for Paging in DataGridView in WinForm Applications[^])

You could display only the last 5 by navigating to the last row in the filtered grid:
VB
DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.RowCount - 1
DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = True


Personally I would not do the filtering on the DataGridView but just populate it with pre-filtered data - in which case my original SQL statement would apply.

E.g. (untested)
C#
private void button1_Click(object sender, EventArgs e)
  {
      var constring = ""; //put your connection string in here
      var sql =
          "SELECT * FROM (SELECT * FROM yourTtable ORDER BY (id) DESC LIMIT 5 WHERE Convert(researchkey,'System.String') like @filter) subq ORDER BY (id) ASC";

      using (var connection = new SqlConnection(constring))
      {
          connection.Open();

          using (var myAdapter = new SqlDataAdapter(sql, connection))
          {
              myAdapter.SelectCommand.Parameters.AddWithValue("@filter", textBox1.Text);
              var ds = new DataSet();
              myAdapter.Fill(ds);
              bs.DataSource = ds.Tables[0];
          }

          connection.Close();
      }
  }
 
Share this answer
 
v4
Comments
EM_Y 17-Mar-17 4:45am    
Is it right to write like this :

  private void button1_Click(object sender, EventArgs e)
        {
            String constring = "";
            MySqlConnection connection = new MySqlConnection(constring);
            connection.Open();
            BindingSource bs = new BindingSource();

            bs.DataSource = dataGridView1.DataSource;
            bs.Filter = "SELECT * FROM (SELECT * FROM database.table ORDER BY (id) DESC LIMIT 5 WHERE Convert(researchKey,'System.String') like '" + textbox.Text + "') subq ORDER BY (id) ASC";

       
            dataGridView1.DataSource = bs;

        }
CHill60 17-Mar-17 8:26am    
No - sorry I have misled you. The query I supplied would be to populate the DGV with filtered data and NOT to filter the view of the existing data in the DGV. I was wrong.
I'll update my solution. Again - my apologies
EM_Y 17-Mar-17 9:18am    
I'm thankful , I wish your help :)
CHill60 17-Mar-17 9:23am    
I've just updated my solution
EM_Y 17-Mar-17 10:01am    
Thank you so much for your help , I will try this :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900