Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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
            {
                
                
            }
        }
       
    }
}




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

C#
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.


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

C#
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...



The third class named "SetariAmanet.cs":

C#
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);
            });
        }

    }
}
Posted
Comments
Jameel VM 4-Feb-13 8:20am    
your question is not clear. is you need to convert datatable to list?
aciobanita constantin 4-Feb-13 8:35am    
Yes.

So, i have a class where i execute the sql queries (SetariAmanet.cs). I send to that class an ID. And i have a the class Contract.cs, and IstoricContract.cs (this is tha class with the information that i need). In the class Contract.cs i have created from
..
internal static IstoricContracte[] IstoricContracte(int contractId)
.. then execute
public IstoricContracte[] IstoricOperatii
{
get
{
return SetariAmanet.IstoricContracte(this.numarContract);
}
}
to obtain the information for a specific ID.

In SetariAmanet.cs i try to convert a datatable theat is created from


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();
}


return the list.

I don't know exactly how to execut in that order my code from my form, and the return list to populate a datagridview.
Jameel VM 4-Feb-13 8:21am    
you can set list as datasource to datagridview directly.then what pblm?
Jameel VM 4-Feb-13 8:41am    
I have posted a solution for converting datatable to list.please look in to that

1 solution

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

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


Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900