使用研究键过滤datagridview并仅显示最后五行。 [英] Filter datagridview with research key and only show the last five rows .

查看:103
本文介绍了使用研究键过滤datagridview并仅显示最后五行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用研究键过滤datagridview时代码有效,但我找不到正确的格式代码来显示最后5行。



我尝试了什么:



我尝试了这段代码,但它不起作用:



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)" ;

推荐答案

首先,永远不要使用字符串连接来创建这样的SQL语句。研究参数并使用它们。



要获得最后5行,请尝试此查询

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
SELECT * FROM (
    SELECT * FROM yourTtable ORDER BY (id) DESC LIMIT 5
    WHERE Convert(researchkey,'System.String') like @filter
) subq
ORDER BY (id) ASC





- 此解决方案仅在填充DataGridView时有效,而不适用于f iltering。我正在尝试找到正确的解决方案,并会尽快重新考虑这个解决方案。



。我已经玩了一段时间,并且只能通过在datagridview上实现分页来限制显示到适合过滤器的第一行 5行

(这些文章提出想法如何做到这一点:

DataGridView使用分页(UserControl) [ ^ ]或

在WinForm应用程序中使用DataGridView进行分页的简单方法 [ ^ ])



你只能显示最后5通过导航到过滤网格中的最后一行:



- 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.

. 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:

DataGridView1.FirstDisplayedScrollingRowIndex = DataGridView1.RowCount - 1
DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = True





我个人不会对DataGridView进行过滤,只是用预过滤的数据填充它 - 在这种情况下我的原始SQL语句将适用。



例如(未经测试)



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)

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();
      }
  }


这篇关于使用研究键过滤datagridview并仅显示最后五行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