Ajouter un bouton à un WinForms DataGridView
y a-t-il un moyen d'ajouter un contrôle (par exemple un bouton) à une cellule Winforms DataGridView en C#?
(ce que je vise est de mettre différents types de contrôles dans différentes cellules de la grille...)
3 réponses
vous pouvez faire comme ceci....
Il y a deux façons de faire cela:
- 1). Lancer un DataGridViewCell à un certain type de cellule qui existe. Pour exemple, convertissez un DataGridViewTextBoxCell en Type DataGridViewComboBoxCell.
- 2). Créer un contrôle et l'ajouter dans la collection de contrôles de DataGridView, définit son emplacement et sa taille pour s'adapter à la cellule qui doit être hôte.
Voir mon exemple de code ci-dessous qui illustre ficelles.
Extrait De Code
    private void Form5_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name");
        for (int j = 0; j < 10; j++)
        {
            dt.Rows.Add("");
        }
        this.dataGridView1.DataSource = dt;
        this.dataGridView1.Columns[0].Width = 200;
        /*
         * First method : Convert to an existed cell type such ComboBox cell,etc
         */
        DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
        ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" });
        this.dataGridView1[0, 0] = ComboBoxCell;
        this.dataGridView1[0, 0].Value = "bbb";
        DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
        this.dataGridView1[0, 1] = TextBoxCell;
        this.dataGridView1[0, 1].Value = "some text";
        DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();
        CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        this.dataGridView1[0, 2] = CheckBoxCell;
        this.dataGridView1[0, 2].Value = true;
        /*
         * Second method : Add control to the host in the cell
         */
        DateTimePicker dtp = new DateTimePicker();
        dtp.Value = DateTime.Now.AddDays(-10);
        //add DateTimePicker into the control collection of the DataGridView
        this.dataGridView1.Controls.Add(dtp);
        //set its location and size to fit the cell
        dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Location;
        dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Size;
    }
DataGridViewButtonColumn est un type de colonne qui contient un bouton cliquable.  Vous pouvez ajouter vos propres commandes pour les cellules:
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
gardez à l'esprit qu'il n'est pas toujours trivial, mais j'ai vu quelqu'un mettre un DataGridView dans une cellule - regardé bizarre.
Il y a aussi les autres colonnes:
DataGridViewButtonColumn
DataGridViewCheckBoxColumn
DataGridViewComboBoxColumn
DataGridViewImageColumn
DataGridViewLinkColumn
DataGridViewTextBoxColumn
vous pouvez les modifier dans L'éditeur de colonne du concepteur en Visual Studio, ou les ajouter en code à la collection de colonnes.
Je l'ajouterais dans le champ designer et je ferais un modèle. Facile à faire, et mettre ce que vous voulez dans le datagridview
Exemple
<asp:DataGrid ID="somedatagrid" runat="server">
    <Columns>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:Button ID="somebutton" runat="server" Text="some button" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>