Datagridview - rename column and access my name

2 Answers 129 Views
GridView
Radek
Top achievements
Rank 1
Iron
Iron
Radek asked on 28 Dec 2022, 08:05 AM

I want to rename column, for example from gridViewTextBoxColumn9 to col_Name and then get access from code.

Instead dataGridView1.Columns["col_Name"].IsVisible = true I want to write col_Name.IsVisible = true

It is possible?

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 28 Dec 2022, 08:29 AM

Hello, Radek,

If the columns are added at design time, note that they are serialized automatically in the Designer.cs file:

Just below the InitializeComponent method, it is possible to see what are the exposed properties for accessing from the RadForm1.cs file. Note that it is not recommended to manipulate the Designer.cs file as it is expected to serialize the design time changes, but it is possible to define the columns outside the method as it is demonstrated below: 

        private void InitializeComponent()
        {
            this.gridViewTextBoxColumn1 = new Telerik.WinControls.UI.GridViewTextBoxColumn();
            Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn2 = new Telerik.WinControls.UI.GridViewTextBoxColumn();
            Telerik.WinControls.UI.TableViewDefinition tableViewDefinition1 = new Telerik.WinControls.UI.TableViewDefinition();
            this.radGridView1 = new Telerik.WinControls.UI.RadGridView();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterTemplate)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // radGridView1
            // 
            this.radGridView1.Location = new System.Drawing.Point(22, 29);
            // 
            // 
            // 
            gridViewTextBoxColumn1.HeaderText = "column1";
            gridViewTextBoxColumn1.Name = "column1";
            gridViewTextBoxColumn2.HeaderText = "column2";
            gridViewTextBoxColumn2.Name = "column2";
            this.radGridView1.MasterTemplate.Columns.AddRange(new Telerik.WinControls.UI.GridViewDataColumn[] {
            gridViewTextBoxColumn1,
            gridViewTextBoxColumn2});
            this.radGridView1.MasterTemplate.ViewDefinition = tableViewDefinition1;
            this.radGridView1.Name = "radGridView1";
            this.radGridView1.Size = new System.Drawing.Size(240, 150);
            this.radGridView1.TabIndex = 0;
            // 
            // RadForm1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 270);
            this.Controls.Add(this.radGridView1);
            this.Name = "RadForm1";
            this.Text = "RadForm1";
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterTemplate)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private Telerik.WinControls.UI.RadGridView radGridView1;
        private Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn1;

Then, it would be necessary to access the column: 

    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();

            this.gridViewTextBoxColumn1.IsVisible = false;
        }
    }

A better option is to add the columns programmatically. The following help article is quite useful on this topic:

https://docs.telerik.com/devtools/winforms/controls/gridview/columns/generating-columns#manual-column-generation 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Radek
Top achievements
Rank 1
Iron
Iron
answered on 29 Dec 2022, 09:14 AM

Thank you for your answer.
I will be created column programmatically because for me is better write code like that:
col_Name.IsVisible = true
instead
dataGridView1.Columns["col_Name"].IsVisible = true
and I do not need remember exact column name

Radek

Tags
GridView
Asked by
Radek
Top achievements
Rank 1
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Radek
Top achievements
Rank 1
Iron
Iron
Share this question
or