GridViewColumn: FieldName vs Name - Incorrect documentation or bug?

16 posts, 1 answers
  1. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 23 Feb 2011 Link to this post

    According to the documentation for the GridView control (v2010.3.10.1215):

    FieldName
    Gets or sets the name data source property or database column to which the GridViewDataColumn is bound. 

    Name
    Gets or sets a string value representing the column's unique name in the Columns collection of the OwnerTemplate.  


    However, the code below does not display the value for SQL field "ProjectTitle"

            GridViewTextBoxColumn1.FieldName = "ProjectTitle"
            GridViewTextBoxColumn1.HeaderText = "Title"
            GridViewTextBoxColumn1.Name = "Title"

    Instead, I have to set the property Name to the SQL field. The property FieldName can be anything.

    GridViewTextBoxColumn1.FieldName = "Foobar"
            GridViewTextBoxColumn1.HeaderText = "Title"
            GridViewTextBoxColumn1.Name = "ProjectTitle"


    Can someone explain to me which is correct?  

    By the way, I am binding to a SQLDataReader and AutoGenerateColumns is false.

  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Hello,

    FieldName is indeed the data source name. And Name is the unique name given to the column by which you can refer to it.
    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 23 Feb 2011 Link to this post

    But it does not appear to be working that way.
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Hi,
    I'll prepare a short sample for you. You have mentioned you have looked at the documentation for the current version. Is this the version that you are using?
    Thanks
    Richard
  6. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    ... in the meatime, here is a sample to try.

    Designer File
    namespace RadControlsWinFormsApp2
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components;
      
            /// <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.radGridView1 = new Telerik.WinControls.UI.RadGridView();
                ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // radGridView1
                // 
                this.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
                this.radGridView1.Location = new System.Drawing.Point(0, 0);
                this.radGridView1.Name = "radGridView1";
                this.radGridView1.Size = new System.Drawing.Size(284, 262);
                this.radGridView1.TabIndex = 0;
                this.radGridView1.Text = "radGridView1";
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(284, 262);
                this.Controls.Add(this.radGridView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();
                this.ResumeLayout(false);
      
            }
      
            #endregion
      
            private Telerik.WinControls.UI.RadGridView radGridView1;
        }
    }

    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;
    using Telerik.WinControls.UI;
      
      
    namespace RadControlsWinFormsApp2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
      
            private void Form1_Load(object sender, EventArgs e)
            {
                this.radGridView1.AllowAddNewRow = false;
                this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;
                this.radGridView1.AutoGenerateColumns = false;
      
      
                List<Person> people = new List<Person>();
                people.Add(new Person("Richard", "1 The Mews", "Bournemouth"));
                people.Add(new Person("Chris", "2 Bucking Road", "Lymington"));
      
                GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn();
                nameColumn.HeaderText = "The Name";
                nameColumn.FieldName = "Name"; // data source name
                nameColumn.Name = "NameUniqueName";
                this.radGridView1.Columns.Add(nameColumn);
      
                GridViewTextBoxColumn address1Column = new GridViewTextBoxColumn();
                address1Column.HeaderText = "The Address 1";
                address1Column.FieldName = "Address1"; // data source name
                address1Column.Name = "Address1UniqueName";
                this.radGridView1.Columns.Add(address1Column);
      
                GridViewTextBoxColumn address2Column = new GridViewTextBoxColumn();
                address2Column.HeaderText = "The Address 2";
                address2Column.FieldName = "Address2"; // data source name
                address2Column.Name = "Address2UniqueName";
                this.radGridView1.Columns.Add(address2Column);
      
                this.radGridView1.DataSource = people;
            }
        }
      
        public class Person
        {
            public Person(string name, string address1, string address2)
            {
                this.Name = name;
                this.Address1 = address1;
                this.Address2 = address2;
            }
      
            public string Name
            { get; set; }
      
            public string Address1
            { get; set; }
      
            public string Address2
            { get; set; }
        }
    }


    edit//updated with the code from the correct sample

    Hope you find that helpful
    Richard
  7. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 23 Feb 2011 Link to this post

    Yes, I am using the current version.  

    In your sample code, you have Field and FieldName set to the same value.  What happens if you set Field to a different value, e.g. "Name2"?  It will not work.  I just tried it, and the entire application bombs out.  

    If you set the FieldName to another value but keep Name the same, the grid will load but that column has no data.

    It's as though the documentation (or logic) is reversed.


    ***  An interesting twist though, in your example, we're binding to a List.  In my application, I am binding to a SQLDataReader.  When binding to SQL, if you set the FieldName to bogus value but set Name to the SQL field, the grid WILL load that column with data.

    There is definitely an issue with those two properties and how the control reads properties from the objects it binds to.  I don't think it's just a documentation issue.

  8. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Hello,

    You may need to refresh your browser as I initially posted the incorrect sample (my apologies) here it is again.

    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;
    using Telerik.WinControls.UI;
      
      
    namespace RadControlsWinFormsApp2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
      
            private void Form1_Load(object sender, EventArgs e)
            {
                this.radGridView1.AllowAddNewRow = false;
                this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;
                this.radGridView1.AutoGenerateColumns = false;
      
      
                List<Person> people = new List<Person>();
                people.Add(new Person("Richard", "1 The Mews", "Bournemouth"));
                people.Add(new Person("Chris", "2 Bucking Road", "Lymington"));
      
                GridViewTextBoxColumn nameColumn = new GridViewTextBoxColumn();
                nameColumn.HeaderText = "The Name";
                nameColumn.FieldName = "Name"; // data source name
                nameColumn.Name = "NameUniqueName";
                this.radGridView1.Columns.Add(nameColumn);
      
                GridViewTextBoxColumn address1Column = new GridViewTextBoxColumn();
                address1Column.HeaderText = "The Address 1";
                address1Column.FieldName = "Address1"; // data source name
                address1Column.Name = "Address1UniqueName";
                this.radGridView1.Columns.Add(address1Column);
      
                GridViewTextBoxColumn address2Column = new GridViewTextBoxColumn();
                address2Column.HeaderText = "The Address 2";
                address2Column.FieldName = "Address2"; // data source name
                address2Column.Name = "Address2UniqueName";
                this.radGridView1.Columns.Add(address2Column);
      
                this.radGridView1.DataSource = people;
            }
        }
      
        public class Person
        {
            public Person(string name, string address1, string address2)
            {
                this.Name = name;
                this.Address1 = address1;
                this.Address2 = address2;
            }
      
            public string Name
            { get; set; }
      
            public string Address1
            { get; set; }
      
            public string Address2
            { get; set; }
        }
    }

    If you can try the sample and let me know.
    I will see if I can try a data reader too.
    Look forward to hearing back from you
    Richard
  9. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Hello,

    I've tried this with a SqlDataReader as the data source, using the sample I had before but just replacing the column names for the column names in a database. Again, if I set the FieldName to the datasource field then everything showed correctly. I.e.
    nameColumn.FieldName = "Name"; // data source name
    nameColumn.Name = "NameUniqueName";

    But if I changed it the other way, it would not show data.
    Richard
  10. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 23 Feb 2011 Link to this post

    I see what the issue was with your first sample.  I did not notice you were also using a view, and when I changed the Name it was not referenced correctly for the View.

    After reviewing my code further, I noticed I was using code from the documentation about "Binding to Datareader".

    If I use this code and the SQL field is "ProjectTitle", it WILL work and returns the correct data in column "Test".

    Dim nameColumn As New GridViewTextBoxColumn()
    nameColumn.HeaderText = "Test"
    nameColumn.FieldName = "Foobar"
    nameColumn.Name = "ProjectTitle"
    gridProjects.Columns.Add(nameColumn)
    gridProjects.MasterTemplate.LoadFrom(drProjectList)

    This code will NOT work and returns no data in column "Test".

    Dim nameColumn As New GridViewTextBoxColumn()
    nameColumn.HeaderText = "Test"
    nameColumn.FieldName = "ProjectTitle"
    nameColumn.Name = "Foobar"
    gridProjects.Columns.Add(nameColumn)
    gridProjects.MasterTemplate.LoadFrom(drProjectList)


    However, if I do not use the "LoadFrom" technique, and instead just use "DataSource", then everything works as expected.  

    'Change this
    gridProjects.MasterTemplate.LoadFrom(drProjectList)
     
    ' To this:
    gridProjects.DataSource = drProjectList


    So, can you reproduce the same issue if you change your SQL sample to use the "LoadFrom" technique?


  11. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Hello,

    The code that I posted above was changed. As per my other reply, I accidentally posted the wrong code initially. Apologies for that. It was updated a moment later.

    Yes, what you have said is also correct. The binding mechanism for LoadFrom is not the same as assigning a DataSource. In LoadFrom, this needs to loop over the reader and find the right column. As you may be aware, there are two ways to find column. By ordinal position or by name. In this case, it needs to find the column by name which is why you have this result.

    I hope that this has helped. If so, please remember to mark as answer and if you have further questions, do let me know
    Thanks
    Richard
  12. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 23 Feb 2011 Link to this post

    Thank you Richard.

    Can you explain to what is the reasoning for the two databinding methods?  Is LoadFrom optimal?

    I am not familiar with LoadFrom?  Is that a Telerik data method or is inherit from a base class? If not inherited, does the documentation describe it further?

  13. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Jason,

    I'd just thought I'd also mention (in case there was any doubt) that you don't even actually need to specify the FieldName at all when using LoadFrom

    Anything else, just let me know
    All the best
    Richard
  14. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Hi Jason,

    Our posts must have crossed. The LoadFrom method is, as far as I know a Telerik addition which builds a RadListSource, assigning the correct values to the correct columns. It loops over the data reader and finds the column by name, for the reader section with the same name.

    Anything else, do let me know
    Richard
  15. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 23 Feb 2011 Link to this post

    Yeap, I can find any other reference to it the .net namespace except for loading assemblies.

    I noticed that LoadFrom does not raise the DataBindingComplete event either.

    I've switched my code to use DataSource which feels more natural anyway. :-)  I just find it odd they don't use it in their documentation nor further explain the LoadFrom method.  Perhaps it's being phased out.

    Thank you for your assistance.
  16. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Here is the documentation for binding to a data reader, though this doesn't mention adding columns manually.
    The method is part of the GridViewTemplate class. i would have to test which is faster under which circumstances. In my view however, data readers are one of the fastest ways to access data in a SQL database.
    Richard
  17. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 23 Feb 2011 Link to this post

    Hi,

    no, the DataBindingComplete event wouldn't get raised as it is not assigning a datasource.
    All the best
    Richard
Back to Top
UI for WinForms is Visual Studio 2017 Ready