开发者

Search function on listview

开发者 https://www.devze.com 2022-12-27 20:28 出处:网络
I am altering the question now! I have the search working using the following: //check the array for a match

I am altering the question now!

I have the search working using the following:

//check the array for a match
        foreach (Delivery d in mainForm.myDeliveries)
        {
            if (d.Del开发者_运维百科iveryName == searchValue)
            {
                ListViewItem item = lstDeliveryDetails.Items.Add(d.DeliveryName);
                item.SubItems.Add(d.DeliveryAddress);
                item.SubItems.Add(d.DeliveryDay);
                item.SubItems.Add(d.DeliveryTime);
                item.SubItems.Add(d.DeliveryMeal);
                item.SubItems.Add(d.DeliveryInstructions);
                item.SubItems.Add(d.DeliveryStatus);

            }
        }

My question is, what is the best way to now search all the other values, to make a complete search? I know i could do if else statements and go through address, day, time, etc.

Is there a better way to acheive this? And at the moment, the user has to enter the entire name to get the result, can i make it if they only enter a part of the name?


Clear all items from your list view and only add the ones that match the search criteria:

lstDeliveryDetails.Clear();
for (int i = 0; i < myDeliveries.Count; i++)
{
    Delivery d = (Delivery)mainForm.myDeliveries[i];

    if (d.DeliveryName == searchValue)
         lstDeliveryDetails.Items.Add(d.DeliveryName);
}


Change your display method such that it accepts a Delivery list argument (or array) like this

private void displayDeliveries(List<Delivery> deliveries)

and change its definition accordingly

In your btnSearch get the list of selected deliveries in List and pass into the displayDeliveries method

While initialising your listView send mainForm.myDeliveries as an argument

Update

Get the keyword from the user and check like this

if (d.DeliveryName.Contains(searchValue))

You can also try incremental search by handling the TextChanged event of textbox if the list is small.

0

精彩评论

暂无评论...
验证码 换一张
取 消