【问题标题】:Multiple DataGridViews on 1 Form1 个表单上的多个 DataGridViews
【发布时间】:2016-05-12 22:47:39
【问题描述】:

我有一个显示 DataGridView 的简单表单;我想在同一个表单上的第一个下方显示第二个 DataGridView 对象。

但是当我运行代码时,它只显示第一个。通过调试器运行时,表单列出了它有两个数据网格视图链接到它。

System.Windows.Forms.Form form
          = new System.Windows.Forms.Form();
        form.Size = new System.Drawing.Size(450, 400);
        form.Text = "Form";

        DataGridView dg = new DataGridView();
        dg.AllowUserToAddRows = false;
        dg.AllowUserToDeleteRows = false;
        dg.AllowUserToOrderColumns = true;
        dg.Dock = System.Windows.Forms.DockStyle.Fill;
        dg.Location = new System.Drawing.Point(0, 0);
        dg.ReadOnly = true;
        dg.TabIndex = 0;
        dg.DataSource = dt;
        dg.Parent = form;



        DataGridView dgHangers = new DataGridView();
        dgHangers.AllowUserToAddRows = false;
        dgHangers.AllowUserToDeleteRows = false;
        dgHangers.AllowUserToOrderColumns = true;
        dgHangers.Dock = System.Windows.Forms.DockStyle.Fill;
// attempting get the bottom of the first DataGridView() so the second will display below.
        dgHangers.Location = new System.Drawing.Point(0, dg.DisplayRectangle.Bottom);
        dgHangers.ReadOnly = true;
        dgHangers.TabIndex = 1;
        dgHangers.DataSource = hangerTable;
        dgHangers.Parent = form;



        form.ShowDialog();

表格:

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    首先:使用System.Windows.Forms.DockStyle.Fill;,您看不到创建的两个数据网格, 所以测试你的两个dataGridView没有这条线。

    或者你可以使用第一个:dg.Dock = System.Windows.Forms.DockStyle.Top;

    第二个:dg.Dock = System.Windows.Forms.DockStyle.Bottom;

    您也可以使用form.Controls.Add(dg); 代替dg.Parent = form;

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2020-09-10
      • 1970-01-01
      • 2019-10-06
      相关资源
      最近更新 更多