Home > Software engineering >  How to delete an item row in C# in a 2D array in forms
How to delete an item row in C# in a 2D array in forms

Time:06-14

Hello I was wondering how can I delete a row from the array in C# forms using a button, like you write which line# you want to delete and then pressing the button will delete that item from the list. The idea is to delete a row using "button 2" that reads the number written in a "textbox". And also how to show a warning message if the row doesn't exist. I cant use the "list<>" or "grid thing". Thanks if any of you have a little hand to lend me. ` public partial class Form1 : Form {

    int pos = 0;
    int counter = 0;
    string[,] sales = new String[50, 5];

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        string product;
        double price, subtotal, total;
        int qty;

        textBox1.BackColor = Color.White;
        if (textBox1.Text == "")
        {
            textBox1.BackColor = Color.Red;
            textBox1.Focus();
            MessageBox.Show("MISSING PRODUCT!!!");
            return;
        }
            textBox2.BackColor = Color.White;
            if (textBox2.Text == "")
        {
            textBox2.BackColor = Color.Red;
            textBox2.Focus();
            MessageBox.Show("MISSING PRODUCT!!!");
             return;
        }
            textBox3.BackColor = Color.White;
            if (textBox3.Text == "")
        {
            textBox3.BackColor = Color.Red;
            textBox3.Focus();
            MessageBox.Show("MISSING PRODUCT!!!");
             return;
        }


        product = textBox1.Text;
        price = double.Parse(textBox2.Text);
        qty = int.Parse(textBox3.Text);
        subtotal = price * qty;

        sales[pos, 0] = pos.ToString();
        sales[pos, 1] = product;
        sales[pos, 2] = price.ToString();
        sales[pos, 3] = qty.ToString();
        sales[pos, 4] = subtotal.ToString();

        pos  ;

      
        textBox5.Text = "";
        total = 0;
        for (int i = 0; i < sales.GetLength(0); i  )
        {

            textBox5.Text  = sales[i, 0]   "      "   sales[i, 1]   "      "   sales[i, 2]   "      "   sales[i, 3]   "      "   sales[i, 4]   "      "   "\r\n";
            //<>
            if (sales[i, 4] != null)
            {
                total  = int.Parse(sales[i, 4]);
            }

        }
             textBox4.Text = total.ToString();
            
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox1.Focus();

    }

    private void button2_Click(object sender, EventArgs e)
    {
          
       // what to put here

    }
}

