Problem with linq to sql and RadGridView

5 posts, 1 answers
  1. Paul
    Paul avatar
    16 posts
    Member since:
    Mar 2009

    Posted 25 Mar 2009 Link to this post

    Hi

    I am trying my first RadGridView bound to a bindingsource which is bound to a link to sql query result.

    When I intially setup the RadGridView I set the datasource to an object which is the linq to sql datacontext class for that table.

    I have formatted the grid to only display 2 of the many available fields and specially formated them size and header title.

    However the formatting changes I have made do not appear and I get a generic 2 50 char cols. However in the designer it looks ok.

    If I edit the designer.cs code and change the IsAutoGenerated to false my formatted cols appear but also two defaulted cols.

    What am I doing wrong?

    form.cs code
        public partial class OrganizationsForm : Telerik.WinControls.RadForm  
        {  
            private CallDataClassesDataContext db;  
     
            public OrganizationsForm()  
            {  
                InitializeComponent();  
     
                db = new CallDataClassesDataContext();  
     
                var OrganizationQuery = from organ in db.Organizations  
                                        orderby organ.Name  
                                        select new 
                                        {  
                                            organ.OrganizationId,  
                                            organ.Name  
                                        };  
     
                organizationBindingSource.DataSource = OrganizationQuery;  
            }  
        }  
     

    designer.cs code
            private void InitializeComponent()  
            {  
                this.components = new System.ComponentModel.Container();  
                Telerik.WinControls.UI.GridViewDecimalColumn gridViewDecimalColumn1 = new Telerik.WinControls.UI.GridViewDecimalColumn();  
                Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn1 = new Telerik.WinControls.UI.GridViewTextBoxColumn();  
                this.organizationBindingSource = new System.Windows.Forms.BindingSource(this.components);  
                this.radGridView1 = new Telerik.WinControls.UI.RadGridView();  
                ((System.ComponentModel.ISupportInitialize)(this.organizationBindingSource)).BeginInit();  
                ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).BeginInit();  
                ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterGridViewTemplate)).BeginInit();  
                ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();  
                this.SuspendLayout();  
                //   
                // organizationBindingSource  
                //   
                this.organizationBindingSource.DataSource = typeof(CallMaintain.Organization);  
                //   
                // radGridView1  
                //   
                this.radGridView1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(194)))), ((int)(((byte)(217)))), ((int)(((byte)(247)))));  
                this.radGridView1.Cursor = System.Windows.Forms.Cursors.Default;  
                this.radGridView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);  
                this.radGridView1.ForeColor = System.Drawing.SystemColors.ControlText;  
                this.radGridView1.ImeMode = System.Windows.Forms.ImeMode.NoControl;  
                this.radGridView1.Location = new System.Drawing.Point(13, 13);  
                //   
                //   
                //   
                this.radGridView1.MasterGridViewTemplate.AllowColumnChooser = false;  
                this.radGridView1.MasterGridViewTemplate.AllowColumnResize = false;  
                this.radGridView1.MasterGridViewTemplate.AllowDeleteRow = false;  
                this.radGridView1.MasterGridViewTemplate.AllowDragToGroup = false;  
                gridViewDecimalColumn1.AllowGroup = false;  
                gridViewDecimalColumn1.AllowResize = false;  
                gridViewDecimalColumn1.DataType = typeof(int);  
                gridViewDecimalColumn1.FieldAlias = "OrganizationId";  
                gridViewDecimalColumn1.FieldName = "OrganizationId";  
                gridViewDecimalColumn1.FormatInfo = new System.Globalization.CultureInfo("");  
                gridViewDecimalColumn1.HeaderText = "OrganizationId";  
                gridViewDecimalColumn1.IsAutoGenerated = true;  
                gridViewDecimalColumn1.ReadOnly = true;  
                gridViewDecimalColumn1.UniqueName = "OrganizationId";  
                gridViewDecimalColumn1.VisibleInColumnChooser = false;  
                gridViewDecimalColumn1.Width = 80;  
                gridViewTextBoxColumn1.AllowGroup = false;  
                gridViewTextBoxColumn1.FieldAlias = "Name";  
                gridViewTextBoxColumn1.FieldName = "Name";  
                gridViewTextBoxColumn1.FormatInfo = new System.Globalization.CultureInfo("");  
                gridViewTextBoxColumn1.HeaderText = "Name";  
                gridViewTextBoxColumn1.IsAutoGenerated = true;  
                gridViewTextBoxColumn1.UniqueName = "Name";  
                gridViewTextBoxColumn1.VisibleInColumnChooser = false;  
                gridViewTextBoxColumn1.Width = 250;  
                this.radGridView1.MasterGridViewTemplate.Columns.Add(gridViewDecimalColumn1);  
                this.radGridView1.MasterGridViewTemplate.Columns.Add(gridViewTextBoxColumn1);  
                this.radGridView1.MasterGridViewTemplate.DataSource = this.organizationBindingSource;  
                this.radGridView1.MasterGridViewTemplate.EnableGrouping = false;  
                this.radGridView1.Name = "radGridView1";  
                this.radGridView1.RightToLeft = System.Windows.Forms.RightToLeft.No;  
                this.radGridView1.ShowGroupPanel = false;  
                this.radGridView1.Size = new System.Drawing.Size(381, 150);  
                this.radGridView1.TabIndex = 0;  
                this.radGridView1.Text = "radGridViewPreview";  
                //   
                // OrganizationsForm  
                //   
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);  
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
                this.ClientSize = new System.Drawing.Size(406, 276);  
                this.Controls.Add(this.radGridView1);  
                this.Name = "OrganizationsForm";  
                this.Text = "Organizations";  
                this.ThemeName = "Office2007Blue";  
                ((System.ComponentModel.ISupportInitialize)(this.organizationBindingSource)).EndInit();  
                ((System.ComponentModel.ISupportInitialize)(this.radGridView1.MasterGridViewTemplate)).EndInit();  
                ((System.ComponentModel.ISupportInitialize)(this.radGridView1)).EndInit();  
                ((System.ComponentModel.ISupportInitialize)(this)).EndInit();  
                this.ResumeLayout(false);  
                this.PerformLayout();  
     
            }  
     

    Thanks Paul
  2. Answer
    Ramius
    Ramius avatar
    127 posts
    Member since:
    Feb 2008

    Posted 26 Mar 2009 Link to this post

    Hello Paul,

    your InitializeComponent() Method is missing the following line:

    this.radGridView1.MasterGridViewTemplate.AutoGenerateColumns = false;

    I hope this helps.

    Kind regards,

    Ramius

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 26 Mar 2009 Link to this post

    Hello Udo,

    When you change IsAutoGenerated design property to false, RadGridView binding generate new columns because AutoGenerateColumns at design time is always true. Please do not use and make changes to IsAutoGenerated property in the design-time code. You can format your auto generated columns at design time directly using Columns collection of MasterGridViewTemplate from VS PropertyGrid. About run time you can set AutoGenerateColumns to false and clear and setup columns collection manually.

    Sincerely yours,
    Julian Benkov
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
  5. Paul
    Paul avatar
    16 posts
    Member since:
    Mar 2009

    Posted 26 Mar 2009 Link to this post

    Hi Ramius and Julian

     

    Thanks for the answers.  Ramius that was the solution.  Adding that line right after the InitializeComponent(); solved the problem.

     

     

    I looked high and low for something like that and have relooked for that particular property in the VS properties and the Property Builder and can not see it.  Had it shown up in either place I would have used it.  Julian maybe this property should be in either VS properties or Property Builder or both.

     

    Julian I do know to not make changes to the designer.cs file, I was only doing that to give more information regarding the problem and the steps I had taken to already figure out the problem.  Making changes to the designer.cs file is a big No No always.

     

    Thanks again.

     

    Paul

  6. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 30 Mar 2009 Link to this post

    Hi Paul,

    Please view the attached screenshot.

    If you have additional questions, please contact us again.

    Kind regards,
    Julian Benkov
    the Telerik team

    Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Back to Top
UI for WinForms is Visual Studio 2017 Ready