0

我已经采取了显示按钮,代码是:-

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)
{

}
4

3 回答 3

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];
}
于 2013-09-06T06:33:22.437 回答
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];
}
于 2013-09-06T06:35:28.983 回答
0

使用波纹管按日期code过滤数据dataGridView将此代码添加到ValueChangedEvent of dateTimePicker. 我希望它会工作:)

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);
        }
于 2015-10-27T06:45:58.630 回答