0

I have an array of dataGridViews created programmatically in one method. how Can I access the array from another method? (in this case Button Click). I can't declare it as member of class because I don't know how big will array be because it depends on file

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

    public void Form1_Load(object sender, EventArgs e)
    {
        string[] lines = System.IO.File.ReadAllLines(@"..\..\Diets.txt");

        int diet_num = 0;
        int grid_num = 0;
        foreach (string x in lines) diet_num++;
        grid_num = (diet_num / Constant.DATAGRID_DIETS_IN_GRID) + 1;
        Font dataGridFont = new Font("Microsoft Sans Serif",12, FontStyle.Bold);
        Color c = Color.FromArgb(255,255,128);

        DataGridView[] grid = new DataGridView[grid_num];
        for (int i = 0; i < grid_num; i++)
        {
            grid[i] = new DataGridView();
            grid[i].Tag = i;
            grid[i].Parent = this;
            grid[i].Location = new Point(12, 12 + (8 + Constant.DATAGRID_ROW_HEIGHT * 2) * i);
            grid[i].Visible = true;
            grid[i].RowHeadersVisible = false;
            grid[i].Height = Constant.DATAGRID_ROW_HEIGHT * 2;
            grid[i].Width = Constant.DATAGRID_COLUMN_SIZE * Constant.DATAGRID_DIETS_IN_GRID + 3;
            grid[i].UserAddedRow += Form1_UserAddedRow;
            grid[i].RowTemplate.Height = Constant.DATAGRID_ROW_HEIGHT;
            grid[i].ColumnHeadersHeight = Constant.DATAGRID_ROW_HEIGHT;
            grid[i].AllowUserToResizeColumns = false;
            grid[i].AllowUserToResizeRows = false;
            grid[i].MultiSelect = false;
            grid[i].ColumnHeadersHeightChanged += Form1_ColumnHeadersHeightChanged;
            grid[i].MouseLeave += Form1_MouseLeave;
            grid[i].Font = dataGridFont;
            grid[i].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid[i].ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            grid[i].ColumnHeadersDefaultCellStyle.BackColor = c;
            grid[i].EnableHeadersVisualStyles = false;

        }
        this.Width = Constant.DATAGRID_COLUMN_SIZE * Constant.DATAGRID_DIETS_IN_GRID + 40;

        int count = 0;
        foreach (string x in lines)
        {
            DataGridViewColumn col = new DataGridViewTextBoxColumn();
            col.Width = Constant.DATAGRID_COLUMN_SIZE;

            col.HeaderText = x;
            int colIndex = grid[count/12].Columns.Add(col);
            count++;
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        grid[0].Visible = false; //can't do that
    }
}
1
  • " how Can I access array from another method?" Not at all, as your array lives only within that method. You´d have to make a member within your class that holds the gridview-instances. Commented May 26, 2017 at 10:17

2 Answers 2

1

You can move grid to class scope:

public partial class Form1 : Form
{
    DataGridView[] grid = null;

    public void Form1_Load(object sender, EventArgs e)
    {    
        ...
        grid = new DataGridView[grid_num];
        ...
    } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

It must either be a variable in scope for both functions (it can still be one function that sets it), or you have to hand it as a parameter for the 2nd Function (only works if function 1 calls function 2).

"I can't declare it as member of class, because i don't know how big will array be, because it depends on file" That sentence makes no sense for me. The array will always take the same amount of storage, no mater where you reference it. You obviously need it in both functions. So it must be avalible for both functions.

What exactly is the problem you are trying to avoid by not making it a class member?

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.