How to add a colimn in Datagrid at runtime

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 59
Reputation: Gaurav arora is an unknown quantity at this point 
Solved Threads: 0
Gaurav arora Gaurav arora is offline Offline
Junior Poster in Training

How to add a colimn in Datagrid at runtime

 
0
  #1
Aug 19th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: How to add a colimn in Datagrid at runtime

 
0
  #2
Aug 19th, 2008
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" );
Last edited by selvaganapathy; Aug 19th, 2008 at 11:35 pm.
KSG
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 59
Reputation: Gaurav arora is an unknown quantity at this point 
Solved Threads: 0
Gaurav arora Gaurav arora is offline Offline
Junior Poster in Training

Re: How to add a colimn in Datagrid at runtime

 
0
  #3
Aug 20th, 2008
Thanx for replying, But my motive is to add a new column with database values. How can i do it?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 26
Reputation: AmirBedair is an unknown quantity at this point 
Solved Threads: 3
AmirBedair AmirBedair is offline Offline
Light Poster

Re: How to add a colimn in Datagrid at runtime

 
0
  #4
Aug 20th, 2008
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
  1. //=============
  2. //Form1.Designer.cs
  3. //===========
  4.  
  5. namespace WinDatagrid
  6. {
  7. partial class Form1
  8. {
  9. /// <summary>
  10. /// Required designer variable.
  11. /// </summary>
  12. private System.ComponentModel.IContainer components = null;
  13.  
  14. /// <summary>
  15. /// Clean up any resources being used.
  16. /// </summary>
  17. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  18. protected override void Dispose(bool disposing)
  19. {
  20. if (disposing && (components != null))
  21. {
  22. components.Dispose();
  23. }
  24. base.Dispose(disposing);
  25. }
  26.  
  27. #region Windows Form Designer generated code
  28.  
  29. /// <summary>
  30. /// Required method for Designer support - do not modify
  31. /// the contents of this method with the code editor.
  32. /// </summary>
  33. private void InitializeComponent()
  34. {
  35. this.dgvTest = new System.Windows.Forms.DataGridView();
  36. this.cbColumn = new System.Windows.Forms.ComboBox();
  37. this.btnAdd = new System.Windows.Forms.Button();
  38. ((System.ComponentModel.ISupportInitialize)(this.dgvTest)).BeginInit();
  39. this.SuspendLayout();
  40. //
  41. // dgvTest
  42. //
  43. this.dgvTest.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  44. this.dgvTest.Location = new System.Drawing.Point(91, 95);
  45. this.dgvTest.Name = "dgvTest";
  46. this.dgvTest.Size = new System.Drawing.Size(389, 254);
  47. this.dgvTest.TabIndex = 0;
  48. //
  49. // cbColumn
  50. //
  51. this.cbColumn.FormattingEnabled = true;
  52. this.cbColumn.Location = new System.Drawing.Point(312, 51);
  53. this.cbColumn.Name = "cbColumn";
  54. this.cbColumn.Size = new System.Drawing.Size(168, 21);
  55. this.cbColumn.TabIndex = 1;
  56. this.cbColumn.SelectedIndexChanged += new System.EventHandler(this.cbColumn_SelectedIndexChanged);
  57. //
  58. // btnAdd
  59. //
  60. this.btnAdd.Location = new System.Drawing.Point(167, 49);
  61. this.btnAdd.Name = "btnAdd";
  62. this.btnAdd.Size = new System.Drawing.Size(113, 23);
  63. this.btnAdd.TabIndex = 2;
  64. this.btnAdd.Text = "Add New Column";
  65. this.btnAdd.UseVisualStyleBackColor = true;
  66. this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
  67. //
  68. // Form1
  69. //
  70. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  71. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  72. this.ClientSize = new System.Drawing.Size(574, 390);
  73. this.Controls.Add(this.btnAdd);
  74. this.Controls.Add(this.cbColumn);
  75. this.Controls.Add(this.dgvTest);
  76. this.Name = "Form1";
  77. this.Text = "Form1";
  78. ((System.ComponentModel.ISupportInitialize)(this.dgvTest)).EndInit();
  79. this.ResumeLayout(false);
  80.  
  81. }
  82.  
  83. #endregion
  84.  
  85. private System.Windows.Forms.DataGridView dgvTest;
  86. private System.Windows.Forms.ComboBox cbColumn;
  87. private System.Windows.Forms.Button btnAdd;
  88. }
  89. }

  1. //========
  2. //Form1.cs
  3. //========
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12.  
  13. namespace WinDatagrid
  14. {
  15. public partial class Form1 : Form
  16. {
  17. DataTable dt = new DataTable("dtGrid");
  18. public Form1()
  19. {
  20. InitializeComponent();
  21.  
  22. cbColumn.Items.Add("UserName");
  23. cbColumn.Items.Add("Telephone");
  24. cbColumn.Items.Add("BirthDate");
  25. cbColumn.Items.Add("Address");
  26. cbColumn.Items.Add("E-Mail");
  27.  
  28.  
  29. dt.Columns.Add("UserName");
  30. dt.Columns.Add("Telephone");
  31. dt.Columns.Add("BirthDate");
  32. dgvTest.DataSource = dt;
  33.  
  34.  
  35. }
  36.  
  37. private void cbColumn_SelectedIndexChanged(object sender, EventArgs e)
  38. {
  39. if (!dt.Columns.Contains(cbColumn.Text))
  40. {
  41. btnAdd.Enabled = true;
  42. }
  43. else
  44. {
  45. btnAdd.Enabled = false;
  46. }
  47. }
  48.  
  49. private void btnAdd_Click(object sender, EventArgs e)
  50. {
  51. dt.Columns.Add(cbColumn.Text);
  52. btnAdd.Enabled = false;
  53. }
  54. }
  55. }
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 59
Reputation: Gaurav arora is an unknown quantity at this point 
Solved Threads: 0
Gaurav arora Gaurav arora is offline Offline
Junior Poster in Training

Re: How to add a colimn in Datagrid at runtime

 
0
  #5
Aug 20th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 26
Reputation: AmirBedair is an unknown quantity at this point 
Solved Threads: 3
AmirBedair AmirBedair is offline Offline
Light Poster

Re: How to add a colimn in Datagrid at runtime

 
0
  #6
Aug 21st, 2008
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
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 59
Reputation: Gaurav arora is an unknown quantity at this point 
Solved Threads: 0
Gaurav arora Gaurav arora is offline Offline
Junior Poster in Training

Re: How to add a colimn in Datagrid at runtime

 
0
  #7
Aug 21st, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 56
Reputation: karthi_selva is an unknown quantity at this point 
Solved Threads: 0
karthi_selva karthi_selva is offline Offline
Junior Poster in Training

how can i add the datagrid into the datagrid,

 
0
  #8
Feb 11th, 2009
hi,
i am new to .net.

i like to add the datagridview into the datagridview.

please share ur ideas,
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 2
Reputation: shilpa88 is an unknown quantity at this point 
Solved Threads: 0
shilpa88 shilpa88 is offline Offline
Newbie Poster
 
-2
  #9
Oct 7th, 2009
plz tell abt how to add moving picture in c# window application????
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 379
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 68
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
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
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC