0

I am working on a program that contains multiple DataGridViews on multiple tab controls. My DataGridViews have a lot of initial formatting done to them at runtime. For example, row 0 and 1 are my first set of "headers" which are read only cells with color and font formatting. Rows 2 and 3 are for data entry with color coding based on values entered. Then, this row organization repeats for rows 4, 5, 6, and 7, then, so on.

I do not want to have to repeat all the setup and formatting code for all the other DataGridViews. Is there a way to create an array of DataGridViews so that I can loop the setup and formatting code?

DataGridView[] subFrames = new DataGridView[16];

The above compiles, but how does one use this? I cannot name a DataGridView control on my form subFrame[0]. Do I have to create the control and define placement, etc in code to do this? Or is there another way?

6
  • Are your DataGridViews themselves static (and simply numerous), or are they dynamically generated somehow? Commented Jun 12, 2015 at 18:43
  • Dynamically generated at run time during program initialization. I had to do this to control each rows attributes as some rows are read only with formatting, and others are not. They are not bound in any way and simply contain user input. Commented Jun 12, 2015 at 18:49
  • In that case, what is generating them? Presumably that code can simply put them in a class-level array, like the one you've shown in the question? What's stopping the code which generates a DataGridView from adding that instance to the array? Commented Jun 12, 2015 at 18:50
  • If I understand correctly, you are saying add the control on my form to the array i created? subFrame[0] = <form DGV control>; Or something like that? If that lets me manipulate the form control instance from subFrame[0], that sould work! Commented Jun 12, 2015 at 18:57
  • 1
    It should work, sure. Unless there's another detail somewhere else in the code that we're not aware of, naturally. The DataGridView instances are objects like any other objects in C#. You can create an array and just set them as the elements of that array for quick access to them. Commented Jun 12, 2015 at 19:02

1 Answer 1

1

If you're generating the DataGridViews dynamically then you can use exactly the approach you propose. Instances of controls on a form are objects in C# like any other, so you can hold a reference to those objects in an array.

Not knowing much of your code, this is a bit of a contrived example. But consider this pattern:

public class Form1
{
    private DataGridView[] subFrames = new DataGridView[16];

    // other code

    private void BuildGrids()
    {
        this.DataGridView1 = BuildFirstGrid();
        subFrames[0] = this.DataGridView1;
        // continue for the rest of the grids
    }

    private void StyleGrids()
    {
        foreach (var grid in subFrames)
            ApplyStyling(grid);
    }
}

The code you're looking to consolidate for this specific question would be in ApplyStyling(), essentially transforming each grid as you describe.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.