Friday, April 24, 2009

Create A Complete Grid with programmetrically in C#

Hi,

This code snippet to help build windows form grid Under the Windows Presentation Foundation with Winodws Host Object.and also it C# code.

Note:Before add this code you need add Windows Host Object WPF Windows under the any parent control, and also rename to windowsFormsHostDetail.

internal void WindowsFormGridInit()
{
System.Windows.Forms.DataGridViewTextBoxColumn Column2;
System.Windows.Forms.DataGridViewCheckBoxColumn Column3;
System.Windows.Forms.DataGridViewCheckBoxColumn Column4;
System.Windows.Forms.DataGridViewTextBoxColumn Column5;
System.Windows.Forms.DataGridViewCheckBoxColumn Column6;
System.Windows.Forms.DataGridViewTextBoxColumn columnDestValueField;
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();

dataGridViewDetail = new System.Windows.Forms.DataGridView();
dataGridViewDetail.AllowUserToAddRows = false;
dataGridViewDetail.DataError += new DataGridViewDataErrorEventHandler(dataGridViewDetail_DataError);
dataGridViewDetail.CellValidating += new DataGridViewCellValidatingEventHandler(dataGridViewDetail_CellValidating);
Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
columnDestValueField = new DataGridViewTextBoxColumn();
Column3 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column4 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column5 = new System.Windows.Forms.DataGridViewTextBoxColumn();
Column6 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
Column7 = new System.Windows.Forms.DataGridViewComboBoxColumn();

((System.ComponentModel.ISupportInitialize)(dataGridViewDetail)).BeginInit();

//
// dataGridView1
//
dataGridViewCellStyle1.BackColor = System.Drawing.Color.LemonChiffon;
dataGridViewCellStyle1.ForeColor = System.Drawing.Color.Black;
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.Color.Beige;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(64)))), ((int)(((byte)(0)))));
dataGridViewDetail.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
dataGridViewDetail.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridViewDetail.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
Column2,
columnDestValueField,
Column3,
Column4,
Column5,
Column6,
Column7

});
dataGridViewDetail.Location = new System.Drawing.Point(40, 46);
dataGridViewDetail.Name = "dataGridView1";
dataGridViewDetail.RowHeadersVisible = false;
dataGridViewDetail.Size = new System.Drawing.Size(655, 150);
dataGridViewDetail.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
dataGridViewDetail.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.Raised;
dataGridViewDetail.BackgroundColor = System.Drawing.Color.Gainsboro;
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Black;
dataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.Blue;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.White;


dataGridViewDetail.TabIndex = 0;
//
// Column2
//
Column2.HeaderText = "Destination Display Name";
Column2.Name = "Column2";
Column2.DataPropertyName = "DestinationDisplayField";
Column2.ReadOnly = true;
Column2.Width = 200;
Column2.DefaultCellStyle = dataGridViewCellStyle2;
Column2.DisplayIndex = 0;

// Destionation Value Field

columnDestValueField.HeaderText = "Destination Element Name";
columnDestValueField.Width = 150;
columnDestValueField.DefaultCellStyle = dataGridViewCellStyle2;
columnDestValueField.DataPropertyName = "DestionationValueField";
columnDestValueField.ReadOnly = true;
columnDestValueField.Width = 200;
columnDestValueField.Name = "DestValueField";
columnDestValueField.DisplayIndex = 1;
//
// Column3
//
Column3.HeaderText = "Ignore";
Column3.DataPropertyName = "IsIgnore";
Column3.Name = "Column3";
Column3.DisplayIndex = 2;
Column3.Visible = false;
//Column3.DefaultCellStyle = dataGridViewCellStyle2;
//
// Column4
//
Column4.HeaderText = "Custom";
Column4.DataPropertyName = "IsCustom";
Column4.Name = "Column4";
Column4.DisplayIndex = 3;
Column4.Visible = false;
//Column4.DefaultCellStyle = dataGridViewCellStyle2;
//
// Column5
//
Column5.HeaderText = "Values";
Column5.DataPropertyName = "CustomValue";
Column5.Name = "Column5";
Column5.DefaultCellStyle = dataGridViewCellStyle2;
columnDestValueField.DisplayIndex = 4;
Column5.Visible = false;
//
// Column6
//
Column6.HeaderText = "Selection";
Column6.DataPropertyName = "IsFixed";
Column6.Name = "Column6";
Column6.DisplayIndex = 5;
Column6.Visible = false;
//Column6.DefaultCellStyle = dataGridViewCellStyle2;
//
// Column7
//

Column7.HeaderText = "CSV Source Fields";
Column7.DisplayMember = "DisplayValue";
Column7.ValueMember = "ValueMember";
Column7.DefaultCellStyle = dataGridViewCellStyle2;
Column7.DisplayIndex = 6;
Column7.Width = 200;
Column7.Name = "Column7";
((System.ComponentModel.ISupportInitialize)(dataGridViewDetail)).EndInit();
windowsFormsHostDetail.Child = dataGridViewDetail;

}


thank you

No comments:

Post a Comment