winforms 如何在datagridview中将包含搜索结果的行置于顶部

mkh04yzy  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(206)

我尝试使用列通过datagridview搜索值,我有应用装置类型和应用装置名称列,多个应用装置可以具有相同的应用装置,用户将使用应用装置类型列进行搜索
我想要的是,当用户搜索应用装置类型时,具有搜索到的应用装置类型的所有行都应该像某种排序一样出现在顶部
下面是我已经尝试过,但它只带来一行
先谢了

private DataGridViewCell gettext(string searchtext,DataGridView dataGridView,int index)
        {
            dtgridappliance.MultiSelect = true;
            dtgridappliance.SelectionMode=DataGridViewSelectionMode.FullRowSelect;
            DataGridViewCell met = null;
            foreach(DataGridViewRow row in dataGridView.Rows)
            {
                for (int i = 0; i < row.Cells.Count; i++)
                {

                    if (row.Cells[index].Value != null && row.Cells[index].Value.ToString().ToLower().Contains(searchtext.ToLower()))
                    {
                        met = row.Cells[index];
                        dtgridappliance.Rows[index].Selected = true;
                        this.dtgridappliance.Sort(dtgridappliance.Columns[0],ListSortDirection.Ascending);
                        dtgridappliance.FirstDisplayedScrollingRowIndex = dtgridappliance.SelectedRows[i].Index;
                        break;
                    } 
                }
            }
            return met;
        }
uoifb46i

uoifb46i1#

您的问题是如何在datagridview中将包含搜索结果的行置于顶部,而您当前的方法是尝试使用DataGridView.Sort直接使用UI控件。
如果您愿意用另一种方法来实现这一点,那么对于DataGridView,通过设置DataSource属性(在本例中为BindingList<Appliance>)并使用该列表通常更容易实现您想要的结果。
对于初学者来说,DGV可以完全设置使用主窗体的OnLoad中的~10行。

public partial class MainForm : Form
{
    public MainForm() => InitializeComponent();
    private BindingList<Appliance> Appliances { get; }  = new BindingList<Appliance>();
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        // Set up the DGV and add a few appliances.
        dtgridappliance.DataSource = Appliances;
        Appliances.Add(new Appliance(ApplianceType.Refrigerator, "Maytag" ));
        Appliances.Add(new Appliance(ApplianceType.Refrigerator, "LG" ));
        Appliances.Add(new Appliance(ApplianceType.Microwave, "Amana" ));
        Appliances.Add(new Appliance(ApplianceType.Dishwasher, "Samsung" ));
        Appliances.Add(new Appliance(ApplianceType.Dishwasher, "Whirlpool" ));
        dtgridappliance.AllowUserToAddRows = false;
        // Format columns
        dtgridappliance.Columns[nameof(Appliance.Name)].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

        // Populate the combo box and dd a handler for when the combo box changes
        object[] values = Enum.GetValues(typeof(ApplianceType)).Cast<object>().ToArray();
        comboBoxSearch.Items.AddRange(values);
        comboBoxSearch.SelectedIndex = (int)ApplianceType.Refrigerator;
        comboBoxSearch.SelectionChangeCommitted += onSearchByApplianceType;
    }
}

其中表示一行数据的类如下所示:

enum ApplianceType { Dishwasher, Microwave, Refrigerator  }
class Appliance
{
    public Appliance(ApplianceType applianceType, string name)
    {
        ApplianceType = applianceType;
        Name = name;
    }
    public ApplianceType ApplianceType { get; private set; }
    public string Name { get; private set; }
}

排序需要一种方法来比较给定的Appliance和组合框中选择的类型:

private int compareToSelectedType(Appliance appliance)
{
    return appliance.ApplianceType.Equals(comboBoxSearch.SelectedItem) ?
        0 : 1; // Ones that match will sort before ones that don''t.
}

现在,当组合框更改时,您可以移除所有DataGridView项,然后以不同的顺序放回。

private void onSearchByApplianceType(object? sender, EventArgs e)
{
    // Use System.Linq to sort a new temporary list.
    var tmp = new List<Appliance>(
        Appliances
        .OrderBy(_=>compareToSelectedType(_))
        .ThenBy(_=>_.Name));

    // Remove all items from the DataGridView
    Appliances.Clear();

    // Put them back in with the new order.
    foreach (var appliance in tmp)
    {
        Appliances.Add(appliance);
    }
}

这“只是一种方法”,但我tested它和它的工作。希望这有助于让您更接近您的预期结果。

相关问题