I have been writing a lot of code in Java and find that it is really easy to create either a console or form application. In fact, all one has to do is add a form and display that form. Simple as a cookie, so to speak. But now I have a big project coming up in Visual C# and I have not used it all that much. I think, I am not sure if I am right, but a console application is C# is just that, a console application which cannot accept any mouse action events. I want to be able to add controls to C# from inside of the form, just like in Java. Add a button, or add a menu. But in C# there are several files that open, properties, assemblyinfo.cs, form1.cs, etc. The code below is in form designer.cs.
So where is the best way to add components from the programming point of view and not the design visual stand point of view?
namespace WindowsFormsTestMenuApplication
{
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.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.optionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.aToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.bToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.cToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem,
this.openToolStripMenuItem,
this.optionsToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(284, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// fileToolStripMenuItem
//
this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.aToolStripMenuItem,
this.bToolStripMenuItem,
this.cToolStripMenuItem});
this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
this.fileToolStripMenuItem.Text = "File";
//
// openToolStripMenuItem
//
this.openToolStripMenuItem.Name = "openToolStripMenuItem";
this.openToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
this.openToolStripMenuItem.Text = "Open";
//
// optionsToolStripMenuItem
//
this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem";
this.optionsToolStripMenuItem.Size = new System.Drawing.Size(61, 20);
this.optionsToolStripMenuItem.Text = "Options";
//
// aToolStripMenuItem
//
this.aToolStripMenuItem.Name = "aToolStripMenuItem";
this.aToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.aToolStripMenuItem.Text = "A";
//
// bToolStripMenuItem
//
this.bToolStripMenuItem.Name = "bToolStripMenuItem";
this.bToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.bToolStripMenuItem.Text = "B";
//
// cToolStripMenuItem
//
this.cToolStripMenuItem.Name = "cToolStripMenuItem";
this.cToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.cToolStripMenuItem.Text = "C";
//
// 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.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem aToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem bToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem cToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem optionsToolStripMenuItem;
}
}
Sounds somehow like a general design/approach question to me.
Why don't you use WPF over WinForms? Gives you a quite handy designer in Visual Studio. You can comfortably build your GUI in WYSIWYG-style. Supports data binding, which - once familiar with - is pretty convenient.
However, later you can still add controls etc. at runtime, see Breems answer.
Note: If you use WPF, you need to use the Windows's dispatcher if you want to add controls from another thread than the Windows's one.
Are you trying to add controls at runtime? If so, as SLaks stated, you will simply need to create a new instance of the control and add it to your form.
// Add a menustrip to the form.
MenuStrip menuStrip = new MenuStrip();
menuStrip.Dock = DockStyle.Top;
this.Controls.Add(menuStrip);
Otherwise, why not utilize the visual designer to add controls to your form?
WinForms controls are regular objects, just like AWT or Swing.
You can create a new ToolStripItem(), set its properties, then add it to a DropDownItems collection.
Related
I have an issue with C# richtextbox and Microsoft equation editor. My goal is to read, modify and save changes in rtf file.
I followed tutorial below:
C# Tutorial : How to insert Math Equation to RichTextBox | FoxLearn
I can read and modify equation in editor, however I can't update richtextbox after modification, modified value is not changed in richtboxtext.
Initial state
Modification
Update(F3) and exit in equation editor doesn't cause change in richtextbox.
However when I double click equation in editor there is changed value.
Here is my code:
using System;
using System.Windows.Forms;
namespace Panel.Views
{
public partial class Axis : Form
{
public Axis()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectedRtf = Panel.Properties.Resources.Plot1_x_axis;
}
}
}
namespace Panel.Views
{
partial class Axis
{
/// <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.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(723, 431);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(28, 36);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(770, 372);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// Axis
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(822, 478);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Axis";
this.Text = "Axis";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
}
}
I would be appreciated for any help, hints or suggestions how to solve this issue.
Thank you in advance.
I have made a sample project after my customers reported a graphical glitch in my software and I managed to reproduce the problem with ease. I create a simple docked DataGridView in a form and I fill it with random data like this:
var ds = new DataSet();
var table = ds.Tables.Add();
Enumerable.Range(0, 100).ForEach(i => table.Columns.Add(i.ToString()));
Enumerable.Range(0, 100).ForEach(i =>
{
var row = table.NewRow();
Enumerable.Range(0, 100).ForEach(j => row[j.ToString()] = Guid.NewGuid().ToString());
table.Rows.Add(row);
});
dataGridView1.DataSource = table;
Now I launch my program, move the window so that a part of it is covered by my taskbar and use a scrollbar. Suddenly all the data is messed up:
UPDATE:
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();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
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(986, 758);
this.dataGridView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(986, 758);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
}
What is the reason behind this?
So as a solution the .Invalidate() method can be used. Although it is not the best way. More about invalidate method can be found here:
https://msdn.microsoft.com/en-us/library/598t492a(v=vs.110).aspx
in my case the issue was that
some DataGridView cells background color were set to transparent
g[x,y].Style.BackColor = Color.Transparent;
I have a ComboBox. Its autocomplete mode is set to "SuggestAppend" and the autocomlete source is set to "ListItems". If I try to update the list of items of that combo box in the combo box's Enter event handler, I get the following unexpected behavior.
When the combobox is NOT focused and I focus it by clicking the arrow next to the combo box, the list of items drops and collapes back instantly. Subsequent click on the arrow drop down the list fine because the combo box is already focused.
I suspect that's because when I update the item list inside the Enter event handler, another event is fired (item list changed ?) for the autocomplete to handle its magic, which triggers the combo box to collapse.
What can I do about this? Do note that it is somewhat important for me to update the list of items in the combobox only when I know that that combobox is going to be used soon (hence the enter event handler).
Here's a minimal compilable example (the button is there so that the combobox isn't initially focused):
Form1 Designer:
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.comboBox1 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(50, 83);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(190, 24);
this.comboBox1.TabIndex = 1;
this.comboBox1.Enter += new System.EventHandler(this.comboBox1_Enter);
//
// button1
//
this.button1.Location = new System.Drawing.Point(165, 35);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 255);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Button button1;
}
Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_Enter(object sender, EventArgs e)
{
string[] items = new string[] { "A", "B", "C" };
comboBox1.Items.Clear();
comboBox1.Items.AddRange(items);
}
}
For your scenario, it's simply a matter of enclosing the ComboBox.Items modification with BeginUpdate / EndUpdate calls:
private void comboBox1_Enter(object sender, EventArgs e)
{
string[] items = new string[] { "A", "B", "C" };
comboBox1.BeginUpdate();
comboBox1.Items.Clear();
comboBox1.Items.AddRange(items);
comboBox1.EndUpdate();
}
It prevents immediate reacting on Items.Clear call which was the cause of the issue.
I am working on a small project using MS Visual Studio C# 2010.
In my MainFormDesigner.cs file I have the following code. All it does is load a web page from my server. I need the app to fill the display which is 1080 x 1920. But when I save and build the app some the the sizes default to the resolution of the screen I am working on.
Is there a way to automatically size the app to fit the resoltion of any screen the app runs on.
namespace Impa_Browser
{
partial class MainForm
{
/// <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.browser = new System.Windows.Forms.WebBrowser();
this.connectLbl = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// browser
//
this.browser.Location = new System.Drawing.Point(0, 0);
this.browser.Margin = new System.Windows.Forms.Padding(0);
this.browser.MinimumSize = new System.Drawing.Size(20, 20);
this.browser.Name = "browser";
this.browser.ScrollBarsEnabled = false;
this.browser.Size = new System.Drawing.Size(1080, 1920); // THIS IS THE RESOLUTION OF THE DISPLAY THE APP WILL RUN ON
this.browser.TabIndex = 0;
this.browser.Url = new System.Uri("example.com", System.UriKind.Absolute);
this.browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.browser_DocumentCompleted);
//
// connectLbl
//
this.connectLbl.Dock = System.Windows.Forms.DockStyle.Fill;
this.connectLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.connectLbl.Location = new System.Drawing.Point(0, 0);
this.connectLbl.Name = "connectLbl";
this.connectLbl.Size = new System.Drawing.Size(1080, 1092); // THIS KEEPS CHANGING TO THE RESOLUTION OF THE SCREEN I AM WORKING ON
this.connectLbl.TabIndex = 1;
this.connectLbl.Text = " Trying to connect ...[20] Please check your Internet router";
this.connectLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1080, 1092); // THIS KEEPS CHANGING TO THE RESOLUTION OF THE SCREEN I AM WORKING ON
this.Controls.Add(this.browser);
this.Controls.Add(this.connectLbl);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Impa";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.WebBrowser browser;
private System.Windows.Forms.Label connectLbl;
}
}
Many thanks for any help you can provide.
If you are using WinForms you can set the WindowState to FormWindowState Maximized like this.
this.WindowState = FormWindowState.Maximized;
For WPF user WindowsState Maximized
this.WindowState = WindowState.Maximized;
There are a few solutions to achive this goal:
1) Simply change the WindowState property in the Designer to Maximized.
2) If you don't wanna change this property you can do this in code - for example in the Load Event of your form like this:
private void MainForm_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
3) Overwrite the OnLoad method of your form like this:
protected override OnLoad(EventArgs e)
{
base.OnLoad(e);
this.WindowState = FormWindowState.Maximized;
}
4) If there is a good reason to not work with WindowState you can use the Screenclass object for screen where your form is displayed. For example like this:
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
// get the current screen
Screen screen = Screen.FromControl(this);
// set Location and Size of your form to fit in the WorkingArea
Location = new Point(screen.WorkingArea.Left, screen.WorkingArea.Top);
Size = new Size(screen.WorkingArea.Width, screen.WorkingArea.Height);
}
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
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;
}
}
}
You can try the BringToBack() and SendToBack() methods on your datagrids.
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.
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.
You can try to gridView1.BringToFront();
However, try using TabControl instead. It has better UI styling and built-in support for the functionality.