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;
Related
For instance, if there is a push button, when the user press the button, the system should detect and record the time in dd-mm-yy. However, if the user press the button many time in 2 second, the system should only record the first time when the user press the button.
Assumption: you want to collect clicks but no more often than every 2 seconds. This simple form will do it.
Code:
namespace RecordClicks {
public partial class Form1 : Form {
private DateTime _recordedClick = DateTime.MinValue;
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
DateTime currentClickTime = DateTime.Now;
if (currentClickTime.Subtract(_recordedClick).TotalMilliseconds > 2000 ) {
_recordedClick = currentClickTime;
lblClick.Text = currentClickTime.ToString();
// TODO: add code to record this click
}
}
}
}
Designer:
namespace RecordClicks {
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.button1 = new System.Windows.Forms.Button();
this.lblClick = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(121, 241);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(132, 53);
this.button1.TabIndex = 0;
this.button1.Text = "Clicker";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// lblClick
//
this.lblClick.AutoSize = true;
this.lblClick.Font = new System.Drawing.Font("Segoe UI", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.lblClick.Location = new System.Drawing.Point(121, 127);
this.lblClick.Name = "lblClick";
this.lblClick.Size = new System.Drawing.Size(90, 45);
this.lblClick.TabIndex = 1;
this.lblClick.Text = "Time";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.lblClick);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Clicker Test";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Button button1;
private Label lblClick;
}
}
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 am developing a windows forms application for Windows Mobile 6 in Visual Studio 2008. There is a requirement to have some common controls on each form, such as Logo (PictureBox), Title (Label) and a small description (also Label). I decided to make a FormBase with those controls and inherit other forms from that base.
The problem is that for some reason, when I drop a Button or another control on that inherited form, I cannot resize it with my mouse. I can press Shift+Arrow to resize anything with my keyboard shortcuts, but the mouse does not work.
It's not very convenient neither for me nor for other developers in my team.
Any suggestions?
Update 1
the base form:
public class FormBase : Form
{
public FormBase()
{
InitializeComponent();
}
/// <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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormBase));
this.pictureLogo = new System.Windows.Forms.PictureBox();
this.labelTitle = new System.Windows.Forms.Label();
this.panelSeparator = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// pictureLogo
//
this.pictureLogo.Image = ((System.Drawing.Image)(resources.GetObject("pictureLogo.Image")));
this.pictureLogo.Location = new System.Drawing.Point(0, 0);
this.pictureLogo.Name = "pictureLogo";
this.pictureLogo.Size = new System.Drawing.Size(48, 48);
//
// labelTitle
//
this.labelTitle.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Bold);
this.labelTitle.ForeColor = System.Drawing.Color.Black;
this.labelTitle.Location = new System.Drawing.Point(54, 2);
this.labelTitle.Name = "labelTitle";
this.labelTitle.Size = new System.Drawing.Size(183, 16);
this.labelTitle.Text = "Title";
//
// panelSeparator
//
this.panelSeparator.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.panelSeparator.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(35)))), ((int)(((byte)(44)))), ((int)(((byte)(75)))));
this.panelSeparator.Location = new System.Drawing.Point(3, 50);
this.panelSeparator.Name = "panelSeparator";
this.panelSeparator.Size = new System.Drawing.Size(234, 1);
//
// FormBase
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(240, 320);
this.ControlBox = false;
this.Controls.Add(this.panelSeparator);
this.Controls.Add(this.labelTitle);
this.Controls.Add(this.pictureLogo);
this.ForeColor = System.Drawing.Color.Black;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.KeyPreview = true;
this.Location = new System.Drawing.Point(0, 0);
this.MinimizeBox = false;
this.Name = "FormBase";
this.Text = "FormBase";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureLogo;
private System.Windows.Forms.Label labelTitle;
private System.Windows.Forms.Panel panelSeparator;
}
the inherited form:
public class FrontDoorForm : FormBase
{
public FrontDoorForm()
{
InitializeComponent();
}
private void buttonQuit_Click(object sender, EventArgs e)
{
Close();
}
/// <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.buttonQuit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonQuit
//
this.buttonQuit.Location = new System.Drawing.Point(3, 54);
this.buttonQuit.Name = "buttonQuit";
this.buttonQuit.Size = new System.Drawing.Size(115, 46);
this.buttonQuit.TabIndex = 2;
this.buttonQuit.Text = "Quit";
this.buttonQuit.Click += new System.EventHandler(this.buttonQuit_Click);
//
// FrontDoorForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(240, 320);
this.Controls.Add(this.buttonQuit);
this.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Regular);
this.ForeColor = System.Drawing.Color.Black;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Location = new System.Drawing.Point(0, 0);
this.MinimizeBox = true;
this.Name = "FrontDoorForm";
this.Text = "FrontDoorForm";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Controls.SetChildIndex(this.buttonQuit, 0);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button buttonQuit;
}
Just to close the question I will quote the answer from the comment:
Change the base form's WindowState back to Normal for a workaround. You can still get it Maximized in the derived form. Hans Passant
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.
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.