Olivier Chung
Olivier Chung

Reputation: 49

C# Overlapping datagrid

I have two DataGrid in same position, so I just hide one of it at initiation. When I set coding to a button like DataGrid1.Visible = false; DataGrid2.Visible = true;

both DataGrid simply just disappear.

I guess the DataGrid1 overlay DataGrid2, so that DataGrid2 is hidden. I try to search the way pulling DataGrid2 out of water, but can't search it.

Also there I have two buttons assigning the same position. And do it as the same as above. The two buttons also disappear

Upvotes: 1

Views: 1429

Answers (5)

Rocky
Rocky

Reputation: 4524

You can do one simple thing that put your both data grid in 2 different panel, and hide and show that panel. It may solve your problem.

Upvotes: 1

Jacob Seleznev
Jacob Seleznev

Reputation: 8131

Try this. It works for me. If that doesn't work, set breakpoint, inspect both datagridView Visible properties.

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

Upvotes: 1

Fendy
Fendy

Reputation: 4643

You can try to gridView1.BringToFront();

However, try using TabControl instead. It has better UI styling and built-in support for the functionality.

Upvotes: 0

agAus
agAus

Reputation: 675

Are you doing this in the Button_Click() event handler on the server side? You may need to add a check for IsPostBack in your Page_Load() event.

Upvotes: 0

SleepNot
SleepNot

Reputation: 3038

You can try the BringToBack() and SendToBack() methods on your datagrids.

Upvotes: 1

Related Questions