How do you put the name of the table into a function as a parameter when a copy of the class is created?



  • That's it. https://ru.stackoverflow.com/questions/472212/%D0%9A%D0%B0%D0%BA-%D0%BF%D0%B5%D1%80%D0%B5%D0%B4%D0%B0%D1%82%D1%8C-%D0%B8%D0%BC%D1%8F-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D1%8B-%D0%B2-%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D1%8E-%D0%BA%D0%B0%D0%BA-%D0%BF%D0%B0%D1%80%D0%B0%D0%BC%D0%B5%D1%82%D1%80 () I asked you to help me figure out how the name of the table could be transferred to a function as a parameter. I was very helpful. But now I have a bigger question.

    Now the name of the table needs to be transferred not to function, but to class, when it's called. The idea is:

    Got it. Form1♪ She's got it. menuStrip1♪ Three elements in the menu. Each of the elements is summarised. Form2 And when you call, you have to put the name of the table in there somehow.

    In turn Form2containing dataGridView1filled with data.

    To make it happen to me. Form2 Established a public method BindData()which completes the table. And I challenge him. Form1 every time you need to call Form2

    var form2 = new Form2();
                form2.BindData(c => c.Goods);
                form2.Show();
    

    QUESTION

    1. How good is this approach?
    2. Are there any better solutions to this task?

    Form1

    using System;
    using System.Windows.Forms;
    using System.Data.Entity;
    

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

        private void goodsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var Goods = new Form2();
            Goods.BindData(c => c.Goods);
            Goods.Show();
        }
    
        private void goodsListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var GoodsList = new Form2();
            GoodsList.BindData(c => c.GoodsList);
            GoodsList.Show();
        }
    }
    

    Form2

    using System;
    using System.Data;
    using System.Linq;
    using System.Windows.Forms;

    namespace WindowsFormsApplication2
    {
    public partial class Form2 : Form
    {
    public Form2()
    {
    InitializeComponent();
    }

        public void BindData<TABLE>(Func<Context, IQueryable<TABLE>> tableSelector)
        {
            using (Context context = new Context())
            {
                var items = from Items in tableSelector(context)
                            select Items;
    
                dataGridView1.DataSource = items.ToList();
            }
        }
    }
    

    }

    Model

    public class Goods
    {
    public int GoodsId { get; set; }
    public string Name { get; set; }
    }

    public class GoodsList
    {
    public int GoodsListId { get; set; }
    public string Name { get; set; }
    }

    public class Context : DbContext
    {
    public DbSet<Goods> Goods { get; set; }
    public DbSet<GoodsList> GoodsList { get; set; }

    public Context()
    {
        Database.SetInitializer(new DropCreateDatabaseAlways&lt;Context&gt;()); //Не обращайте внимания на эту строку.
    }
    

    }



  • To be honest, the code looks as bad as possible.

    UI should not know about the database.

    In fact, how do you imagine Form2 that can paint an arbitrary table? It's best to create a pack of specialized forms that can only display one type of data. And then the data can just be transmitted to the designer.

    But considering your question, if you have the form as smart as possible, you can try something:

      public partial class Form2<T> : Form
      {
        public Form2(Func<Context, IQueryable<T>> tableSelector)
        {
          InitializeComponent();
    
      using (Context context = new Context())
      {
        var items = from Items in tableSelector(context)
                    select Items;
        dataGridView1.DataSource = items.ToList();
      }
    }
    

    }

    That's why we need to fix another Form2.Designer.cs-- the class should not be called Form2, but... Form2<T>




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2