向 Winforms DataGridView 添加按钮

发布于 2024-12-12 05:42:43 字数 96 浏览 0 评论 0原文

有没有办法将控件(例如按钮)添加到 C# 中的 Winforms DataGridView 单元格?

(我的目标是将各种控件放在网格的不同单元格中......)

Is there a way to add a control (e.g. a button) to a Winforms DataGridView cell in C#?

(What I'm aiming at is putting various kinds of controls in different cells of the grid...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

并安 2024-12-19 05:42:43

你可以这样做......

有两种方法可以做到这一点:

  • 1)。将 DataGridViewCell 转换为现有的特定单元格类型。为了
    例如,将 DataGridViewTextBoxCell 转换为
    DataGridViewComboBoxCell 类型。
  • 2)。创建一个控件并将其添加到控件集合中
    DataGridView,设置其位置和大小以适合要显示的单元格
    主持人。

请参阅下面的示例代码,其中说明了这些技巧。

代码片段

    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;
    }

you can do like this....

There're two ways to do this:

  • 1). Cast a DataGridViewCell to a certain cell type that exists. For
    example, convert a DataGridViewTextBoxCell to
    DataGridViewComboBoxCell type.
  • 2). Create a control and add it into the controls collection of
    DataGridView, set its location and size to fit the cell that to be
    host.

See my sample code below which illustrates the tricks.

Code Snippet

    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;
    }
转角预定愛 2024-12-19 05:42:43

DataGridViewButtonColumn 是提供的包含可单击按钮的列类型。您可以将自己的控件添加到单元格中:

http://msdn.microsoft.com /en-us/library/7tas5c80.aspx

请记住,这并不总是微不足道的,但我见过有人将整个 DataGridView 放入单元格中 - 看起来很奇怪。

还有其他提供的列:

DataGridViewButtonColumn
DataGridViewCheckBoxColumn
DataGridViewComboBoxColumn
DataGridViewImageColumn
DataGridViewLinkColumn
DataGridViewTextBoxColumn

您可以在 Visual Studio 设计器的列编辑器中更改这些列,或以代码形式将它们添加到 Columns 集合中。

The DataGridViewButtonColumn is a provided column type that contains a clickable button. You can add your own controls to the cells:

http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

Bear in mind it isn't always trivial, but I have seen someone put an entire DataGridView into a cell - looked weird.

There are also other provided columns:

DataGridViewButtonColumn
DataGridViewCheckBoxColumn
DataGridViewComboBoxColumn
DataGridViewImageColumn
DataGridViewLinkColumn
DataGridViewTextBoxColumn

You can change these in the column editor in the designer in Visual Studio, or add them in code to the Columns collection.

凉风有信 2024-12-19 05:42:43

我会将其添加到设计器中并创建一个模板字段。将您想要的任何内容放入 datagridview

示例中的简单方法

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

I would add it in the designer and make a template field. Easy way to go and put anything you want in the datagridview

Example

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文