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

Bound Radgridview exception when Keypress into the Search Row

4 Answers 78 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Fabrizio
Top achievements
Rank 1
Fabrizio asked on 05 Jul 2019, 01:27 PM

Hello

I contact you because I have been a problem for several days and I can not find the solution

I have a bound radgridview control in readonly. I allow to search row.

When I write something in the search row, my application is crashing.

You can download my simple example HERE

Thank you for your help

Good job, the telerik team  ;-)

Fabrizio

 

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;
using Telerik.WinControls.UI;
 
namespace test
{
 
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        #region School class
 
        public class School
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public short SchoolYear { get; set; }
            public string Locality { get; set; }
            public string Titulaire { get; set; }
            public short Degre { get; set; }
 
            public List<Student> Students = new List<Student>();
 
            public void AddStudent(Student student)
            {
                Students.Add(student);
            }
 
            public void RemoveStudent(Student student)
            {
                Students.Remove(student);
            }
 
            public int NombreEleve => Students.Count;
        }
 
        #endregion
 
        #region Student class
        public class Student
        {
            public Guid Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public Guid IdSchool { get; set; }
 
            public List<Cote> Cotes = new List<Cote>();
            public School School = new School();
 
            public string FullName => $"{LastName} {FirstName}";
            public string SchoolName => School.Name;
            public int NombreCote => Cotes.Count;
 
            public void AddCote(Cote cote)
            {
                Cotes.Add(cote);
            }
 
            public void RemoveCote(Cote cote)
            {
                Cotes.Remove(cote);
            }
        }
        #endregion
 
        #region Discipline class
 
        public class Discipline
        {
            public Guid Id { get; set; }
            public string Name { get; set; }
            public List<Control> Controls = new List<Control>();
 
            public int NombreControle => Controls.Count;
 
            public void AddControl(Control control)
            {
                Controls.Add(control);
            }
 
            public void RemoveControl(Control control)
            {
                Controls.Remove(control);
            }
        }
        #endregion
 
        #region Control class
        public class Control
        {
            public Guid Id { get; set; }
            public string Title { get; set; }
            public DateTime Date { get; set; }
            public decimal Quotation { get; set; }
            public short Period { get; set; }
            public Guid IdDiscipline { get; set; }
            public Discipline Discipline { get; set; }
            public List<Cote> Cotes = new List<Cote>();
 
            public void AddCote(Cote cote)
            {
                Cotes.Add(cote);
            }
 
            public void RemoveCote(Cote cote)
            {
                Cotes.Remove(cote);
            }
 
            public string DisciplineName => Discipline.Name;
            public int NombreCote => Cotes.Count;
        }
 
        #endregion
 
        #region Cote class
        public class Cote
        {
            public enum eQuotationState
            {
                Top = 0,
                Defeat,
                Average,
                Good,
                Absent
            }
 
            public Guid IdStudent { get; set; }
            public Guid IdControl { get; set; }
            public DateTime Date { get; set; }
            public decimal? Quotation { get; set; }
            public Guid Id { get; set; }
            public Control Control { get; set; }
            public Student Student { get; set; }
 
            public string FullNameStudent => Student?.FullName;
            public Guid? IdSchool => Student?.IdSchool;
            public string SchoolName => Student?.SchoolName;
            public string ControlTitle => Control?.Title;
            public Int16? ControlPeriod => Control?.Period;
            public Guid? IdDiscipline => Control?.IdDiscipline;
            public string DisciplineName => Control?.DisciplineName;
            public decimal ControlQuotation => Control?.Quotation ?? 0M;
            public bool IsAbsent => (Quotation == null);
            public eQuotationState QuotationState =>
            (
                IsAbsent ?
                eQuotationState.Absent :
                (Quotation < (Control.Quotation / 2)) ?
                eQuotationState.Defeat :
                (Quotation == (Control.Quotation / 2)) ?
                eQuotationState.Average :
                (Quotation == Control.Quotation) ?
                eQuotationState.Top :
                eQuotationState.Good
            );
        }
 
        #endregion
 
         
        private BindingList<School> _schools = new BindingList<School>();
        private BindingList<Student> _students = new BindingList<Student>();
        private BindingList<Discipline> _disciplines = new BindingList<Discipline>();
        private BindingList<Control> _controls = new BindingList<Control>();
        private BindingList<Cote> _cotes = new BindingList<Cote>();
         
        public RadForm1()
        {
            CreateDatas();
 
            InitializeComponent();
            CreateBoundQuotationGrid();
        }
 
         
        private void CreateDatas()
        {
            var school1 = new School { Id = Guid.NewGuid(), Degre = 1, Name = "School 1", SchoolYear = 2019, Titulaire = "Name 1" };
            _schools.Add(school1);
 
            var student1 = new Student { Id = Guid.NewGuid(), FirstName = "Toto", LastName = "Toto", School = school1, IdSchool = school1.Id };
            var student2 = new Student { Id = Guid.NewGuid(), FirstName = "Tata", LastName = "Tata", School = school1, IdSchool = school1.Id };
            _students.Add(student1);
            _students.Add(student2);
            school1.Students.AddRange(_students.Where(x => x.IdSchool == school1.Id));
 
            var discipline1 = new Discipline { Id = Guid.NewGuid(), Name = "French" };
            var discipline2 = new Discipline { Id = Guid.NewGuid(), Name = "English" };
            _disciplines.Add(discipline1);
            _disciplines.Add(discipline2);
 
            var control1 = new Control { Id = Guid.NewGuid(), Discipline = discipline1, IdDiscipline = discipline1.Id, Date = DateTime.Now, Period = 1, Title = "French control 1", Quotation = 10M };
            var control2 = new Control { Id = Guid.NewGuid(), Discipline = discipline1, IdDiscipline = discipline1.Id, Date = DateTime.Now, Period = 1, Title = "French control 2", Quotation = 20M };
            var control3 = new Control { Id = Guid.NewGuid(), Discipline = discipline1, IdDiscipline = discipline1.Id, Date = DateTime.Now, Period = 2, Title = "French control 3", Quotation = 30M };
            var control4 = new Control { Id = Guid.NewGuid(), Discipline = discipline2, IdDiscipline = discipline2.Id, Date = DateTime.Now, Period = 2, Title = "Ensglish control 1", Quotation = 100M };
            var control5 = new Control { Id = Guid.NewGuid(), Discipline = discipline2, IdDiscipline = discipline2.Id, Date = DateTime.Now, Period = 2, Title = "Ensglish control 2", Quotation = 200M };
            var control6 = new Control { Id = Guid.NewGuid(), Discipline = discipline2, IdDiscipline = discipline2.Id, Date = DateTime.Now, Period = 1, Title = "Ensglish control 3", Quotation = 300M };
            _controls.Add(control1);
            _controls.Add(control2);
            _controls.Add(control3);
            _controls.Add(control4);
            _controls.Add(control5);
            _controls.Add(control6);
            discipline1.Controls.AddRange(_controls.Where(x => x.IdDiscipline == discipline1.Id));
            discipline2.Controls.AddRange(_controls.Where(x => x.IdDiscipline == discipline2.Id));
 
            var cote1 = new Cote { Id = Guid.Empty, Date = DateTime.Now, Control = control1, IdControl = control1.Id, Student = student1, IdStudent = student1.Id, Quotation = 10M };
            var cote2 = new Cote { Id = Guid.Empty, Date = DateTime.Now, Control = control1, IdControl = control1.Id, Student = student2, IdStudent = student2.Id, Quotation = 0M };
            var cote3 = new Cote { Id = Guid.Empty, Date = DateTime.Now, Control = control2, IdControl = control2.Id, Student = student1, IdStudent = student1.Id, Quotation = 10M };
            var cote4 = new Cote { Id = Guid.Empty, Date = DateTime.Now, Control = control3, IdControl = control3.Id, Student = student1, IdStudent = student1.Id, Quotation = 30M };
            var cote5 = new Cote { Id = Guid.Empty, Date = DateTime.Now, Control = control4, IdControl = control4.Id, Student = student2, IdStudent = student2.Id, Quotation = 100M };
            var cote6 = new Cote { Id = Guid.Empty, Date = DateTime.Now, Control = control5, IdControl = control5.Id, Student = student2, IdStudent = student2.Id, Quotation = default(decimal?) };
            var cote7 = new Cote { Id = Guid.Empty, Date = DateTime.Now, Control = control6, IdControl = control6.Id, Student = student1, IdStudent = student1.Id, Quotation = 200M };
            var cote8 = new Cote { Id = Guid.Empty, Date = DateTime.Now, Control = control6, IdControl = control6.Id, Student = student2, IdStudent = student2.Id, Quotation = 0M };
            _cotes.Add(cote1);
            _cotes.Add(cote2);
            _cotes.Add(cote3);
            _cotes.Add(cote4);
            _cotes.Add(cote5);
            _cotes.Add(cote6);
            _cotes.Add(cote7);
            _cotes.Add(cote8);
            student1.Cotes.AddRange(_cotes.Where(x => x.IdStudent == student1.Id));
            student2.Cotes.AddRange(_cotes.Where(x => x.IdStudent == student2.Id));
            control1.Cotes.AddRange(_cotes.Where(x => x.IdControl == control1.Id));
            control2.Cotes.AddRange(_cotes.Where(x => x.IdControl == control2.Id));
            control3.Cotes.AddRange(_cotes.Where(x => x.IdControl == control3.Id));
            control4.Cotes.AddRange(_cotes.Where(x => x.IdControl == control4.Id));
            control5.Cotes.AddRange(_cotes.Where(x => x.IdControl == control5.Id));
            control6.Cotes.AddRange(_cotes.Where(x => x.IdControl == control6.Id));
        }
         
 
        /// <summary>
        /// Création des DGV adéquats
        /// </summary>
        private void CreateBoundQuotationGrid()
        {
            using (this.radGridView1.DeferRefresh())
            {
                radGridView1.ShowFilteringRow = true;
                radGridView1.EnableFiltering = true;
 
 
                radGridView1.MasterTemplate.Reset();
                radGridView1.MasterTemplate.ShowTotals = true;
                radGridView1.MasterTemplate.AddNewBoundRowBeforeEdit = true;
 
                radGridView1.MasterView.TableAddNewRow.IsPinned = true;
                radGridView1.MasterView.TableAddNewRow.PinPosition = PinnedRowPosition.Top;
                radGridView1.MasterView.TableSearchRow.CloseOnEscape = false;
                radGridView1.MasterView.TableSearchRow.ShowCloseButton = false;
 
                radGridView1.TableElement.RowHeaderColumnWidth = 50;
 
                // Ajouter la ligne des totaux
                radGridView1.SummaryRowsBottom.Add(new GridViewSummaryRowItem(new GridViewSummaryItem[]
                {
                    new GridViewSummaryItem("CoteColumn", "Total {0:N2}", GridAggregateFunction.Sum),
                    new GridViewSummaryItem("NomEleveColumn", "{0} cotes", GridAggregateFunction.Count)
                }));
 
                radGridView1.MasterView.SummaryRows[0].IsPinned = true;
                radGridView1.MasterView.SummaryRows[0].PinPosition = PinnedRowPosition.Bottom;
                radGridView1.BottomPinnedRowsMode = GridViewBottomPinnedRowsMode.Fixed;
 
                radGridView1.MasterTemplate.Columns.Add(new GridViewComboBoxColumn("PeriodeColumn", "ControlPeriod")
                {
                    HeaderText = "Period",
                    TextAlignment = ContentAlignment.MiddleCenter,
                    HeaderTextAlignment = ContentAlignment.MiddleCenter,
                    AutoSizeMode = BestFitColumnMode.AllCells,
                    MinWidth = 80,
                    DropDownStyle = RadDropDownStyle.DropDownList,
                    FilteringMode = GridViewFilteringMode.DisplayMember
                });
 
                radGridView1.MasterTemplate.Columns.Add(new GridViewComboBoxColumn("DisciplineColumn", "IdDiscipline")
                {
                    HeaderText = "Discipline",
                    TextAlignment = ContentAlignment.MiddleLeft,
                    HeaderTextAlignment = ContentAlignment.MiddleCenter,
                    AutoSizeMode = BestFitColumnMode.AllCells,
                    MinWidth = 160,
                    DropDownStyle = RadDropDownStyle.DropDownList,
                    ValueMember = "Id",
                    DisplayMember = "Name",
                    FilteringMode = GridViewFilteringMode.DisplayMember,
                    Tag = false
                });
 
                radGridView1.MasterTemplate.Columns.Add(new GridViewComboBoxColumn("LibelleControleColumn", "IdControl")
                {
                    HeaderText = "Control title",
                    TextAlignment = ContentAlignment.MiddleLeft,
                    HeaderTextAlignment = ContentAlignment.MiddleCenter,
                    AutoSizeMode = BestFitColumnMode.AllCells,
                    WrapText = true,
                    MinWidth = 160,
                    DropDownStyle = RadDropDownStyle.DropDownList,
                    ValueMember = "Id",
                    DisplayMember = "Title",
                    FilteringMode = GridViewFilteringMode.DisplayMember,
                    Tag = false
                });
 
                radGridView1.MasterTemplate.Columns.Add(new GridViewComboBoxColumn("LibelleClasseColumn", "IdSchool")
                {
                    HeaderText = "School name",
                    TextAlignment = ContentAlignment.MiddleLeft,
                    HeaderTextAlignment = ContentAlignment.MiddleCenter,
                    AutoSizeMode = BestFitColumnMode.AllCells,
                    WrapText = true,
                    MinWidth = 160,
                    DropDownStyle = RadDropDownStyle.DropDownList,
                    ValueMember = "Id",
                    DisplayMember = "Name",
                    FilteringMode = GridViewFilteringMode.DisplayMember,
                    Tag = false
                });
 
                radGridView1.MasterTemplate.Columns.Add(new GridViewComboBoxColumn("NomEleveColumn", "IdStudent")
                {
                    HeaderText = "Student fullname",
                    TextAlignment = ContentAlignment.MiddleLeft,
                    HeaderTextAlignment = ContentAlignment.MiddleCenter,
                    AutoSizeMode = BestFitColumnMode.AllCells,
                    MinWidth = 160,
                    DropDownStyle = RadDropDownStyle.DropDownList,
                    ValueMember = "Id",
                    DisplayMember = "FullName",
                    FilteringMode = GridViewFilteringMode.DisplayMember,
                    Tag = false
                });
 
                radGridView1.MasterTemplate.Columns.Add(new GridViewDateTimeColumn("DateColumn", "Date")
                {
                    HeaderText = "Date",
                    TextAlignment = ContentAlignment.MiddleCenter,
                    HeaderTextAlignment = ContentAlignment.MiddleCenter,
                    AutoSizeMode = BestFitColumnMode.AllCells,
                    MinWidth = 100,
                    MaxWidth = 100,
                    Format = DateTimePickerFormat.Short,
                    FormatString = "{0:d}",
                    Tag = true
                });
 
                radGridView1.MasterTemplate.Columns.Add(new GridViewDecimalColumn("CoteColumn", "Quotation")
                {
                    HeaderText = "Quotation",
                    TextAlignment = ContentAlignment.MiddleCenter,
                    HeaderTextAlignment = ContentAlignment.MiddleCenter,
                    AutoSizeMode = BestFitColumnMode.AllCells,
                    DecimalPlaces = 2,
                    ShowUpDownButtons = false,
                    AllowGroup = false,
                    Minimum = 0,
                    MinWidth = 130,
                    Tag = true
                });
 
                radGridView1.MasterTemplate.Columns.Add(new GridViewDecimalColumn("QuotationStateColumn", "QuotationState")
                {
                    HeaderText = "State",
                    TextAlignment = ContentAlignment.MiddleCenter,
                    HeaderTextAlignment = ContentAlignment.MiddleCenter,
                    AutoSizeMode = BestFitColumnMode.AllCells,
                    DecimalPlaces = 2,
                    ShowUpDownButtons = false,
                    ReadOnly = true
                });
            }
        }
 
        public void SetBindingColumnsQuotation()
        {
            var periodColumn = (this.radGridView1.MasterTemplate.Columns["PeriodeColumn"] as GridViewComboBoxColumn);
            periodColumn.DataSource = _controls.Select(x => x.Period).Distinct().OrderBy(x => x);
 
            var matiereColumn = (this.radGridView1.MasterTemplate.Columns["DisciplineColumn"] as GridViewComboBoxColumn);
            matiereColumn.DataSource = _controls.Select(x => x.Discipline).Distinct().OrderBy(x => x.Name);
 
            var controlColumn = (this.radGridView1.MasterTemplate.Columns["LibelleControleColumn"] as GridViewComboBoxColumn);
            controlColumn.DataSource = _controls.OrderBy(x => x.Title);
 
            var schoolColumn = (this.radGridView1.MasterTemplate.Columns["LibelleClasseColumn"] as GridViewComboBoxColumn);
            schoolColumn.DataSource = _students.Select(x => x.School).Distinct().OrderBy(x => x.Name);
 
            var studentColumn = (this.radGridView1.MasterTemplate.Columns["NomEleveColumn"] as GridViewComboBoxColumn);
            studentColumn.DataSource = _students.OrderBy(x => x.FullName);
        }
 
        private void RadForm1_Load(object sender, EventArgs e)
        {
            using (radGridView1.DeferRefresh())
                radGridView1.DataSource = _cotes;
 
            radGridView1.MasterTemplate.BestFitColumns(BestFitColumnMode.AllCells);
 
            SetBindingColumnsQuotation();
 
            WindowState = FormWindowState.Maximized;
        }
    }
 
}

