943,544 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 6526
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 19th, 2008
0

How to add a colimn in Datagrid at runtime

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Gaurav arora is offline Offline
59 posts
since Mar 2007
Aug 19th, 2008
0

Re: How to add a colimn in Datagrid at runtime

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.
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Aug 20th, 2008
0

Re: How to add a colimn in Datagrid at runtime

Thanx for replying, But my motive is to add a new column with database values. How can i do it?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Gaurav arora is offline Offline
59 posts
since Mar 2007
Aug 20th, 2008
0

Re: How to add a colimn in Datagrid at runtime

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
C# Syntax (Toggle Plain Text)
  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. }

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 3
Light Poster
AmirBedair is offline Offline
26 posts
since Jul 2008
Aug 20th, 2008
0

Re: How to add a colimn in Datagrid at runtime

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Gaurav arora is offline Offline
59 posts
since Mar 2007
Aug 21st, 2008
0

Re: How to add a colimn in Datagrid at runtime

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
Reputation Points: 10
Solved Threads: 3
Light Poster
AmirBedair is offline Offline
26 posts
since Jul 2008
Aug 21st, 2008
0

Re: How to add a colimn in Datagrid at runtime

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Gaurav arora is offline Offline
59 posts
since Mar 2007
Feb 11th, 2009
0

how can i add the datagrid into the datagrid,

hi,
i am new to .net.

i like to add the datagridview into the datagridview.

please share ur ideas,
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
karthi_selva is offline Offline
65 posts
since Oct 2008
Oct 7th, 2009
-2
Re: How to add a colimn in Datagrid at runtime
plz tell abt how to add moving picture in c# window application????
Reputation Points: 9
Solved Threads: 0
Newbie Poster
shilpa88 is offline Offline
2 posts
since Oct 2009
Oct 8th, 2009
0
Re: How to add a colimn in Datagrid at runtime
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.
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Accessing variable properties from a dynamically registered DLL
Next Thread in C# Forum Timeline: Help with Snake Game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC