Jump to content

C# - Storing multiple DataGridView Columns to string[]

BSpendlove

I am having bit of a muddle with something I am trying to clean up in an application. Just a bit of detail so the reader understands what I am trying to do:

 

The application pulls information from a MySQL Database and shows them in a DataGridView. My current method is working fine: When the user double clicks the specific cell, the information is saved as public variables so they can be used on another form without having to contact the database again. My approach currently is:

 

Spoiler

private void grid_components_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow comrow = this.grid_components.Rows[e.RowIndex];

                Public_Variables.component_id = comrow.Cells[0].Value.ToString();
                Public_Variables.component_stockid = comrow.Cells[1].Value.ToString();
                Public_Variables.component_manufacture = comrow.Cells[2].Value.ToString();
                Public_Variables.component_model = comrow.Cells[3].Value.ToString();
                Public_Variables.component_servicetag = comrow.Cells[4].Value.ToString();
                Public_Variables.component_condition = comrow.Cells[5].Value.ToString();
                Public_Variables.component_itemtype = comrow.Cells[6].Value.ToString();
                Public_Variables.component_infoa = comrow.Cells[7].Value.ToString();
                Public_Variables.component_infob = comrow.Cells[8].Value.ToString();
                Public_Variables.component_infoc = comrow.Cells[9].Value.ToString();
                Public_Variables.component_infod = comrow.Cells[10].Value.ToString();
                Public_Variables.component_ebayid = comrow.Cells[11].Value.ToString();
                Public_Variables.component_ebayprice = comrow.Cells[12].Value.ToString();
                Public_Variables.component_ebaylink = comrow.Cells[13].Value.ToString();
                Public_Variables.component_quantity = comrow.Cells[14].Value.ToString();
                Public_Variables.component_status = comrow.Cells[15].Value.ToString();
            }

            Single_Window___Grid_View.SingleWindow_Component window = new Single_Window___Grid_View.SingleWindow_Component();
            window.Show();
        }

 

 

Now, I believe I am close to what I want to achieve but trying to look for some guidance to where I am going wrong...

Spoiler

        private void grid_storage_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string[] storage_public_variables =
            {
                Public_Variables.storage_id, Public_Variables.storage_stockid, Public_Variables.storage_manufacture, Public_Variables.storage_model,
                Public_Variables.storage_servicetag, Public_Variables.storage_condition, Public_Variables.storage_capacity, Public_Variables.storage_firmware,
                Public_Variables.storage_rpm, Public_Variables.storage_cache, Public_Variables.storage_interface, Public_Variables.storage_ebayid,
                Public_Variables.storage_ebayprice, Public_Variables.storage_ebaylink, Public_Variables.storage_ebayquantity, Public_Variables.storage_ebaystatus
            };

            if (e.RowIndex >= 0)
            {
                DataGridViewRow storagerow = this.grid_storage.Rows[e.RowIndex];

                for (int i = 0; i < storage_public_variables.Length; i++)
                {
                    storage_public_variables[i] = storagerow.Cells[i].Value.ToString();
                }

                MessageBox.Show("Debug: \n\nStorage ID = " + Public_Variables.storage_id);
                
            }

            Single_Window___Grid_View.SingleWindow_Storage window = new Single_Window___Grid_View.SingleWindow_Storage();
            window.Show();
        }

 

I believe I am going wrong at:

 

for (int i = 0; i < storage_public_variables.Length; i++)
                {
                    storage_public_variables[i] = storagerow.Cells[i].Value.ToString();
                }

 

I'm going to put a link to my PC specs which actually aren't my PC specs and I cry myself to sleep everyday so I can have these PC specs but I can't afford these PC specs so PC specs PC specs PC specs PC specs PC specs PC specs.

Link to comment
Share on other sites

Link to post
Share on other sites

When you declare your storage_public_variables array you define it with values of Public_Variables variables, not pointer to them nor references, so when you assigning anything by storage_public_variables you just modify storage_public_variables array, not Public_Variables variable.

 

What type is Public_Variables? Maybe you can create array of strings that would be names of dictionary in same order as columns, and then assign cell to dictionary by such name.

 

            Dictionary<string, string> vars = new Dictionary<string, string>() { { "keyA", "beforeA" }, { "keyB", "beforeB" }, { "keyC", "beforeC" } };

            string[] keys = {"keyA", "keyB", "keyC"};
            string[] cells = {"cellA", "cellB", "cellC"};

            Console.WriteLine("keyA before: {0}", vars["keyA"]);

            for(int i = 0; i < keys.Length; i++) {
                vars[keys[i]] = cells[i];
            }

            Console.WriteLine("keyA after: {0}", vars["keyA"]);

It will output

keyA before: beforeA
keyA after: cellA

 

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Mr_KoKa said:

-snip-

Dohhh! Of course, my brain is playing stupid with the storage_public_variables!

 

Public_Variables is a class that contains just a ton of public strings which are then assigned at different parts in the application. (I created a seperate class for all my strings since I didn't want my main form full of tons of strings) Mainly it contains all the strings for when I double click a cell (I have many gridviews which show different tables in the database) and it saves all the DataGridView information

 

I would guess a MUCH better way than just creating mutliple strings in a separate class lol

 

Sorry, I can be a tiny bit dumb since programming isn't my strength (just created this application to improve the speed I work with inventory management).. What would the string[] keys be representing? Many thanks for the reply

I'm going to put a link to my PC specs which actually aren't my PC specs and I cry myself to sleep everyday so I can have these PC specs but I can't afford these PC specs so PC specs PC specs PC specs PC specs PC specs PC specs.

Link to comment
Share on other sites

Link to post
Share on other sites

Your keys would be component_id, component_stockid, component_manufacture in correct order so when you iterate trough keys/cells cell 0 will assign to component_id key.

 

There is a way of enclosure a variable inside of another object (class/struct), so when you pass your object by reference you can change it's value (without changing reference). But I would go with dictionary here.

Link to comment
Share on other sites

Link to post
Share on other sites

wouldn't it be better to store the data in xml then you can just pull the nodes you want as you need it?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, vorticalbox said:

wouldn't it be better to store the data in xml then you can just pull the nodes you want as you need it?

I wouldn't have a clue how well this would work, but I find it more convenient since when I go to another cell in the same category (example: storage) then everything will just be replaced (storage_id, storage_stockid, storage_manufacture), just makes it easier to access for me but maybe it might be easier

I'm going to put a link to my PC specs which actually aren't my PC specs and I cry myself to sleep everyday so I can have these PC specs but I can't afford these PC specs so PC specs PC specs PC specs PC specs PC specs PC specs.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, BSpendlove said:

I wouldn't have a clue how well this would work, but I find it more convenient since when I go to another cell in the same category (example: storage) then everything will just be replaced (storage_id, storage_stockid, storage_manufacture), just makes it easier to access for me but maybe it might be easier

actually xml might not be easier.

 

from what I get from this is if you go to another category does it need to pull from database again?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

18 minutes ago, vorticalbox said:

actually xml might not be easier.

 

from what I get from this is if you go to another category does it need to pull from database again?

nope, since when the user passes the login screen to the 'dashboard' (where I have tabControl, tab1 = Computer stock, tab2 = Laptop stock etc... each tab containing a gridview), everything from each category is pulled from the database before the dashboard loads then the connection closes so I don't have to really open a connection again until I add a new item/update an item. I thought that would secure it a tiny bit since now everything is stored 'locally'? (not that it is very sensitive information anyway)

 

So it pretty much creates a 'local database' (just grabs all the stock from database and stores it in all the gridviews) of our stock in the application (but you can refresh/update the gridviews if other users have updated/created more items)

I'm going to put a link to my PC specs which actually aren't my PC specs and I cry myself to sleep everyday so I can have these PC specs but I can't afford these PC specs so PC specs PC specs PC specs PC specs PC specs PC specs.

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, BSpendlove said:

nope, since when the user passes the login screen to the 'dashboard' (where I have tabControl, tab1 = Computer stock, tab2 = Laptop stock etc... each tab containing a gridview), everything from each category is pulled from the database before the dashboard loads then the connection closes so I don't have to really open a connection again until I add a new item/update an item. I thought that would secure it a tiny bit since now everything is stored 'locally'? (not that it is very sensitive information anyway)

 

So it pretty much creates a 'local database' (just grabs all the stock from database and stores it in all the gridviews) of our stock in the application (but you can refresh if other users have updated/created more items)

Right I get you now. Yeah xml wouldn't be better. My bad.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Erik Sieghart said:

Why on earth would you want to do that?
 

They gave you a DataTable man, which is like having a section of the database locally. I think you need to consult MDSN a lot more about the structures you're working with. You can use LINQ on them to do amazing things; putting them in lists or arrays for columns is clunky.

 

I recommend you push updates after the user presses a save button or something, only making the changes to the database once in a large batch.

Don't feel bad though; it's a problem we all run into when first starting with databases.

I've been suggested to just pass the the whole row of data from the table into the other form instead of storing them as variables... I guess I'll approach that method instead :)

 

 

I'm going to put a link to my PC specs which actually aren't my PC specs and I cry myself to sleep everyday so I can have these PC specs but I can't afford these PC specs so PC specs PC specs PC specs PC specs PC specs PC specs.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×