C#重叠数据网格

时间:2013-03-14 04:01:25

标签: c# winforms datagrid

我有两个DataGrid在同一个位置,所以我只是在启动时隐藏其中一个。 当我将编码设置为像这样的按钮时 DataGrid1.Visible = false; DataGrid2.Visible = true;

两个DataGrid都只是消失了。

我猜DataGrid1覆盖了DataGrid2,因此隐藏了DataGrid2。 我尝试搜索从水中拉出DataGrid2的方式,但无法搜索它。

此外,我有两个按钮分配相同的位置。 并按照与上述相同的方式进行。 这两个按钮也会消失

5 个答案:

答案 0 :(得分:1)

您可以在datagrids上尝试BringToBack()和SendToBack()方法。

答案 1 :(得分:1)

试试这个。这个对我有用。如果这不起作用,请设置断点,检查两个datagridView可见属性。

<强> Form1.designer.cs

namespace WindowsFormsApplication1
{
partial class Form1
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.dataGridView2 = new System.Windows.Forms.DataGridView();
        this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.button1 = new System.Windows.Forms.Button();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Column1});
        this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView1.Location = new System.Drawing.Point(0, 0);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(284, 262);
        this.dataGridView1.TabIndex = 0;
        // 
        // Column1
        // 
        this.Column1.HeaderText = "Column1";
        this.Column1.Name = "Column1";
        // 
        // dataGridView2
        // 
        this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.Column2});
        this.dataGridView2.Dock = System.Windows.Forms.DockStyle.Fill;
        this.dataGridView2.Location = new System.Drawing.Point(0, 0);
        this.dataGridView2.Name = "dataGridView2";
        this.dataGridView2.Size = new System.Drawing.Size(284, 262);
        this.dataGridView2.TabIndex = 1;
        this.dataGridView2.Visible = false;
        // 
        // Column2
        // 
        this.Column2.HeaderText = "Column2";
        this.Column2.Name = "Column2";
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(209, 227);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(75, 23);
        this.button1.TabIndex = 2;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.dataGridView2);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private System.Windows.Forms.DataGridViewTextBoxColumn Column1;
    private System.Windows.Forms.DataGridView dataGridView2;
    private System.Windows.Forms.DataGridViewTextBoxColumn Column2;
    private System.Windows.Forms.Button button1;
}
}

<强> Form1.cs的

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        dataGridView1.Visible = !dataGridView1.Visible;
        dataGridView2.Visible = !dataGridView2.Visible;
    }
}
}

答案 2 :(得分:1)

您可以做一件简单的事情,将两个数据网格放在两个不同的面板中,然后隐藏并显示该面板。它可以解决你的问题。

答案 3 :(得分:0)

您是在服务器端的Button_Click()事件处理程序中执行此操作吗?您可能需要在Page_Load()事件中添加对IsPostBack的检查。

答案 4 :(得分:0)

您可以尝试使用gridView1.BringToFront();

但是,请尝试使用TabControl。它具有更好的UI样式和内置的功能支持。