4 Answers, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 08 Jul 2019, 08:46 AM
Hello Fabrizio,

Thank you for the sample project. This is caused by an issue in our implementation. I have logged this issue on our Feedback Portal. You can track its progress, subscribe to status changes and add your comment to it here. I have also updated your Telerik Points

To workaround this you need to create a custom GridViewSearchRowInfo class and override the GetCellFormattedValue method:
class MySerachRow : GridViewSearchRowInfo
{
    public MySerachRow(GridViewInfo viewInfo) : base(viewInfo)
    {
    }
 
    public override string GetCellFormattedValue(GridViewRowInfo row, GridViewColumn column)
    {
        if (row is GridViewSummaryRowInfo)
        {
            return "";
        }
        return base.GetCellFormattedValue(row, column);
    }
}

To replace the default instance use the following event: 
private void RadGridView1_CreateRowInfo(object sender, GridViewCreateRowInfoEventArgs e)
{
    if (e.RowInfo is GridViewSearchRowInfo)
    {
        e.RowInfo = new MySerachRow(e.ViewInfo);
    }
 
}

Should you have further questions, I would be glad to help.

Regards,
Dimitar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Fabrizio
Top achievements
Rank 1
answered on 08 Jul 2019, 09:53 AM
Thank you Dimitar.

I will try your solution, so this solution can resolve the bug ?

Where can I see my telerik points ?
Best regards
Fabrizio
0
Fabrizio
Top achievements
Rank 1
answered on 08 Jul 2019, 09:53 AM
Thank you Dimitar.

I will try your solution, so this solution can resolve the bug ?

Where can I see my telerik points ?
Best regards
Fabrizio
0
Dimitar
Telerik team
answered on 08 Jul 2019, 11:50 AM
Hi Fabrizio,

Yes this will remove the exception.

Do not hesitate to contact us if you have other questions.
 
Regards,
Dimitar
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Fabrizio
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Fabrizio
Top achievements
Rank 1
Share this question
or