【问题标题】:Anchoring checkbox in DatagridviewDatagridview中的锚定复选框
【发布时间】:2012-11-26 12:42:29
【问题描述】:

我正在创建一个基于 winform 的桌面应用程序,并使用 Datagridview 填充数据。

我在其中一个标题栏中使用checkbox。表格很大以适合屏幕,并使用滚动条在水平和垂直方向上移动。

checkbox 需要随着滚动条的移动而移动。然而,问题是它仍然是静态的。

知道如何锚定它,这样当滚动条移动时,复选框也会相应移动。

谢谢

编辑:

自动生成的设计器代码:

            this.checkheader.AutoSize = true;
            this.checkheader.BackColor = System.Drawing.Color.White;
            this.checkheader.FlatAppearance.BorderColor = System.Drawing.Color.White;
            this.checkheader.Location = new System.Drawing.Point(49, 96);
            this.checkheader.Name = "checkheader";
            this.checkheader.Size = new System.Drawing.Size(15, 14);
            this.checkheader.TabIndex = 21;
            this.checkheader.UseVisualStyleBackColor = false;
            this.checkheader.CheckedChanged += new System.EventHandler(this.checkboxHeader_CheckedChanged);
        // 

【问题讨论】:

  • 如何将复选框放在标题中?它只是网格顶部的静态控制吗?
  • 如何使用标题栏中的复选框?你能解释一下代码吗?
  • 你能告诉我们自动生成的设计器代码吗?
  • 哇,我什至不知道您可以在标题中使用 CheckBox。绝对+1的问题。 :)
  • 与@Needo 所说的一样,它只是绘制在DataGridView 控件的前面,这就是它不随滚动条一起移动的原因。你到底需要一个datagridview中的复选框来做什么?

标签: c# winforms datagridview


【解决方案1】:

您可以使用 datagridview 'scroll'-event 来执行以下操作:

private void dataGridView1_Scroll(object sender, ScrollEventArgs e)
    {
        if (e.ScrollOrientation.Equals(ScrollOrientation.HorizontalScroll))
        {
            checkBox1.Location = new Point(checkBox1.Location.X - (e.NewValue - e.OldValue), checkBox1.Location.Y);
        }
        if (checkBox1.Location.X < dataGridView1.Location.X + 40)
        {
            checkBox1.Visible = false;
        }
        else
        {
            checkBox1.Visible = true;
        }
    }

迟到总比没有好?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多