Click here to Skip to main content
15,887,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My problem is, when I import a csv file to datagridview, then the binary search is working very well. For the second time as well. After the third time, the binary-search is not working well. It does not finds any object from the ArrayList.
I use Windows application for this project.

What I have tried:

Adding data to the ArrayList after csv is imported:
ListOfPeople.Add(Something.Trim());


Converting datagridview values to ArrayList before search in binary:
foreach (var row in dataGridView2.Rows.Cast<DataGridViewRow>())
{
    ListOfPeople[row.Index] = row.Cells[0].Value.ToString().Trim();
}

ListOfPeople is the ArrayList name.

Binary Search Code:
int index = this.ListOfPeople.BinarySearch(textBoxBinarySearch.Text);
if (index != -1)
{
        dataGridView2.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
        dataGridView2.Rows[index].Selected = true;
        dataGridView2.CurrentCell = dataGridView2.Rows[index].Cells[0];
        MessageBox.Show("Index is equal to: " + index, "Binary Search");

        textBoxBinarySearch.Clear();
    }
Posted
Comments
[no name] 1-Sep-18 16:44pm    
Why do you keep "importing"? Do you "clear" before each import? Or just keep "adding"?
Galarist_00 2-Sep-18 2:08am    
I have to import file. I fixed my problem by adding ArrayList.Clear(); before import another csv file, if I clear the datagridview.

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