我正在尝试创建一个表单,用户可以在数据网格视图中查看数据库中的数据。我希望用户能够选择从一个组合框cmbDate开始的时间段和另一个组合框中的句点cmbDateTo.I已经编写了以下代码:
命名空间linqToSql_trial { public partial class frmMonthlyOperatorStatistics:Form { private userLoginDataContext dc;
public frmMonthlyOperatorStatistics()
{
InitializeComponent();
dc = new userLoginDataContext();
}
private void LoadData()
{
cmbDate.DataSource = dc.dailyOperatorStatistics.Select(x=>x.date);
cmbDate.DisplayMember = "date";
cmbDate.ValueMember = "date";
}
private void LoadDateTo()
{
cmbToDate.DataSource = from to in dc.dailyOperatorStatistics
select to;
cmbToDate.DisplayMember = "date";
cmbToDate.ValueMember = "date";
}
private void btnLoad_Click(object sender, EventArgs e)
{
this.operatorStatDataGridView.DataSource = dc.dailyOperatorStatistics.Where(x => x.date >= Convert.ToDateTime(cmbDate.SelectedItem) && <= Convert.ToDateTime(cmbToDate.SelectedItem));
}
private void frmMonthlyOperatorStatistics_Load(object sender, EventArgs e)
{
LoadData();
LoadDateTo();
}
} } 它通过零件&lt; = Convert.ToDateTime(cmbToDate.SelectedItem))在按钮点击方法上产生错误;
答案 0 :(得分:0)
我设法得到了解决方案,我只是在btnLoad_clik方法中编写了以下代码。
this.operatorStatDataGridView.DataSource = dc.dailyOperatorStatistics.Where(x =&gt; x.date&gt; = Convert.ToDateTime(cmbDate.SelectedItem)&amp;&amp; x.date&lt; = Convert.ToDateTime(cmbToDate.SelectedItem) ));
并将loadToDate方法更改为此
private void LoadDateTo() { cmbToDate.DataSource = dc.dailyOperatorStatistics.Select(x =&gt; x.date); cmbToDate.DisplayMember =“date”; cmbToDate.ValueMember =“date”;
}
这已经能够给我所需的结果。