This is a migrated thread and some comments may be shown as answers.

GridViewColumn: FieldName vs Name - Incorrect documentation or bug?

15 Answers 185 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jason Parrish
Top achievements
Rank 1
Jason Parrish asked on 23 Feb 2011, 07:04 PM
According to the documentation for the GridView control (v2010.3.10.1215):

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

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


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.

15 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 09:46 PM
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
0
Jason Parrish
Top achievements
Rank 1
answered on 23 Feb 2011, 09:47 PM
But it does not appear to be working that way.
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 09:52 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 09:57 PM
... 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
0
Jason Parrish
Top achievements
Rank 1
answered on 23 Feb 2011, 10:14 PM
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.

0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 10:22 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 10:37 PM
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
0
Jason Parrish
Top achievements
Rank 1
answered on 23 Feb 2011, 11:11 PM
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?


0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 11:25 PM
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
0
Jason Parrish
Top achievements
Rank 1
answered on 23 Feb 2011, 11:30 PM
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?

0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 11:34 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 11:40 PM
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
0
Jason Parrish
Top achievements
Rank 1
answered on 23 Feb 2011, 11:44 PM
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.
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 11:44 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 23 Feb 2011, 11:46 PM
Hi,

no, the DataBindingComplete event wouldn't get raised as it is not assigning a datasource.
All the best
Richard
Tags
GridView
Asked by
Jason Parrish
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Jason Parrish
Top achievements
Rank 1
Share this question
or