the Design View of Datagridview in visual studio

sorry that i have to put this thread in Visual C# General since i cannot find an feasible category for this thread.

I have suffered for a long time, i have a datagridview control in a windows form, it was supposed to be a Property of the class, but everytime i switch to Design View of the form, the definition of datagridview is automatically moved into the InitializeComponent() method as a local variable rather than a property of the form, and i have to cut the code and paste into the property declaration part.....

please help...

i got mad with that....

thanks a million

renjian

[632 byte] By [Alan.Ren] at [2008-1-9]
# 1
How did you defined the property?
EliGazit at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2

simply drag a datagridview control from the toolbox on the left side of Visual Studio. the datagridview was declared as a property of the form(that was what i want), but next time you switch to design view, the definination just went away...

thanks

here is the code snippet:

Before:

partial class MyForm

{
/// <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()
{

// code here was deleted

}

#endregion
System.Windows.Forms.DataGridView dgvProduct;
}

after :

partial class MyForm

{
/// <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.Windows.Forms.DataGridView dgvProduct = new System.Windows.Forms.DataGridView();

// code here was deleted

}

#endregion
}

Alan.Ren at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

The code in InitializeComponent is automattically generated by the ide. You should not put your code in there. I would put it in forms constructor instead.

Code Snippet

public Form1()

{

InitializeComponent();

// Your code should go here

}

KenTucker at 2007-10-3 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...