【问题标题】:Search As You Type in C# Using DataGridView使用 DataGridView 在 C# 中键入时进行搜索
【发布时间】:2019-01-24 21:19:39
【问题描述】:

我只是想和大家分享一些知识,因为我花了将近半天的时间在VS2017中使用winforms寻找“键入时搜索”的解决方案。我发现的大多数其他解决方案都使用 SQL 数据库作为数据源,或者建议使用其他工具或控件,这不是我正在寻找的解决方案。

无论如何,我向 Web API 发出请求并将响应放入列表中。然后,我将该列表用作 DataGridView 控件的数据源。然后我使用 Key_Up 作为触发器。下面是我的最终解决方案,效果很好!

希望这会有所帮助!

【问题讨论】:

  • 为了使您的知识共享有效,并使您的帖子以 stackoverflow 格式对未来的读者有用,您应该提出一个好的问题并发布一个好的答案。如果您阅读有关How to AskHow to Answer 的信息,那就太好了。
  • 很抱歉,我认为这很好分享,因为我自己找不到好的答案。
  • 我并不是说分享不好。你的问题有一个很好的标题,但缺乏很好的描述。事实上,你是在讲述一个关于解决方案的故事,同时你应该描述你试图解决的问题,分享一些代码,预期的输出,......。通过提出一个好问题,您将帮助未来的读者了解他们是否有同样的问题。此外,通过提出一个好的问题,您可能会收到一些好的答案或指向一些重复的链接。同样关于答案,它应该包含一些关于解决方案的描述以及它如何帮助解决问题。

标签: c# datagridview full-text-search datasource


【解决方案1】:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AddUser_API
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /*Makes the request to API for the groups when the form loads. I obviously have a different class that handles this request*/
            Request userClient = new Request();

            //endpoint is a GET request
            userClient.endPoint = userClient.endPoint;
            userClient.httpMethod = httpVerb.GET;

            string strResponse = string.Empty;

            strResponse = userClient.makeRequest();

            /*This will put the response into a list then fill the datagridview control with the Web API response*/

            List<getUser> grpName = JsonConvert.DeserializeObject<List<getUser>>(strResponse);

            dgvUserList.DataSource = grpName;

//Cosmetics
                dgvUserList.Columns[0].DefaultCellStyle.Padding = new Padding(0, 0, 28, 0);
                dgvUserList.Columns[1].DefaultCellStyle.Padding = new Padding(0, 0, 28, 0);   
            }

/* the following key up will allow the user to search as they type in the textbox control*/

        private void txtbxByUsername_KeyUp(object sender, KeyEventArgs e)
        {
            string searchValue = txtbxByUsername.Text.ToLower();
            dgvUserList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            try
            {
                foreach (DataGridViewRow row in dgvUserList.Rows)
                {
                    for (int i = 0; i < row.Cells.Count; i++)
                    {
                        if (row.Cells[i].Value != null && row.Cells[i].Value.ToString().ToLower().Contains(searchValue))
                        {
                            int rowIndex = row.Index;
                            dgvUserList.Rows[rowIndex].Selected = true;
                            break;
                        }

                    }

                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多