如何在日期时间选择器的所选日期的数据网格视图中显示数据?

时间:2013-09-06 06:30:49

标签: 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";
        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];
}

问题是它在点击时显示数据网格视图但不显示所选日期数据。

datetimepicker的编码区: -

private void dtTmPkrWtr_ValueChanged(object sender, EventArgs e)
{

}

3 个答案:

答案 0 :(得分:0)

您使用的是DateTime.Now,而不是DateTimePicker的选定值,请尝试以下操作:

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];
}

答案 1 :(得分: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];
}

答案 2 :(得分:0)

使用下面的code按日期 dataGridView 过滤数据,将此代码添加到ValueChanged {{1}事件1}}。我希望它会起作用:)。

dateTimePicker