简体   繁体   English

C# - DataGridView 和 DateTimePicker 的问题

[英]C# - Problem with DataGridView and DateTimePicker

I need help in understanding where I'm wrong, I'm using a DataGridView to allow the display of a table in the Form and I wanted to insert the function of selecting a range of dates with two DateTimePicker fields and start the SELECT Statement with a "CERCA" button, when, however, I start the program and try to make the selection in the DataGridView field only an empty line appears even if I have set two SelectCommand instructions.我需要帮助来理解我错在哪里,我正在使用 DataGridView 来允许在表单中显示表格,并且我想插入选择具有两个 DateTimePicker 字段的日期范围的功能并启动 SELECT 语句“CERCA”按钮,但是,当我启动程序并尝试在 DataGridView 字段中进行选择时,即使我设置了两个 SelectCommand 指令,也只会出现一个空行。 I don't understand where I'm wrong.我不明白我错在哪里。

    public DataGridView_Maria()
    {
        InitializeComponent();
    }
    SqlConnection conn = new SqlConnection("Data Source=192.168.0.51;Initial 
    Catalog=Archivio_Stipendi;User ID=sa;Password=NI2000ma;");

    private void CERCA_Button_Click(object sender, EventArgs e)
    {
        conn.Open();
        'SqlDataAdapter sdf = new SqlDataAdapter(@"SELECT GIORNO, RIPOSO, ORE_LAVORO, COMPENSO_LAVORO FROM [dbo].[MARIA_VARAVALLO] WHERE GIORNO BETWEEN '@DA_GIORNO' AND '@A_GIORNO';", conn);'

        sdf.SelectCommand.Parameters.AddWithValue("@DA_GIORNO", Da_Mar_DTP.Text);
        sdf.SelectCommand.Parameters.AddWithValue("@A_GIORNO", A_Mar_DTP.Text);

        DataTable sd = new DataTable();
        sdf.Fill(sd);
        DataGridView_Mar.DataSource = sd;

        conn.Close();
    }
}

I just made the change as per your advice and, by setting the date range directly from the query, the DataGridView works, hitting the "CERCA" button displays the set date.我刚刚根据您的建议进行了更改,并通过直接从查询设置日期范围,DataGridView 工作,点击“CERCA”按钮显示设置的日期。 Fantastic!极好的! But I still don't know why the DataTimePicker gives him problems ...但我仍然不知道为什么 DataTimePicker 会给他带来问题......

This is the modified code:这是修改后的代码:

    public DataGridView_Maria()
    {
        InitializeComponent();
    }
    SqlConnection conn = new SqlConnection("Data Source=192.168.0.51;Initial Catalog=Archivio_Stipendi;User ID=sa;Password=NI2000ma;");
    private void CERCA_Button_Click(object sender, EventArgs e)
    {
        conn.Open();
        SqlDataAdapter sdf = new SqlDataAdapter(@"SELECT GIORNO, RIPOSO, ORE_LAVORO, COMPENSO_LAVORO FROM [dbo].[MARIA_VARAVALLO] WHERE GIORNO BETWEEN 'Lunedì 09 Marzo 2020' AND 'Lunedì 09 Marzo 2020';", conn);
        DataTable sd = new DataTable();
        sdf.Fill(sd);
        DataGridView_Mar.DataSource = sd;

        conn.Close();
    }
}

} }

Instead of the Text properties of the DateTimePicker (s), try the Value properties, like this:尝试使用Value属性,而不是DateTimePicker (s) 的Text属性,如下所示:

sdf.SelectCommand.Parameters.AddWithValue("@DA_GIORNO", Da_Mar_DTP.Value);
sdf.SelectCommand.Parameters.AddWithValue("@A_GIORNO", A_Mar_DTP.Value);

Edit … I think you'll also need to remove the ' (single quotes) around the parameters in the query:编辑...我认为您还需要删除查询中参数周围的' (单引号):

WHERE GIORNO BETWEEN '@DA_GIORNO' AND '@A_GIORNO'

… becomes: ……变成:

WHERE GIORNO BETWEEN @DA_GIORNO AND @A_GIORNO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM