J'utilise ce code lors de la lecture à partir de SQL Server. Le problème est qu'il exécute la boucle autant de fois mes résultats SQL Server.Création de boutons dynamiques lors de la lecture à partir de SQL Server
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);
}
}
}
Mon résultat est ci-dessous:
Et mon résultat souhaité est:
Où dois-je placer ma boucle pour obtenir le résultat désiré? Ou devrais-je le casser d'une manière ou d'une autre?
Merci.
Egalement si je veux utiliser deux lignes ??
La boucle interne avec 'la variable col' exécute jusqu'à' NUM_COLS' pour chaque itération de la variable 'row'. Vous pouvez utiliser une seule boucle ici. – shahsani