`

CodePudding user response:

Context

I'm going to give you two answers here as you aren't clear on whether or not you are required to use an array instead of a List<>. I'm also a little unsure about what exactly you're asking for, as you haven't given any examples of what you hope the output would be, or anything like that, so I apologize if I totally miss the mark. Just let me know and I'll change my answer to match what you're asking more appropriately :)

Using an array

private void button2_Click(object sender, EventArgs e)
{
    //Checks to see if the desired row to delete exists
    if(sales[pos][0] is null)
    {
        //Lets the user know the index is already empty
        MessageBox.Show("Index has no product listed.");
        return;
    }
    
    //Loops through each item at the pos index
    for(int i = 0; i < sales.GetLength(1); i  )
    {
        //remove
        sales[pos][i] = null;
    }

    /*******************************************************
    * I'm unsure if you would want to drop the index of all
    * of the arrays with a larger index number down one, so
    * this next bit of code will do this, but if you don't
    * want this functionality, just ignore this.
    ********************************************************/

    //Loops through every index from pos to the second to last index
    for(int i = pos; i < sales.GetLength(0) - 1; i  )
    {
        //Loops through every index of a product
        for(int j = 0; j < sales.GetLength(1); j  )
        {
            //Sets current row to the value in the next
            sales[i][j] = sales[i 1][j];
        }
    }
}

Using a List<>

/*****************************************************
* I've changed the name of the variable to pascal case
* as this is Microsoft's documentation standard.
* This also shows how you would need to change the 
* declaration of the Sales object.
*****************************************************/
List<List<string>> Sales = new List<List<string>>();

private void button2_Click(object sender, EventArgs e)
{
    //Checks if the index is out of bounds of the list.
    if(pos > Sales.Count() - 1)
    {
        //Lets the user know the index out of bounds
        MessageBox.Show("Index is out of bounds.");
        return;
    }
    
    //Remove the product at the selected index
    Sales.RemoveAt(pos);
}

Products

I notice that you seem to be trying to make a list of products to be sold. I would recommend that you create a Product class, which holds all of the information that a product has. A quick mock up of that could look something like this:

public class Product 
{
    /**********************************************************
    * If you are unfamiliar with how properties work, 
    * I recommend looking into them more, but quick, simplified
    * explanation is they are just variables you access from
    * other classes that have access to this one by
    * saying, in this case, Product.AttributeOne, and you
    * can even set it by writing Product.AttributeOne = "foo";
    **********************************************************/
    
    public string AttributeOne { get; set; }
    public string AttributeTwo { get; set; }
    public string AttributeThree { get; set; }
    public string AttributeFour { get; set; }
    public string AttributeFive { get; set; }
    
}

Using this class could simplify other code you have, and make your array/list one dimensional. I would highly recommend making products an object, just as it would give you more functionality for a lot less code. I'll show you how the code for button1_Click and button2_Click() would look like with this change

public partial class Form1 : Form
{

    int pos = 0;
    int counter = 0;
    List<Product> Sales = new List<Product>();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        string product;
        double price, subtotal, total;
        int qty;

        textBox1.BackColor = Color.White;

        if (textBox1.Text == "")
        {
            textBox1.BackColor = Color.Red;
            textBox1.Focus();
            MessageBox.Show("MISSING PRODUCT!!!");
            return;
        }

        textBox2.BackColor = Color.White;

        if (textBox2.Text == "")
        {
            textBox2.BackColor = Color.Red;
            textBox2.Focus();
            MessageBox.Show("MISSING PRODUCT!!!");
            return;
        }

        textBox3.BackColor = Color.White;

        if (textBox3.Text == "")
        {
            textBox3.BackColor = Color.Red;
            textBox3.Focus();
            MessageBox.Show("MISSING PRODUCT!!!");
            return;
        }


        product = textBox1.Text;
        price = double.Parse(textBox2.Text);
        qty = int.Parse(textBox3.Text);
        subtotal = price * qty;

        Sales.ElementAt(pos).AttributeOne = pos.ToString();
        Sales.ElementAt(pos).AttributeTwo = product;
        Sales.ElementAt(pos).AttributeThree = price.ToString();
        Sales.ElementAt(pos).AttributeFour = qty.ToString();
        Sales.ElementAt(pos).AttributeFive = subtotal.ToString();

        pos  ;

  
        textBox5.Text = "";
        total = 0;

        for (int i = 0; i < Sales.Count; i  )
        {

            textBox5.Text  = Sales.ElementAt(i).AttributeOne   "      "   Sales.ElementAt(i).AttributeTwo   "      "   Sales.ElementAt(i).AttributeThree   "      "   Sales.ElementAt(i).AttributeFour   "      "   Sales.ElementAt(i).AttributeFive   "      "   "\r\n";
            //<>
            if (Sales.ElementAt(i).AttributeFive != null)
            {
                total  = int.Parse(sales[i, 4]);
            }

        }

        textBox4.Text = total.ToString();
            
        textBox1.Text = "";
        textBox2.Text = "";
        textBox3.Text = "";
        textBox1.Focus();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        //Checks if the index is out of bounds of the list.
        if(pos > Sales.Count() - 1)
        {
            //Lets the user know the index out of bounds
            MessageBox.Show("Index is out of bounds.");
            return;
        }
        
        //Remove the product at the selected index
        Sales.RemoveAt(pos);
    }
}
  • Related