B
B
balikus2014-08-09 12:55:06
C++ / C#
balikus, 2014-08-09 12:55:06

How to bind data from listbox to DataGridView using bindingSource.DataSource?

Getting familiar with C#. There are, in my opinion, quite sensible youtube lessons from Defazze on the principles of programming in C # and in the 15th I stumbled in this code - the author does not respond:

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.Collections;
using System.Reflection;
using Microsoft.SqlServer.Server;

namespace Tree3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 

            List<Person> people=new List<Person>
            {
                new Person()
                {
                    Familia="Ивановский",
                    Imia="Иван",
                    Pater="Иваныч",
                    Bday=new DateTime(1980,12,1)
                },

                new Person()
                {
                    Familia="Иванов",
                    Imia="Иван",
                    Pater="Иваныч",
                    Bday=new DateTime(1981,10,18)
                }
            };

            bindingSource1.DataSource=people;
        }
    }
}

My program with the Person class and the corresponding properties compiles, but the result is a table with empty strings along the length of the list. But such lines output the following to the console: Console.WriteLine(people[0].Familia); -> Ivanovsky... Console.WriteLine(bindingSource1.DataSource); -> System.Collections.Generic.List`1[Tree3.Person] ...Of course, the author displays everything normally. How it was necessary to write the Person class so that the list is displayed in the table. After all, most likely the error lies in this class. I have this:
using System;

namespace Tree3
{
    public class Person
    {
        public DateTime Bday { get; set; }
        public string Familia { get; set; }
        public string Imia { get; set; }
        public string Pater { get; set; }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
balikus, 2014-08-11
@balikus

The thing is that it was necessary not to drag the new bindingSource1 from the toolbar to the form and try to bind it, but directly in the DataGrid to set the data source class (in the presented menu - the object) Person, which will automatically create personBindingSource and connect the grid with the object. You can then edit the column headings from the default ones.

S
Sumor, 2014-08-11
@Sumor

Everything is fine with the class and binding. You have an error in the description of the display in the DataGridView. Therefore, Xaml is needed to determine your error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question