3层WinForms C#Datagrid。使用来自不同查询的值填充网格时遇到问题。 [英] 3-tier WinForms C# Datagrid. Having trouble filling the grid with values from different queries.

查看:47
本文介绍了3层WinForms C#Datagrid。使用来自不同查询的值填充网格时遇到问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我正在尝试使用来自不同查询的值填充我的datagridview。

I'm currently trying to fill my datagridview with values from different queries.

我已经开始尝试将一个查询的结果添加到特定列。问题是,因为我没有太多关注datagridviews我有点迷失我已经尝试查找不同的方法来向行添加值,只有大多数帖子或指南想要
使用SqlClient与经典" ; Sqlconnect con = new SqlConnect();"

I've started to try and add the result from one query to a specific column. Problem is that since i haven't had much focus on datagridviews i'm somewhat lost i've tried looking up different ways to add values to rows, only that most post or guides want to use SqlClient with the classic "Sqlconnect con= new SqlConnect();"

并使用与我正在使用的方法不同的方法绑定数据。

and bind the data with a different approach than what i'm using.

我'我专注于在我开始休息之前将1个查询添加到datagridview到正确的列,因为我相信让一个人工作可能是一个可以应用于其他查询的学习体验。

I'm focusing on getting 1 query added to the datagridview to the correct column before i start making the rest, since i believe getting one to work may be a learning experience that can be applied to the other queries.

这就是现在表示层的样子:

This how the presentation layer looks like right now:

//the code in presentation layer

            BusinessManager bm = new BusinessManager();
            
            int agentId = int.Parse(cbAgent.Text.Trim());
            int year = int.Parse(Year.Text.Trim());
            int month = int.Parse(Month.Text.Trim());
            var dtYear = new DateTime(year, 1, 1);
            var dtMonth = new DateTime(1, month, 1);
            
            bm.ChildInsurance(agentId, dtYear, dtMonth);

在datagridview中我有一个名为Child Insurrance的列,我希望得到的结果来自要添加的查询。

In the datagridview I have a column named Child Insurrance which I want the result from the query to be added.

在业务层中它看起来像这样:

in business layer it looks like this:

        public int ChildInsurrance(int agentId, DateTime year, DateTime month)
        {
           int sum = dm.ChildInsurranceSum(agentId, year, month);
            return sum;
        }




这是我在数据访问层的查询修改,现在我只想要在datagrid中发送和显示的值:

And this is the query in data access layer which i will modify, right now i'm just want the value sent and presented in the datagrid:

        public int ChildInsurranceSum (int agentId, DateTime year, DateTime month)
        {
            DataModelContainer db = new DataModelContainer();
            var sum = (from x in db.InsurranceSet
                      join y in db.PrivateApplicationSet on
                      x.Privateapplication.privateApplicationId equals y.privateApplicationId
                      where x.paydate == year & x.paydate == month && y.Agents.agentId == agentId
                      select x).Count();
            return sum;
                      
                      
        }

有什么建议吗?或者您需要更多信息?

Any suggestions? Or do you need more info?

问候 

Gab

注意:变量已被翻译为更容易理解。

NOTE: the variables has been translated to easier to understand.

瑞典布罗斯大学学生

推荐答案

如果您使用Winforms UI的某些架构模式,则可以解决它。我使用MVP(模型 - 视图 - 演示者)模式来使用WinForms。您创建与datagridview相关的模型并将其绑定到。因此,在模型对象中,您需要
的所有数据。该数据由演示者准备。因此,这意味着您从数据库中获取数据并创建模型。如果需要从另一个查询中获取某些数据,则演示者可以将其提供并将其添加到模型中。模型绑定到视图(datagridview组件)。
If you use some architectural pattern for Winforms UI you solve it. I use MVP (model-view-presenter) pattern to work with WinForms. You create model which is related to datagridview and bind it into. So in model object there are all data which you need in view. This data are prepared by presenter. So it means you take data from database and create to model. If some data are need to be taken from another query, presenter could give it and add it into model. Model is binded into view (datagridview component).


这篇关于3层WinForms C#Datagrid。使用来自不同查询的值填充网格时遇到问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