0

I have a program that automatically generates a 2D grid of buttons and stores the grid in a nested list and I am trying to export this list to MS Excel. however the code that I am trying throws to many errors. I can get this working without using a list but I need to use a nested list in order to clear the list a populate it again if and when the size of the grid increases or decreases. Is the logic that I'm using even doable

As follows:

        //This is not the complete code
        List<List<Button>> buttonss = new List<List<Button>>();
        List<Button> rowList = new List<Button>();

        //Some method that creates a grid of buttons 
        buttons = new Button[row][];
        for (int r = 0; r < row; r++)
        { 
            buttons[r] = new Button[col];
            buttonss.Add(rowList);    
            for (int c = 0; c < col; c++)
            {
                buttons[r][c] = new Button();
                rowList.Add(buttons[r][c]);
            }
        }

The next thing that I want to do is ot export this list into excel.

The grid: enter image description here

Button:

//Export to MS Excel button
private void btnExport_Click(object sender, EventArgs e)
{
    ep.Do("sheet.xsl", rowList);//(ERROR 0)
}

Class:

//Export class
public void Do(string excelName, System.Collections.Generic.List<Button[][]> Grid)
{
    for (int i = 0; i <= Grid.Count(); i++)
    {
        for (int j = 0; j <= Grid[i].Count(); j++)
        {

            AddData(i, j, Grid[i][j]);//(ERROR HERE [1])

        }
    }
    //app.SaveWorkspace(excelName);
}

public void AddData(int row, int col, System.Collections.Generic.List<Button[][]> button)
{
    if (button == null) return;
    row++;
    col++;

    Range range = worksheet.Cells[row + 2, col + 2];
    if (!defaultBackgroundIsWhite)
    {
        range.Interior.Color = button.BackColor.ToArgb();//(ERROR HERE[2])
    }
    else
        range.Interior.Color = button.BackColor.Name != "Control" ? button.BackColor.ToArgb() : System.Drawing.Color.White.ToArgb();//(ERROR HERE)
    // range.NumberFormat = "";
    worksheet.Cells[row + 2, col + 2] = button.Text;//(ERROR HERE[3])
    row--;
    col--;
}

Errors: 0:Argument 2: cannot convert from 'System.Collections.Generic.List' to 'System.Collections.Generic.List' C:..

1:The best overloaded method match for 'SmartRota.ExportHeadWaiter.AddData(int, int, System.Collections.Generic.List)' has some invalid arguments C:..

2:Error 3 'System.Collections.Generic.List' does not contain a definition for 'BackColor' and no extension method 'BackColor' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?) C:..

3: Same eroor as above

0

1 Answer 1

2

Well there is quite a few problems with your code, mostly the Type of the parameters your functions receive.

For instance: 1 . rowList is a List<Button> and you're passing it to the function Do() yet, the function Do expects a List<Button[][]>

2 . To make things worse, AddData expects to receive an array of Buttons yet the entire code inside AddData considers you having only one button and not an array.

3 . Calling ToArgb() return as int, yet you're trying to put it in a Color

Without trying to really understand what you're trying to do, I'm guessing this is how you want to declare your functions:

public void Do(string excelName, System.Collections.Generic.List<Button[]> Grid)

and:

public void AddData(int row, int col, Button button)
Sign up to request clarification or add additional context in comments.

7 Comments

Any idea how I can sort this out
Well start by correctly declaring your functions' parameters. Then try to understand why you're trying to Pass a List<Button> to the Do function which should probably expect a List<Button[]> (based on your supplied code).
I corrected that but its say that AddData(i, j, hwGrid[i][j]); and ep.Do("sheet.xsl", rowList); has some invalid arguments
About AddData, it shouldn't write that if you what I told you. About Do(..., rowList) - just as I said, you're passing a List<Button> instead of a List<Button[]>
Do you mean I need to make rowlist into an array
|

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.