Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从日期时间选择器在选定日期的datagridview中显示数据?_C# - Fatal编程技术网

C# 如何从日期时间选择器在选定日期的datagridview中显示数据?

C# 如何从日期时间选择器在选定日期的datagridview中显示数据?,c#,C#,我已按下显示按钮,代码为:- private void btnDisplay_Click(object sender, EventArgs e) { DateTime selectedDate = DateTime.Now; String query = "SELECT sno,currDate,WtrNm,Type,No FROM WtrTblAllot WHERE currDate = @SelectedDate"; S

我已按下显示按钮,代码为:-

private void btnDisplay_Click(object sender, EventArgs e)
{            
        DateTime selectedDate = DateTime.Now;
        String query = "SELECT sno,currDate,WtrNm,Type,No FROM WtrTblAllot WHERE currDate = @SelectedDate";
        SqlCommand command = new SqlCommand(query, con);
        command.Parameters.AddWithValue("@SelectedDate", selectedDate);
        SqlDataAdapter da = new SqlDataAdapter(command);
        DataSet ds = new DataSet();
        da.Fill(ds);
        dgvWtrAllot.DataSource = ds.Tables[0];
}

问题是,它在单击时显示datagrid视图,但不显示选定的日期数据

日期时间选择器的编码区域:-

private void dtTmPkrWtr_ValueChanged(object sender, EventArgs e)
{

}

您正在使用
DateTime。现在
而不是
日期时间选择器的选定值,请尝试以下操作:

private void btnDisplay_Click(object sender, EventArgs e)
{
    DateTime selectedDate = dtTmPkrWtr.Value;
    String query = "SELECT sno,currDate,WtrNm,Type,No FROM WtrTblAllot WHERE currDate = @SelectedDate";
    SqlCommand command = new SqlCommand(query, con);
    command.Parameters.AddWithValue("@SelectedDate", new DateTime(selectedDate.Year, selectedDate.Month, selectedDate.Day, selectedDate.Hour, selectedDate.Minute, selectedDate.Second));
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dgvWtrAllot.DataSource = ds.Tables[0];
}

最好有方法加载数据

private void dtTmPkrWtr_ValueChanged(object sender, EventArgs e)
{            
       LoadGridData(dateTimePicker1.Value);
}

private void btnDisplay_Click(object sender, EventArgs e)
{            
       LoadGridData(DateTime.Now);     
}

private void LoadGridData(DateTime selectedDate)
{
    String query = "SELECT sno,currDate,WtrNm,Type,No FROM WtrTblAllot WHERE currDate = @SelectedDate";
    SqlCommand command = new SqlCommand(query, con);
    command.Parameters.AddWithValue("@SelectedDate", dateTimePicker1.Value);
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dgvWtrAllot.DataSource = ds.Tables[0];
}

使用下面的
code
按日期过滤
dataGridView
中的数据,将此代码添加到
dateTimePicker
ValueChanged
事件中。我希望它能奏效:)


“问题是它在单击时显示datagrid视图,但不显示选定的日期数据。”我不确定这意味着什么。你能详细说明一下吗?你能重新表述一下这个问题吗?我不知道你的问题是什么is@lc. 不显示数据。。。它只显示数据库头(例如sno.,currdate…)@V4Vendetta不显示数据。。。它只显示数据库头(例如sno.,currdate…)您确定存在值为
DateTime的实际记录吗。现在
?thanx@Damith!!!!您的代码正在工作,但未显示数据。。。它只显示数据库标题(例如sno.,currdate…)是的,这是因为您正在使用btnDisplay Click事件,该事件要求具有DateTime的行。现在,实际上不可能有具有DateTime的行。现在值,仅使用dtTmPkrWtr ValueChanged事件或使用我的解决方案。@trippino当我检查数据库表时,它显示的格式(currDate-'2013-09-05 13:03:10.983')但在date-time piker中,格式为2013年5月9日..那么你能帮我吗..@Damith当我检查数据库表时,它显示的格式是(currDate-'2013-09-05 13:03:10.983')但是在日期-时间选择器中,格式是2013年9月5日。所以你能帮我吗…这只是一个可视化的问题,如果你继续使用SqlParameters,你应该不会有问题。我建议你不要同时存储毫秒,否则你的查询将(几乎)从不返回行使用您的编码相同的问题仅数据库头不带数据@trippinoremove millizes from fields并查询并重试(为了删除毫秒,我更新了答案)。如果不需要,请考虑删除秒-分钟-小时。对于查询:使用我的更新答案,对于数据库:删除秒后的3位数字(例如:2013-09-05 13:03:10.983成为2013-09-05 13:03:10)。请注意DateTimePicker.Value提供了所选日期和当前时间,因此我认为您应该只保留日期部分,并删除小时-分钟-秒
try
        {

            con = new SqlConnection(cs.ConDB);
            con.Open();

            cmd = new SqlCommand(@"SELECT RTRIM(sno)as [sno],RTRIM(currDate) as [currDate],
            RTRIM(WtrNm) as [WtrNm],RTRIM(Type) as [Type],(No ) as [No] from WtrTblAllot where currDate like '" + currDate.Text + "%' order by currDate", con);
            SqlDataAdapter myDA = new SqlDataAdapter(cmd);
            DataSet WtrTblAllot = new DataSet();
            myDA.Fill(WtrTblAllot , "WtrTblAllot ");
            dataGridView1.DataSource = WtrTblAllot .Tables["WtrTblAllot "].DefaultView;
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }