22 January 2014

DataGridView not showing the DataSource (List, Array) in Grid

Hi Friends,

Sometime we bind the datasource with DataGridView object but still it doesn't show the data in grid view.

Consider we have a class Employee as

 class Employee
 {
     public string fname;
     public string sname;

     public Employee(string f, string s)
     {
         fname = f;
         sname = s;
     }
 }

and we bind List/Array of the Employee class as the datasource of the DataGridView in window form as below -

public partial class Form1 : Form
{
    List<Employee> lst = new List<Employee>();    
    
    public Form1()
    {
        InitializeComponent();

        lst.Add(new Employee("deepak1", "sharma"));
        lst.Add(new Employee("deepak2", "sharma"));
        lst.Add(new Employee("deepak3", "sharma"));                
        
        dataGridView1.DataSource = lst;
        dataGridView1.Refresh();        
    }

}

but when we open the form we are not able to see the data in datagridview.

The main reason is we don't have get set properties in Employee class.  Even the fname and sname are public but still we have to define the properties for the attribute we want to show.

You just need to modify two line in your Employee class.



 class Employee
 {
     public string fname; 
     public string sname; 
    
     public string fname { get; set; }
     public string sname { get; set; }
    
     public Employee(string f, string s)
     {
         fname = f;
         sname = s;
     }
 }

Now if you run the above code it will display the records in DataGridView. :)

Happy Code Sharing and Blogging.. :)

2 comments:

  1. Gracias totales por esto, el unico link que sirvió

    ReplyDelete
  2. My Tactics: Datagridview Not Showing The Datasource (List, Array) In Grid >>>>> Download Now

    >>>>> Download Full

    My Tactics: Datagridview Not Showing The Datasource (List, Array) In Grid >>>>> Download LINK

    >>>>> Download Now

    My Tactics: Datagridview Not Showing The Datasource (List, Array) In Grid >>>>> Download Full

    >>>>> Download LINK tG

    ReplyDelete