-1

I'm using this code while reading from SQL Server. The problem is that it runs the loop as many times are my SQL Server results.

SqlCommand sqlCmd = new SqlCommand("Select Name from TablesDesigner", con);

SqlDataReader sqlReader = sqlCmd.ExecuteReader();

while (sqlReader.Read())
{
    for (int row = 0; row < NUM_ROWS; row++)
    {
        TableRow tablerow = new TableRow(this);

        TableLayout.LayoutParams linearLayoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MatchParent, TableLayout.LayoutParams.MatchParent, 1.0f);
        tablerow.LayoutParameters = linearLayoutParams;
        table.AddView(tablerow);

        for (int col = 0; col < NUM_COLS; col++)
        {
            int FINAL_COL = col;
            int FINAL_ROW = row;

            Button btn = new Button(this);
            TableRow.LayoutParams linearLayoutParams2 = new TableRow.LayoutParams(TableRow.LayoutParams.MatchParent, TableRow.LayoutParams.MatchParent, 1.0f);
            btn.LayoutParameters = linearLayoutParams2;
            btn.Text = sqlReader["Name"].ToString();
            tablerow.AddView(btn);
        }
    }
}

My result is below:

enter image description here

And my desired result is:

enter image description here

Where should I place my loop for getting the desired result? Or should I break it somehow?

Thanks.

Also what if I want to use two rows??

1
  • The inner loop with col variable is executing upto NUM_COLS for each iteration of row variable. You may use only one loop here. Commented Oct 17, 2017 at 0:25

1 Answer 1

1

When confronted with a difficult problem, break it down into manageable bits (this is good coding practice anyway).

First, get the data you need into a single list. Don't forget to Dispose your DataReader.

public List<string> GetButtonNames()
{
    var buttonNames = new List<string>();
    SqlCommand sqlCmd = new SqlCommand("Select Name from TablesDesigner", con);
    using (var sqlReader = sqlCmd.ExecuteReader())
    {
        while (sqlReader.Read())
        {
            buttonNames.Add(sqlReader["Name"].ToString());
        }
    }
    return buttonNames;
}

Then write a function to organize it into a 2D list. I stole the logic for this from this question.

public static List<List<string>> Make2DList(List<string> input, int width=4)  
{        
    var output = new List<List<string>>(); 

    for (int i=0; i < input.Count; i+= width) 
    { 
        output.Add(input.GetRange(i, Math.Min(width, input.Count - i))); 
    } 

    return output; 
} 

Now you have a list of lists. The "outer" list corresponds to each table row. The inner list is a list of column values within that row.

Now all you need is code to make it into a table. Because we organized the data into a grid already, we can use normal foreach syntax which makes it very easy.

public void RenderButtonTable(List<List<string>> names)
{
    var layout = new TableLayout.LayoutParams
        (
            TableLayout.LayoutParams.MatchParent,
            TableLayout.LayoutParams.MatchParent, 
            1.0f
        );
    tablerow.LayoutParameters = layout;

    foreach (var row in names)
    {
        TableRow tablerow = new TableRow(this);
        tablerow.LayoutParameters = layout;
        table.AddView(tablerow);
        foreach (var col in row)
        {
            Button btn = new Button(this);
            btn.Text = col;
            tablerow.AddView(btn);
        }
    }
}

Put it all together:

void CreateDynamicButtonsWhileReadingFromSQLServer()
{
    var buttonNames = GetButtonNames();
    var gridData = Make2DList(buttonNames, NUM_COLS);
    RenderButtonTable(gridData);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much it works exellent!!! But where should i place a btn.Click += (sender, e) => { Toast.MakeText(this, btn.Text, ToastLength.Short); }; For taking Button click name?
How can i handle button click event for each of my buttons?

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.