Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to search a list of objects based on RowIndex value of a DataGridView?

I have a list of objects that are being displayed in rows on a DataGridView object in one form. Each row has a button that, when clicked, will show more details about that specific object from the list.

For my example, I have four objects in the list. When the first row is clicked, the details for the first object are displayed. However, the same details are shown for the second and third object. Finally, the fourth object shows details for the second object in the list.

When each object is created, they're given a DateTime value so that they will all be different and I figured that would be an easy way to find them.

Here's the code for the button click event:

private void GridDataView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        var senderGrid = (DataGridView)sender;

        if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
            e.RowIndex >= 0)
        {
            objectListing clickEvent = currentSearch.Find(x => x.listDate.Contains(e.RowIndex.ToString()));

            listInfo showItem = new listInfo(click.listDate); // Form to show details
            var result = showItem.ShowDialog();
            if (result == DialogResult.OK)
            {
                showItem.Close();
            }
            else
            {
                showItem.Close();
            }
        }
    }

At the beginning of the event there is some code to assemble the list of objects from file. But as this exact code works in all other parts of the program, that shouldn't be the problem. I've omitted those lines to keep the example shorter.

Comments