Gridvew不显示数据库中的数据 [英] Gridvew not showing data from the database
问题描述
当我在datetimepicker中选择日期时,数据不显示在数据库的gridvew中。
string query =从entryTable中选择txtDate,txtCustomerName,txtAddress,其中txtDate = @ myDate;
SqlConnection conn = new SqlConnection(connstr);
SqlDataAdapter dataAdapter = new SqlDataAdapter(query,conn);
dataAdapter.SelectCommand.Parameters.Add(@ myDate,DateTime.Now);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
dataGridView1.DataSource = table;
bindingSource1.DataSource = table;
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = bindingSource1;
请记住
... WHERE txtDate = @ myDate
只会返回完全相同的值:下降到毫秒,你可以找到你的问题,是你的数据库中没有符合你标准的记录 - 因此,你的观点确实是空的。
有几件事你可以这样做:
1)首先,证明这是问题:完全删除WHERE子句,并检查你的视图是否已完全填充。如果是,那就是问题。
2)尝试更改你的比较:如果你想要一天的所有记录,那么试试:
WHERE CAST(txtDate AS DATE ) BETWEEN CAST( @ myDate AS < span class =code-keyword> DATE ) AND CAST(DATEADD(dd, 1 , @ myDate ) AS DATE )
但是当你提到DateTimePicker时 - 你没有在你的代码中使用DateTimePicker,所以我怀疑你想要将DateTime.Now替换为对DTP的引用...
Hi,
Data does not showing in gridvew from database when i am selecting the date in the datetimepicker.
string query = "select txtDate,txtCustomerName,txtAddress from EntryTable where txtDate=@myDate"; SqlConnection conn = new SqlConnection(connstr); SqlDataAdapter dataAdapter = new SqlDataAdapter(query, conn); dataAdapter.SelectCommand.Parameters.Add("@myDate", DateTime.Now); SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter); DataTable table = new DataTable(); table.Locale = System.Globalization.CultureInfo.InvariantCulture; dataAdapter.Fill(table); dataGridView1.DataSource = table; bindingSource1.DataSource = table; dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); dataGridView1.ReadOnly = true; dataGridView1.DataSource = bindingSource1;
Bear in mind that
... WHERE txtDate=@myDate
will only return values which are EXACTLY the same: down to the millisecond and you may find your problem, is that there are no records in your DB which match your criteria - as a result, your view would indeed be empty.
There are a few things you can do:
1) First off, prove that this is the problem: remove the WHERE clause completely, and check that your view is fully populated. If it is, that's the problem.
2) Try changing your comparison: If you want all records on a single day, then try:
WHERE CAST(txtDate AS DATE) BETWEEN CAST(@myDate AS DATE) AND CAST(DATEADD(dd, 1, @myDate) AS DATE)
But when you mention DateTimePicker - you aren't using a DateTimePicker in your code, so I suspect you want to replace DateTime.Now with a reference to the DTP as well...
这篇关于Gridvew不显示数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!