Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Generics
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;
 
namespace ShowEntityOfInherited
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            List<student> All = new List<student>();
            Student s1 = new Student();
 
            s1.StudentName = "Moumit";
            s1.StudentRoll = 10;
            s1.StudentClass.KlassName = "V";
 
            Student s2 = new Student();
 
            s2.StudentName = "Moumit1";
            s2.StudentRoll = 11;
            s2.StudentClass.KlassName = "VI";
 
            All.Add(s1);
            All.Add(s2);
 
            dgv.BindingContext = new BindingContext();
            dgv.DataSource = All;
 
            dgv.Columns["StudentName"].HeaderText = "Name";
            dgv.Columns["StudentRoll"].HeaderText = "Roll";
            //dgv.Columns["KlassName"].HeaderText = "Klass"; //Not working
            //dgv.Columns["Klass.KlassName"].HeaderText = "Klass"; //Not working
        }
    }
 
    public class Klass
    {
        public string KlassName { get; set;}
    }
 
    public class Student
    {
        public string StudentName { get; set; }
        public int StudentRoll { get; set; }
        public Klass StudentClass { get; set; }
 
        public Student()
        {
            StudentClass = new Klass();
        }
    }
}

 
I wanna show Klass value in column to......... But it is showing class name instead ... how to solve it........
Posted 18-May-12 4:50am
Edited 18-May-12 4:56am
Wes Aday94.3K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The reason is that the DataGridView can display only one level of Tabular data. It cannot show Tables nested inside a cell. To get the desired result modify the code as follows:
namespace ShowEntityOfInherited
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            List<student> All = new List<student>();
            Student s1 = new Student();
 
            s1.StudentName = "Moumit";
            s1.StudentRoll = 10;
            s1.KlassName = "V";
 
            Student s2 = new Student();
 
            s2.StudentName = "Moumit1";
            s2.StudentRoll = 11;
            s2.KlassName = "VI";
 
            All.Add(s1);
            All.Add(s2);
 
            dgv.BindingContext = new BindingContext();
            dgv.DataSource = All;
 
            dgv.Columns["StudentName"].HeaderText = "Name";
            dgv.Columns["StudentRoll"].HeaderText = "Roll";
            dgv.Columns["KlassName"].HeaderText = "Klass"; 
            
        }
    }
 
 //   public class Klass
 //   {
 //       public string KlassName { get; set;}
 //   }
 
    public class Student
    {
        public string StudentName { get; set; }
        public int StudentRoll { get; set; }
        public string KlassName { get; set; }
 
//  public Student()
//        {
//            StudentClass = new Klass();
//        }
    }
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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 System.Reflection;
 
namespace ShowEntityOfInherited
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            List<student> All = new List<student>();
            
            Student s1 = new Student();
            s1.StudentName = "Moumit";
            s1.StudentRoll = 10;
            s1.StudentClass.KlassName = "V";
            s1.StudentClass.KlassMonitor = "RAM";
 
            Student s2 = new Student();
 
            s2.StudentName = "Moumit1";
            s2.StudentRoll = 11;
            s2.StudentClass.KlassName = "VI";
            s2.StudentClass.KlassMonitor = "Shyam";
 
            All.Add(s1);
            All.Add(s2);            
 
            //
            dgv.BindingContext = new BindingContext();
            dgv.DataSource = All;
 
            //
            if (!dgv.Columns.Contains("ABC"))
            {
                DataGridViewTextBoxColumn dc = new DataGridViewTextBoxColumn();
                dc.Name = "ABC";
                dgv.Columns.Add(dc);
            }
 
            //
            dgv.Columns["StudentName"].HeaderText = "Name";
            dgv.Columns["StudentRoll"].HeaderText = "Roll";
            dgv.Columns["StudentClass"].HeaderText = "Class";
            dgv.Columns["ABC"].HeaderText = "Monitor";          
        }
 
        private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            string cName = dgv.Columns[e.ColumnIndex].Name;
            if (dgv.Rows[e.RowIndex].DataBoundItem != null)
            {
                switch (cName)
                {
                    case "StudentClass":
                        e.Value = ((Student)dgv.Rows[e.RowIndex].DataBoundItem).StudentClass.KlassName;
                        break;
                    case "ABC":
                        e.Value = ((Student)dgv.Rows[e.RowIndex].DataBoundItem).StudentClass.KlassMonitor;
                        break;
                    default:
                        break;
 
                }
            }
        }
    }
 
    public class Klass
    {
        public string KlassName { get; set; }
        public string KlassMonitor { get; set; }
    }
 
    public class Student
    {
        public string StudentName { get; set; }
        public int StudentRoll { get; set; }
        public Klass StudentClass { get; set; }
 
        public Student()
        {
            StudentClass = new Klass();
        }
    }
} 
</student></student>
 
In that way it solved temporarily ...........
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 18 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100