本文介绍了选择datetimepicker值时,Datagridview显示为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



当我选择From Date和Todate from datetimepicker来显示datagridview中的数据时datagridview显示我的null数据,代码写入按钮点击evet



我的尝试:



Hi all,

When i choose From Date and Todate from datetimepicker to show data in datagridview at that time datagridview show me null data , code is written button click evet

What I have tried:

 DateTime startT = new DateTime();
                DateTime endT = new DateTime();
                startT = dateTimePicker1.Value.Date;
                endT = dateTimePicker2.Value.Date;
                if (startT.Date > endT.Date)
                {
                    MessageBox.Show("To Date Cannot be greater than Start Date");
                }
                else
                {
                    string connetionString = null;
                    connetionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
                    con.ConnectionString = connetionString;

                    DataSet ds = new DataSet();
                    string sql = "SELECT Medicine_name,sum(Medicine_count) as Medicine_count,To_Date from Medicine_count where [To_Date] Between #" + startT.ToString("dd'/'MM'/'yyyy") + "#And #" + endT.ToString("dd'/'MM'/'yyyy") + "#group by Medicine_name,Medicine_count,To_Date";
                    OleDbConnection connection = new OleDbConnection(connetionString);
                    OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
                    ds = new DataSet();
                    connection.Open();
                    dataadapter.Fill(ds);
                    connection.Close();
                    dataGridView1.DataSource = ds;
                    temp = true;
}

推荐答案

string sql = "SELECT Medicine_name, SUM(Medicine_count) AS Medicine_count, To_Date FROM Medicine_count WHERE [To_Date] BETWEEN ? AND ? GROUP BY Medicine_name,Medicine_count,To_Date";
OleDbConnection connection = new OleDbConnection(connetionString);
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection);
dataadapter.SelectCommand.Parameters.AddWithValue("?", startT);
dataadapter.SelectCommand.Parameters.AddWithValue("?", endT);





并且SELECT可能无法达到您想要的效果:GROUP BY会将列表中的每一列视为生成新列的原因行: []


这篇关于选择datetimepicker值时,Datagridview显示为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:15
查看更多