Hi all, Iwant to know is there any way to add a new column in datagrid depending upon the value of a combobox. I have columns in a table whose names are same as combobox's items names. I need to add that column in datagrid which value is selected by user.
Please help me its urgent

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( "MyColumn", "Column 1" );

Thanx for replying, But my motive is to add a new column with database values. How can i do it?

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

//=============
//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;
    }
}
//========
//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;
        }
    }
}

I have tried it with the coding given above But it is still not adding new column. Plus i want to know how that particular column can be bound with database column

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

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

hi,
i am new to .net.

i like to add the datagridview into the datagridview.

please share ur ideas,

plz tell abt how to add moving picture in c# window application????

commented: unrelated to the OP -1

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.

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

Hi,

To my mind, you would want to apply the class as a filter to your Select rather than displaying it as a column. So if they select First class then only flights with First Class are displayed, etc.

I'm trying to get a better idea of the data your working with here. What type of data are you storing in the Economy, Business and First columns?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.