DataTable to List,返回并填充datagridview [英] DataTable to List, return and populate a datagridview

查看:96
本文介绍了DataTable to List,返回并填充datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我要从列表中填充数据网格视图。



我有一个表单,有一个datagridview,我想填充datagridview表单列表。



当表单打开时,他从另一个表单收到一个id,id为i需要从列表中的表中加载所有数据,然后从该列表返回列表,并读取我的表单中的列表并从列表中填充数据网格。











我知道很多代码都是红色的,但请一些人帮助我。





以下所有代码。



我这样做:



和表格代码:

Hello,

I what to populate a datagridview from a list.

I have a form, with a datagridview, i want to populate the datagridview form a list.

When the form is open, he receive an id from another form, with that id i need too load all data from a table in a list, and then from that list return the list, and read the list in my form and populate tha datagridview from tha list.





I know is a lot code to red, but please some one help me.


All code below.

I do something like that:

And the form code:

using Amanet_.Amanet.Core;


namespace Amanet_.VizualizareDate.VizualizareContracte
{
    public partial class winIstoricContracte : Form
    {
        public Contract istoricContract;
        public winIstoricContracte(int contractId)
        {
            InitializeComponent();
            this.istoricContract = SetariAmanet.IncarcaContract(contractId);
        }

        private void winIstoricContracteLoad(object sender, EventArgs e)
        {
            if (istoricContract == null)
            {
                MessageBox.Show(this, string.Format("Contractul {0} nu a fost gasit.", istoricContract), "Afișare istoric contract", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                
                
            }
        }
       
    }
}









一个名为Contract.cs的类,下面的部分代码:







A class named "Contract.cs", partial code below:

using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Amanet_.Amanet.Core
{
    public class Contract
    {
        
        private int clientId;
        private int numarContract;
        private DateTime dataCreare;
        private DateTime dataTerminare;
        private decimal procent;
        private DateTime dataInceput;

        
        public Contract(DataTable ValoareCautata)
        {
            this.numarContract = (int)ValoareCautata.Rows[0]["ContractId"];
            this.clientId = (int)ValoareCautata.Rows[0]["clientId"];
            this.dataCreare = (DateTime)ValoareCautata.Rows[0]["CreateDate"];
            this.dataInceput = (DateTime)ValoareCautata.Rows[0]["StartDate"];
            this.dataTerminare = (DateTime)ValoareCautata.Rows[0]["EndDate"];
            this.procent = (decimal)ValoareCautata.Rows[0]["Procent"];
        }

        public IstoricContracte[] IstoricOperatii
        {
            get
            {
                return SetariAmanet.IstoricContracte(this.numarContract);
            }
        }

        public int ClientId
        {
            get
            {
                return this.clientId;
            }
            set
            {
                this.clientId = value;
            }
        }
...... seme get and set.





第二节名为IstoricContracte.cs的部分代码如下:





Second class named "IstoricContracte.cs" partial code below:

using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Amanet_.Amanet.Core
{
    public class IstoricContracte
    {
        private string actiunea;
        private int nristoricContract;
        private int contractId;
        internal const string CREARE_CONTRACT = "creare contract";
        private DateTime data;
        private string descriere;
        internal const string PRELUNGIRE_CONTRACT = "prelungire contract";
        internal const string MODIFICARE_CONTRACT = "modificare contract";
        private decimal platit;
        private decimal incasat;

        public IstoricContracte()
        {
        }

        public IstoricContracte(DataTable ValoareContract)
        {
            this.nristoricContract = (int)ValoareContract.Rows[0]["ContractHistoryId"];
            this.contractId = (int)ValoareContract.Rows[0]["ContractId"];
            this.actiunea = (string)ValoareContract.Rows[0]["InsertAction"];
            this.descriere = (string)ValoareContract.Rows[0]["Description"];
            this.platit = (decimal)ValoareContract.Rows[0]["Payment"];
            this.incasat = (decimal)ValoareContract.Rows[0]["Price"];
            this.data = (DateTime)ValoareContract.Rows[0]["DateInsert"];
        }

.... some get and set...







名为SetariAmanet.cs的第三个类:






The third class named "SetariAmanet.cs":

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Amanet_.Amanet.Core
{
    internal static class SetariAmanet
    {
        private static T[] Lista<T>(DataTable valoare, Converter<DataTable, T> creaza)
        {
            List<T> list = new List<T>();
            if (valoare != null)
            {                
                 list.Add(creaza(valoare));
          
            }
            return list.ToArray();
        }
        internal static Contract IncarcaContract(int contractId)
        {
            Program.Connection.CommandText = "select * from Contracts where ContractId=@ContractId";
            Program.Connection.AddParameter("@ContractId", contractId);
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);
            if (Table.Rows.Count == 0)
            {
                throw new Exception(string.Format("Contractul {0} nu a fost gasit.", contractId));
            }
            return new Contract(Table);
        }

        internal static IstoricContracte[] IstoricContracte(int contractId)
        {
            Program.Connection.CommandText = "select * from ContractHistory where ContractId=@ContractId";
            Program.Connection.AddParameter("@ContractId", contractId);
            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);

            if (Table.Rows.Count == 0)
            {
                throw new Exception(string.Format("Contractul {0} nu a fost gasit.", contractId));
            }

            return Lista<IstoricContracte>(Table, delegate(DataTable rezultatTB)
            {
                return new IstoricContracte(rezultatTB);
            });
        }

    }
}

推荐答案

尝试将此数据转换为List

Try this for converting datatable to List
IEnumerable<DataRow> sequence = dt.AsEnumerable();
or

List<DataRow> list = dt.AsEnumerable().ToList();





希望这有助于



Hope this helps


这篇关于DataTable to List,返回并填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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