Avatar of JJGarc1a
Flag for Mexico asked on

Fill an array from a datagridview

Hi Everyone,
     I have a main form made to enter observations, on the form you select from a combobox the employee name who made the observation, but I have been requested to add an option to add more employees to an observation, there can be 8 or 10 employees who somehow made the same observation, so it's necesary to know how many more did this.

So, I added a button, once clicked another form shows up, this form has a datagridview with a combobox colum, now, what I need is that once the employees have been added, to press a button and save those employees to a table.

what I need is to display those names there until the user presses the save button the values will be saves into a database, I thought on an array to read and store the values from the Datagridview, but honestly, I don't know how to do this, can anyone help me?
Visual Basic.NET

Avatar of undefined
Last Comment

8/22/2022 - Mon

The array will be saved on the main form


I don't understand completely what your current situation is. To verrify: You have a second form, showing a grid with comboboxes. Does this mean that someone adds a row to the grid for every employee they want to add? Then, when closing that form you want to show the current selection in the main form and only when save is clicked that selection is actually saved. How are those values saved? Are they id's to some employees table, are they the names, are the values stored in a seperate table or in some delimited values list?

Anyways, you could choose to create virtual table (System.Data.Datable) with columns to store the selected employees in. Then you bind this table to the grid and all the data is stored in the table. Next, when closing the form you write the contents of the table to an xml string (datatable.WriteXml) which you can store in your backend database when the user decides to save the data. The same string can also be used to repopulate the datatable (and this the grid) when the second form is reopend (datatable.ReadXml)

I hope this puts you some sort of direction but I can only really help you out if I have a better understanding of what you are trying to accomplish.


Sorry, my mistake, Yes, I have a second form which comes up once the user click on a button to add more employees to the observation, the second form has a datagridview with a combobox where you can choose the employee name, per every different employee is to be a new row added manually by the user, the data stored is the employee id_number.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck

I presume that employees that were already added to the obervation before have to  be shown in the grid aswell.
What I still don't know is how you will store the id's of the employees that have been added. Are they stored in a seperate n:n table in the database or are they stored in a single column for that observation.
From your first explanation the last one seems the case which means you would have to loop through all the rows in grid and concatenate the id value of each row with a dilimiter (e.g. ;) The result string can then be stored in the database.
When you reload the form you will still need to find a way to repopulate the grid from the array

The code snippet show how you can go through the grid and create a resulting string of ;-dilimited employee id's. If it you do want an array, then just leave out the string.join in the end.

Public Function CreateArrayFromGrid() As String
        Dim al As New ArrayList()
        For Each r As DataGridViewRow In dgv.Rows
        Dim result As String = String.Join(";", al.ToArray(GetType(String)))
        Return result
    End Function

Open in new window


Ok, the idea is to 'keep' the records in the array until the record from Form1 is saved, once this record is saved it will generate a Key, with this key I want to insert these records(records from array on form2) in another table that will include Record_ID, Employee_Number.


View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

Ok, can you explain me how could I use a datatable for this matter knowing the fact that I still don't have a Key generated on form1 to save the data entered on form2?, thanks a lot for your patience!
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.