Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In my form i have a datagridview, I want to fill the datagridview from database and make some opereration befor filling the datagridview.

I use this code for selecting:

Program.Connection.CommandText = "select Contracts.ContractId, Contracts.StartDate, Contracts.EndDate, sum(ContractItems.Payment) AS Total_Platit from Contracts INNER JOIN ContractItems ON Contracts.ContractId = ContractItems.ContractId WHERE ClientID=@ClientID "
                + "GROUP BY Contracts.ContractId, Contracts.StartDate, Contracts.EndDate ORDER BY Contracts.StartDate";
            Program.Connection.AddParameter("@ClientID", cboNumeClient.SelectedValue.ToString());                    

            DataTable Table = new DataTable();
            Program.Connection.FillDataTable(Table, true);


dataContracteClienti.DataSource = Table;

MIDL
dataContracteClienti.Columns[0].HeaderText = "Nr. contract";
            dataContracteClienti.Columns[1].HeaderText = "Încheiat la";
            dataContracteClienti.Columns[2].HeaderText = "Expira la";
            dataContracteClienti.Columns[3].HeaderText = "Total plătit";


everything is ok.



Show me everything I selected, but also have other selected fields that do not want to appear as they are in the database, I want to do some mathematical calculations and then be displayed.


I tried

C#
public class ContracteClienti
        {
            public ContracteClienti(string NrContract, string sex, string dob, string data)
            {
                _nrContract = NrContract;
                _sex = sex;
                _dateOfBirth = dob;
                _date = Date;
            }
            public string NrContract
            {
                get { return _nrContract; }
                set { _nrContract = value; }
            }
            public string Sex
            {
                get { return _sex; }
                set { _sex = value; }
            }
            public string DateOfBirth
            {
                get { return _dateOfBirth; }
                set { _dateOfBirth = value; }
            }
            public string Date
            {
                get { return _date; }
                set { _date = value; }
            }
            private string _nrContract;
            private string _sex;
            private string _dateOfBirth;
            private string _date;
        }

        class PersonList : List<ContracteClienti>
        {
        }



PersonList people = new PersonList();

            for (int i = 0; i < Table.Rows.Count; i++)
            {

                people.Add(new ContracteClienti(Table.Rows[i]["ContractId"].ToString(), Table.Rows[i]["StartDate"].ToString(), Table.Rows[i]["EndDate"].ToString(), Table.Rows[i]["Total_Platit"].ToString()));

            }

            dataContracteClienti.DataSource = people;


But, for exemple the
Total_Platit
, created in the selection does not appear in the datagridview.

Suggestions?
any other method to display data in list?
Posted

1 solution

How about using a DataAdapter to fill a <dataset>. Then the dataset's datatable you may perform your calculations like expression columns, applying new selection queries etc. Then bind to the data grid.

The Datatable can be Queryable as a linQ object. Also you can create Custom DataViews from datatable which can be bind to the data grid. Try in those ways and google may help you.
 
Share this answer
 
Comments
aciobanita 27-Mar-11 10:00am    
I use:

dataContracteClienti.Rows.Clear();
Program.Connection.CommandText = "select Contracts.ContractId, Contracts.StartDate, Contracts.EndDate, sum(ContractItems.Payment) AS Total_Platit from Contracts INNER JOIN ContractItems ON Contracts.ContractId = ContractItems.ContractId WHERE ClientID=@ClientID "
+ "GROUP BY Contracts.ContractId, Contracts.StartDate, Contracts.EndDate ORDER BY Contracts.StartDate";
Program.Connection.AddParameter("@ClientID", cboNumeClient.SelectedValue.ToString());
DataTable Table = new DataTable();
Program.Connection.FillDataTable(Table, true);

Table.Columns.Add(new DataColumn("ExtendedPrice", typeof(Decimal), "Total_Platit * 1.2"));

foreach (DataRow Row in Table.Rows)
{
dataContracteClienti.Rows.Add(Row["ContractId"].ToString(), Convert.ToDateTime(Row["StartDate"].ToString()), Convert.ToDateTime(Row["EndDate"].ToString()), Convert.ToInt32(Row["Total_Platit"].ToString()), Row["ExtendedPrice"].ToString());
}


look's ok
aciobanita 27-Mar-11 10:04am    
As you see i use Convert.ToInt32(), because i set the column in the datagridview to a coustem format: 0,00 RON, is ok, or is another way to do?

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