0

So its a bit of a long question I am running into a severe problem where I have a windows form C# application that is a bicycle inventory management system for one of my college classes. I have figured out how to remove a currently selected 'Part' from the datagridview by building the code inside of a button_click event, but that is for that specific form and my class wants me to build a specific deletePart bool method taking a Part parameter that includes ID, name, price, inStock, min, and max.I have not been able to find a way to transfer the code from the button_click event to the deletePart method and would like some assistance.

public static bool deletePart(Part deletedPart)
   {
        MainScreen mainScreen = new MainScreen();
        bool isDeleted = false;
        if (MessageBox.Show("Are you sure you want to remove this part?", "Remove Part", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            isDeleted = true;
        }
        if (isDeleted == true)
        {
            mainScreen.PartsGridView.Rows.RemoveAt(mainScreen.PartsGridView.CurrentRow.Index);
        }
        return isDeleted;
   }

This is all I have inside my button_click Event

private void DeleteBtn_Click(object sender, EventArgs e)
    {

        if (MessageBox.Show("Are you sure you want to remove this part?", "Remove Part", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            PartsGridView.Rows.RemoveAt(PartsGridView.CurrentRow.Index);
        }
    }

Any advice on how to build the deletePart method would be helpful I am so lost.

Dathon Weber
  • 53
  • 2
  • 8
  • Not Really the method being a boolean is what is really confusing me but I do not know how to word my questions very well. – Dathon Weber Feb 09 '20 at 10:08
  • Hmm, so what is your current problem with `deletePart` method ?! – Hamed Moghadasi Feb 09 '20 at 10:16
  • I do not know if I am using the boolean part of the method correctly with my current code, and it wants me to pass a Part as a parameter into this method to be deleted but I do not know how to create a Part that consists of all the current data that is selected in the DataGridView. – Dathon Weber Feb 09 '20 at 10:25
  • It is difficult to understand what you are asking. From you comment… _”it wants me to pass a Part as a parameter into this method to be deleted”_ I assume “Part” is a “CLASS” with some properties. Therefore, if each row in the grid contains a “Part”’s properties, then create a new “Part” object to delete from the row in the grid. I would assume each cell in the grids row is a property of a “Part” object. Simply grab the cell values and make a new “Part” object to pass to your delete method. – JohnG Feb 10 '20 at 01:20
  • The `deletePart` method looks odd in a sense that the code appears to be deleting the “Current” row in the grid. The method is “TAKING” (passing in) a `Part` object (`deletePart`), however this `deletePart` object is NEVER used. In addition, how would you know that the `Part` passed in is the “CURRENT” row in the grid? I would guess that you need to search for the passed in `Part` and delete THAT row, which may not necessarily be the “Current” row in the grid. This is unclear. – JohnG Feb 10 '20 at 01:20
  • Yes Part is a class and it contains an ID, Name, Price, InStock, Min, and Max Properties. How do I grab the current Cell Values and pass them into a created Part? – Dathon Weber Feb 10 '20 at 03:25
  • 1
    @DathonWeber - for example Part.Name = dataGridView1.CurrentRow.Cells[0].Value.ToString(); (datagridview is 0-based, means 0 is Your first collumn) – Kuba Do Feb 10 '20 at 09:55

0 Answers0