-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??

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
dSaos
  • 13
  • 5
  • The inner loop with `col` variable is executing upto `NUM_COLS` for each iteration of `row` variable. You may use only one loop here. – shahsani Oct 17 '17 at 00:25

1 Answers1

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);
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • 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? – dSaos Oct 17 '17 at 08:18
  • How can i handle button click event for each of my buttons? – dSaos Oct 17 '17 at 09:15
  • [Link](https://stackoverflow.com/questions/6187944/how-can-i-create-a-dynamic-button-click-event-on-a-dynamic-button) – John Wu Oct 17 '17 at 09:46