-3

so I here is my code: I have 2 forms, - abutton in form1 takes you to form2 . -form2 has Datagridview in it - you enter the informations(name, age) in form 1 and then you load them in a datagridview in form2 -when I chose a row to delete fom datagrid view I want that row to be deleted too from the array.(how can I do it) thankyou in advance

class Class1
{
   public struct client
    {
        public string nom;
        public string prenom;
        public int age;
    }

   public static client[] TC = new client[100];
   public static int i = 0;
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
    }

    private void btn_ajouter_Click(object sender, EventArgs e)
    {

        Class1.TC[Class1.i].nom = textBox_nom.Text;
        Class1.TC[Class1.i].prenom = textBox_prenom.Text;
        Class1.TC[Class1.i].age = int.Parse(textBox_age.Text);

        textBox_age.Clear();
        textBox_nom.Clear();
        textBox_prenom.Clear();

        Class1.i = Class1.i + 1;
    }
}


public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void btn_afficher_Click(object sender, EventArgs e)
    {
        dataGridView1.Rows.Clear();
        for (int j = 0; j <= Class1.i-1; j++)
        {
            dataGridView1.Rows.Add(Class1.TC[j].nom,Class1.TC[j].prenom,Class1.TC[j].age);
        }
    }

    private void btn_supprimer_Click(object sender, EventArgs e)
    {
        dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
    }
}
1
  • 1
    What's your question? Where exactly are you stuck? How have you tried to remove an element from the array? (The proposed duplicate question has some good solutions for that.) Commented Jan 11, 2016 at 0:19

3 Answers 3

4

Here's an extension method to remove an item from an array:

public static T[] RemoveAt<T> (this T[] arr, int index) {
    return arr.Where ((e, i) => i != index).ToArray ();
}

Since arrays are immutable in C#, you can't actually remove an element from an array. The extension method returns a new array where the specified element is removed, so you should call it like that: myarr = myarr.RemoveAt (index);

Sign up to request clarification or add additional context in comments.

9 Comments

This doesn't "remove" anything from the array, it creates a second array without that element, so everytime you'd get a new array.
where to put that code? because I have a a delete button that deletes rows from datagridview
@CharafB.: This code would go in a static class for extension methods: msdn.microsoft.com/en-us/library/bb383977.aspx
I'm sorry I can't understand your code, I'm a beginner and I don't know how to use you code
@CharafB. Create a static class, add using System.Linq; to the top of the file and paste the code into the class. You then will be able to use it like that: myarray = myarray.RemoveAt (index); @RonanThibaudau Edited the question to clarify that
|
2

You can't "remove" from an array, an array's size is fixed. You create an array of 100 clients there will always be 100 clients, you should be using a List instead and use the add / remove methods on it to change it's elements.

2 Comments

good, but would you write the code for me please, I showed what I did above
No, this answers your question but you need to write your own code, as i said rewrite this class by removing the array and replacing it with a List<client> instead it does everything an array does but is also modifiable.
-1

You can instead create new list the same as your array like this

var yourList = yourArray.ToList();

yourList.Remove(someValue)
// to remove a specific value from your array

 yourList.RemoveAt(someIndex)
// to remove value from specific index.

6 Comments

And exactly how would this modify the array?
This doesn't remove from the array at all, it creates a new list and removes from that new list, the array is completely unaffected and since you're never storing the list itself neither is anything, this is basically a funtional no operation except that it allocates memory.
okay; and after removing from the list ; that doesn't remove fom the array, I need a new array that doeszn't contain that element I removed.
thakyou for your answer but it's not finished yet as I said
just use the list you can also later do yourList.ToArray() and use it like array if you really need an array so badly
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.