Comment faire un tableau en c#

BAHROUNRABII Messages postés 20 Date d'inscription jeudi 7 août 2008 Statut Membre Dernière intervention 29 novembre 2008 - 8 oct. 2008 à 17:46
cs_Robert33 Messages postés 834 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 14 janvier 2017 - 12 mars 2011 à 11:17
Je debute en c# et je suis sous VS 2008. J'aimerai faire un tableau de 8 ligne et 7 colonnes ,et chaque colonne représente un petit tableau de 3 ligne et 4 colonnes.est ce que c'est possible et comment je peutle faire?
Merci beaucoup.

3 réponses

louloujedi Messages postés 1 Date d'inscription vendredi 11 mars 2011 Statut Membre Dernière intervention 11 mars 2011
11 mars 2011 à 23:03
je suis aussi d'ebutant en c# et jaimerais savoir comment inserer des elements(string,int,float.etc) dans un tableau et de pouvoir les afficher.(merci)
0
cs_Robert33 Messages postés 834 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 14 janvier 2017 33
12 mars 2011 à 11:17
Bonjour

Pour la création des tableaux, les liens de xmox667 donnent suffisament d'informations.
pour l'affichage sur un formulaire, on peut utiliser un datagridview ou des TableLayoutPanels

exemple de création dynamique:
private void BuildTableLayoutPanel()
{
    this.SuspendLayout();

    //Création d'un tableau principal de 8 lignes par 7 colonnes
    TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
    tableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
    
    tableLayoutPanel.ColumnCount = 7;
    for (int i = 0; i < tableLayoutPanel.ColumnCount; i++)
        tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100f / tableLayoutPanel.ColumnCount));

    tableLayoutPanel.RowCount = 8;
    for (int i = 0; i < tableLayoutPanel.RowCount; i++)
        tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100f / tableLayoutPanel.RowCount));
    
    //dans chaque cellule du tableau, on ajoute un sous tableau de 3 lignes par 4 colonnes
    for (int c = 0; c < tableLayoutPanel.ColumnCount; c++)
        for (int r = 0; r < tableLayoutPanel.RowCount; r++)
        {
            //Création d'un sous tableau de 3 lignes par 4 colonnes
            TableLayoutPanel subTableLayoutPanel = new TableLayoutPanel();
            subTableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            
            subTableLayoutPanel.ColumnCount = 3;
            for (int i = 0; i < subTableLayoutPanel.ColumnCount; i++)
                subTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100f / (float)subTableLayoutPanel.ColumnCount));

            subTableLayoutPanel.RowCount = 4;
            for (int i = 0; i < subTableLayoutPanel.RowCount; i++)
                subTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100f / (float)subTableLayoutPanel.RowCount));
            //ajout du sous tableau dans la cellule concernée
            tableLayoutPanel.Controls.Add(subTableLayoutPanel, c, r);
        }
        

    //Taille et position du tableau principal
    tableLayoutPanel.Size = new System.Drawing.Size(500, 500);
    tableLayoutPanel.Location = new System.Drawing.Point(0,0);
    this.ResumeLayout();
    
    this.Controls.Add(tableLayoutPanel);

}


Pour peupler les cellules on insert des labels dans les cellules

// Ajout d'un label dans la cellule 3,3 du sous tableau 4,5
TableLayoutPanel subTableLayoutPanel = tableLayoutPanel.GetControlFromPosition(3, 4) as TableLayoutPanel;
if (subTableLayoutPanel != null)
{
    Label  label = new Label();
    label.Text = "Hi";
    label.Font = new Font("arrial narrow", 6f);
    label.ForeColor = Color.White;
    label.BackColor = Color.DarkBlue;
    subTableLayoutPanel.Controls.Add(label,2,2);
}



Bob.
C# is amazing, enjoy it!
0
Rejoignez-nous