如何从datetimepicker获取数据在C#中 - [已解决] [英] how to get data from datetimepicker In C# - [Solved]
问题描述
你好......
我正在研究一个项目然后我遇到了从datetimepicker获取数据的问题...
代码显示没有错误但是我可以不要比较datetimepicker的日期......
我的问题是:
我想在DataGridView中打印数据,输入示例:数据来自1/3/2012到2012年2月3日
要知道我在数据库中只有1个日期,所以当我从......到...选择日期时,它应该在datagridview中显示数据.... br />
提前感谢
我的图片预览想要!!
Hello ...
Am working on a project and then i faced a problem getting data from datetimepicker ...
the code Below Showed no error but i can''t compare date from both datetimepicker ...
My Problem is :
I want to print the data In DataGridView that entered Example : data from 1/3/2012 To 2/3/2012
to be know that i have only 1 date in database so when i choose a date from ... to ... it should show data in datagridview ....
thanks in advance
Picture Preview of What i want to !!
private void btncount_Click(object sender, EventArgs e)
{
String from = datepickerfrom.Value.Date.ToString();
String to = datepickerto.Value.Date.ToString();
SqlConnection con = new SqlConnection(HomeCalculation.Properties.Settings.Default.Setting);
SqlCommand command = new SqlCommand("SELECT * FROM Expenses Where Date=@date AND Date=@date2",con);
command.Parameters.AddWithValue("@date",datepickerfrom.Value.Date);
command.Parameters.AddWithValue("@date2",datepickerto.Value.Date);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
如果你没有得到它只是告诉我解释我的问题更专业的方式:)
If You Didn''t Get it Just tell me to explain my problem way more professional :)
推荐答案
嗨waleedquwaider
我认为你必须更改你的查询,因为它用于比较两个字段,在这里你可以使用在数据范围之间。
Hi waleedquwaider
I think you have to change your query because and is use for compare two fields, here you can use Between for rang of data.
private void btncount_Click(object sender, EventArgs e)
{
String from = datepickerfrom.Value.Date.ToString();
String to = datepickerto.Value.Date.ToString();
SqlConnection con = new SqlConnection(HomeCalculation.Properties.Settings.Default.Setting);
SqlCommand command = new SqlCommand("SELECT * FROM Expenses Where Date BETWEEN @date AND Date=@date2",con);
command.Parameters.AddWithValue("@date",datepickerfrom.Value.Date);
command.Parameters.AddWithValue("@date2",datepickerto.Value.Date);
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
这篇关于如何从datetimepicker获取数据在C#中 - [已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!