| | |
How to add a colimn in Datagrid at runtime
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
Hi, Use the following syntax to add Column at runtime
DataGridView1.Columns.Add ( string ColumnName, string ColumnCaption)
If your Combobox values are unique then use ComboBox selected value as Column name and Caption otherwise choose your own column name and Columncaption can be combobox value. because Columnnames are unique.
Use SelectedIndexChanged event of Combobox to add new Column and
After adding new Column remove it from the Combobox
Ex
DataGridView1.Columns.Add ( string ColumnName, string ColumnCaption)
If your Combobox values are unique then use ComboBox selected value as Column name and Caption otherwise choose your own column name and Columncaption can be combobox value. because Columnnames are unique.
Use SelectedIndexChanged event of Combobox to add new Column and
After adding new Column remove it from the Combobox
Ex
DataGridView1.Columns.Add( "MyColumn", "Column 1" ); Last edited by selvaganapathy; Aug 19th, 2008 at 11:35 pm.
Selva •
•
Join Date: Jul 2008
Posts: 26
Reputation:
Solved Threads: 3
Hi Gaurav
Please check the next code, to add columns to data grid view at run time from combo box
if I miss understand You, please explan and I will try to help you
Please check the next code, to add columns to data grid view at run time from combo box
if I miss understand You, please explan and I will try to help you
C# Syntax (Toggle Plain Text)
//============= //Form1.Designer.cs //=========== namespace WinDatagrid { 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.dgvTest = new System.Windows.Forms.DataGridView(); this.cbColumn = new System.Windows.Forms.ComboBox(); this.btnAdd = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dgvTest)).BeginInit(); this.SuspendLayout(); // // dgvTest // this.dgvTest.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dgvTest.Location = new System.Drawing.Point(91, 95); this.dgvTest.Name = "dgvTest"; this.dgvTest.Size = new System.Drawing.Size(389, 254); this.dgvTest.TabIndex = 0; // // cbColumn // this.cbColumn.FormattingEnabled = true; this.cbColumn.Location = new System.Drawing.Point(312, 51); this.cbColumn.Name = "cbColumn"; this.cbColumn.Size = new System.Drawing.Size(168, 21); this.cbColumn.TabIndex = 1; this.cbColumn.SelectedIndexChanged += new System.EventHandler(this.cbColumn_SelectedIndexChanged); // // btnAdd // this.btnAdd.Location = new System.Drawing.Point(167, 49); this.btnAdd.Name = "btnAdd"; this.btnAdd.Size = new System.Drawing.Size(113, 23); this.btnAdd.TabIndex = 2; this.btnAdd.Text = "Add New Column"; this.btnAdd.UseVisualStyleBackColor = true; this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(574, 390); this.Controls.Add(this.btnAdd); this.Controls.Add(this.cbColumn); this.Controls.Add(this.dgvTest); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dgvTest)).EndInit(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.DataGridView dgvTest; private System.Windows.Forms.ComboBox cbColumn; private System.Windows.Forms.Button btnAdd; } }
C# Syntax (Toggle Plain Text)
//======== //Form1.cs //======== using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WinDatagrid { public partial class Form1 : Form { DataTable dt = new DataTable("dtGrid"); public Form1() { InitializeComponent(); cbColumn.Items.Add("UserName"); cbColumn.Items.Add("Telephone"); cbColumn.Items.Add("BirthDate"); cbColumn.Items.Add("Address"); cbColumn.Items.Add("E-Mail"); dt.Columns.Add("UserName"); dt.Columns.Add("Telephone"); dt.Columns.Add("BirthDate"); dgvTest.DataSource = dt; } private void cbColumn_SelectedIndexChanged(object sender, EventArgs e) { if (!dt.Columns.Contains(cbColumn.Text)) { btnAdd.Enabled = true; } else { btnAdd.Enabled = false; } } private void btnAdd_Click(object sender, EventArgs e) { dt.Columns.Add(cbColumn.Text); btnAdd.Enabled = false; } } }
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Senior Solution Developer
Please rate my answer
•
•
Join Date: Jul 2008
Posts: 26
Reputation:
Solved Threads: 3
forget you are dealing with datagrid now you are in dealing with the datatable add column add rows to it any, by mapping or select direct from a database.. any way any change in the datatable will be chnaged in datagrid cause datagrid bounded or this datatable.
I wish you understand this technique in dealing with datagrid
Regards,
Amir Bedair
I wish you understand this technique in dealing with datagrid
Regards,
Amir Bedair
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Senior Solution Developer
Please rate my answer
•
•
Join Date: Mar 2007
Posts: 59
Reputation:
Solved Threads: 0
sorry I couldnt understand ur suggestion. I explain u my problem. I m making a project on Airlines Reservation. Now after selecting source,destination,class and date of travel when user clicks on "Available Flights" button then the flights available on the date selected by user are displayed in the datagrid. The information in the datagrid is : FlightNo, Source, Destination, Departure Time, Arrival Time. Now i want to add another column which exists in the same table as others. That column must be the value selected in class combo box. i.e. Economy,Business or First. But i m unable to do it. Hope u understand my problem
Thnx
Thnx
0
#10 Oct 8th, 2009
Shilpa88, if you have a problem that you need help with, please look for a relevant thread or start a new one yourself. Please dont add unrelated questions to current threads.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Other Threads in the C# Forum
- Previous Thread: Accessing variable properties from a dynamically registered DLL
- Next Thread: Help with Snake Game
Views: 3282 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file form format forms ftp function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics string table textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml





