datagridview填充,填充,数据绑定 [英] datagridview populate, fill, data bindig
问题描述
在我的表单中,我有一个datagridview,我想从数据库中填充datagridview并在填充datagridview之前进行一些操作.
我使用此代码进行选择:
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 =表格;
dataContracteClienti.DataSource = Table;
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
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
不会出现在datagridview中.
有建议吗?
还有其他方法可以在列表中显示数据吗?
, created in the selection does not appear in the datagridview.
Suggestions?
any other method to display data in list?
推荐答案
如何使用DataAdapter
填充< dataset>.然后,您可以执行数据集的数据表,例如表达式列,应用新的选择查询等.然后绑定到数据网格.
数据表可以是Queryable
作为linQ对象.您还可以从可绑定到数据网格的数据表中创建Custom DataViews
.以这些方式尝试,Google可能会为您提供帮助.
How about using aDataAdapter
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 beQueryable
as a linQ object. Also you can createCustom DataViews
from datatable which can be bind to the data grid. Try in those ways and google may help you.
这篇关于datagridview填充,填充,数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!