C# 在 Datagridview 中显示两个日期之间的数据。来自 DatetimePicker 或文本框的日期
C# show data between two dates in Datagridview. Dates from DatetimePicker or Textbox
我正在使用 C# 编写代码,其中我使用 OLEDB 连接字符串连接 MS 访问数据库。
我有一个表单,可以根据某些条件在 datagridview 中显示数据库中的数据。
以下是标准:
a) 人物(来自
中的数据库
b) 流程(来自文本框中的数据库)
c) 从日期(日期时间选择器)
d) 迄今为止(日期时间选择器)
我想要的结果:首先我 select 人然后处理然后从日期然后到日期并单击查看按钮。应该根据上述标准显示来自 MS-Access 的数据 I selected.
我试过以下方法:
代码:
1. 对于人员和流程过滤器:
DataView DV = new DataView(dt1);
DV.RowFilter = string.Format("[Person] LIKE '%{0}%'", textBox5.Text);
dataGridView1.DataSource = DV;
- 对于两个日期之间的日期时间,我尝试了很多 Google 但没有找到答案。我在下面试过:
a)
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
string queryString = "";
queryString = "SELECT * FROM Table1 WHERE dob BETWEEN @startdate AND @enddate";
System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(queryString, con);
sqlCmd.Parameters.Add("@startdate", System.Data.SqlDbType.Date).Value = textBox7.Text;
sqlCmd.Parameters.Add("@enddate", System.Data.SqlDbType.Date).Value = textBox8.Text;
System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
GridView1.DataSource = dataSet;
GridView1.DataBind();
b)
string FD = "";
FD = dateTimePicker4.Value.ToString("dd-MM-yyyy");
string TD = "";
TD = dateTimePicker5.Value.ToString("dd-MM-yyyy");
connection.Close();
connection.Open();
OleDbCommand command123 = new OleDbCommand();
command123.Connection = connection;
string query123 = "select * from Table1 where [P Date] between date '"# + dateTimePicker4.Text.ToString() + #"' and date '"# + dateTimePicker5.Text.ToString() + #"'"
command123.CommandText = query123;
OleDbDataAdapter da123 = new OleDbDataAdapter(command123);
DataTable dt123 = new DataTable();
da123.Fill(dt123);
dataGridView1.DataSource = dt123;
c)
DataTable dt = new DataTable();
da.Fill(dt);
DataView DV = new DataView(dt1);
DV.RowFilter = string.Format("[P Date] >=" + textBox7.Text + " and <" + textBox8.Text + "");
dataGridView1.DataSource = DV;
d)
DataTable dt1 = new DataTable();
DataView DV = new DataView(dt1);
//DV.RowFilter = "[P Date] IN (#11/01/2019#, #11/11/2019#)";
//DV.RowFilter = "[P Date] >=#"+dateTimePicker4.Text+"# and [P Date] <=#"+dateTimePicker5.Text+"#";
//dataView.RowFilter = "Date IN (#12/31/2008#, #1/1/2009#)" // date time values
//DV.RowFilter ="([P Date] >=CDate('dateTimePicker4.Text')) and ([P Date] <=CDate('dateTimePicker5.Text'))";
//DV.RowFilter = string.Format(CultureInfo.InvariantCulture.DateTimeFormat, "([P Date]>=#{dateTimePicker4.text}#) and ([P Date] <=#{dateTimePicker5.Text}#)");
dataGridView1.DataSource = DV;
For connection I am trying OLEDB connection.
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabasePath.accdb;
Jet OLEDB:Database Password=password";
connection.Close();
connection.Open();
OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
//Select all column use belw query
string query = "select * from Table1";
command1.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command1);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
connection.Close();
this code is working but problem is that it shows all data not based on criteria based data.
I expect the output in Datagridview based on all criteria. Show only data which falls under all criteria.
我没看到你描述你的结果有什么问题。如果您正在寻求一般帮助如何做到这一点,这里有一个例子。请记住:示例很简单,没有使用参数查询。由你决定。
已转换 C#
string wQueryCreated, wQueryChanged, wQueryCreatedBy, wQueryChandedBy;
if (this.fiCreated.Checked == true)
wQueryCreated = " AND DateCreated >= '" + Format(this.dtpFiCreatedOd.Value, "yyyy-MM-dd 00:00") + "' AND DateCreated <= '" + Format(this.dtpFiCreatedDo.Value, "yyyy-MM-dd 23:59") + " ' ";
else
wQueryCreated = "";
if (this.fiChanged.Checked == true)
wQueryChanged = " AND DateModified BETWEEN '" + Format(this.dtpDateChangedFrom.Value, "yyyy-MM-dd") + "' AND '" + Format(this.dtpDateChangedTo.Value, "yyyy-MM-dd") + "' ";
else
wQueryChanged = "";
if (this.fiCreatedBy.Checked == true)
wQueryCreatedBy = " AND PersonCreatedBy = " + this.fiCreatedBy.SelectedValue + " ";
else
wQueryCreatedBy = "";
if (this.fiChandedBy.Checked == true)
wQueryChandedBy = " AND PersonModifiedBy = " + this.fiChandedBy.SelectedValue + " ";
else
wQueryChandedBy = "";
// use query conditions in SELECT statement
queryString = "SELECT * FROM Table1 WHERE DateDeleted IS NULL " + wQueryCreated + wQueryChanged + wQueryCreatedBy + wQueryChandedBy + "; ";
原文VB.NET:
Dim wQueryCreated, wQueryChanged, wQueryCreatedBy, wQueryChandedBy As String
If Me.fiCreated.Checked = True Then ' filter date created
wQueryCreated = " AND DateCreated >= '" & Format(Me.dtpFiCreatedOd.Value, "yyyy-MM-dd 00:00") & "' AND DateCreated <= '" & Format(Me.dtpFiCreatedDo.Value, "yyyy-MM-dd 23:59") & " ' "
else
wQueryCreated = ""
End If
If Me.fiChanged.Checked = True Then ' filter datechanged
wQueryChanged = " AND DateModified BETWEEN '" & Format(Me.dtpDateChangedFrom.Value, "yyyy-MM-dd") & "' AND '" & Format(Me.dtpDateChangedTo.Value, "yyyy-MM-dd") & "' "
else
wQueryChanged = ""
End If
If Me.fiCreatedBy.Checked = True Then ' filter chandged by person
wQueryCreatedBy = " AND PersonCreatedBy = " & Me.fiCreatedBy.SelectedValue & " "
else
wQueryCreatedBy = ""
End If
If Me.fiChandedBy.Checked = True Then ' filter modified by person
wQueryChandedBy = " AND PersonModifiedBy = " & Me.fiChandedBy.SelectedValue & " "
else
wQueryChandedBy = ""
End If
' use query conditions in SELECT statement
queryString = "SELECT * FROM Table1 WHERE DateDeleted IS NULL " & wQueryCreated & wQueryChanged & wQueryCreatedBy & wQueryChandedBy & "; "
这个例子使用了4个条件,总共从6个字段中获取信息(dtp前缀意味着DateTimePicker
)。注意第一个和第二个条件之间的日期处理有点不同,它们做类似的事情。
另请注意,此方法需要 1 个固定条件。它可能是 1=1
,但很明显,您通常会像我在示例中那样使用它,即排除软删除的行 (DateDeleted IS NULL
)。您需要 1 个固定条件,这样您就不必关心其余条件的开头 " AND "
,无论它们的用法如何。
您可以使用 "endless" 条件并组合它们,即使是不同的数据类型。如果您需要限制条件组合,请在 运行 查询之前验证它们是否已填写。显然,你不必测试检查CheckBoxes
,你可以检查ComboBox1.SelectedValue > -1
(在ComboBox
中选择了一些东西),或者TextBox1.Text.Length >= 3
(至少3个字符是进入TextBox
),等等
我正在使用 C# 编写代码,其中我使用 OLEDB 连接字符串连接 MS 访问数据库。 我有一个表单,可以根据某些条件在 datagridview 中显示数据库中的数据。 以下是标准:
a) 人物(来自
中的数据库b) 流程(来自文本框中的数据库)
c) 从日期(日期时间选择器)
d) 迄今为止(日期时间选择器)
我想要的结果:首先我 select 人然后处理然后从日期然后到日期并单击查看按钮。应该根据上述标准显示来自 MS-Access 的数据 I selected.
我试过以下方法:
代码: 1. 对于人员和流程过滤器:
DataView DV = new DataView(dt1);
DV.RowFilter = string.Format("[Person] LIKE '%{0}%'", textBox5.Text);
dataGridView1.DataSource = DV;
- 对于两个日期之间的日期时间,我尝试了很多 Google 但没有找到答案。我在下面试过:
a)
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
string queryString = "";
queryString = "SELECT * FROM Table1 WHERE dob BETWEEN @startdate AND @enddate";
System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(queryString, con);
sqlCmd.Parameters.Add("@startdate", System.Data.SqlDbType.Date).Value = textBox7.Text;
sqlCmd.Parameters.Add("@enddate", System.Data.SqlDbType.Date).Value = textBox8.Text;
System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
GridView1.DataSource = dataSet;
GridView1.DataBind();
b)
string FD = "";
FD = dateTimePicker4.Value.ToString("dd-MM-yyyy");
string TD = "";
TD = dateTimePicker5.Value.ToString("dd-MM-yyyy");
connection.Close();
connection.Open();
OleDbCommand command123 = new OleDbCommand();
command123.Connection = connection;
string query123 = "select * from Table1 where [P Date] between date '"# + dateTimePicker4.Text.ToString() + #"' and date '"# + dateTimePicker5.Text.ToString() + #"'"
command123.CommandText = query123;
OleDbDataAdapter da123 = new OleDbDataAdapter(command123);
DataTable dt123 = new DataTable();
da123.Fill(dt123);
dataGridView1.DataSource = dt123;
c)
DataTable dt = new DataTable();
da.Fill(dt);
DataView DV = new DataView(dt1);
DV.RowFilter = string.Format("[P Date] >=" + textBox7.Text + " and <" + textBox8.Text + "");
dataGridView1.DataSource = DV;
d)
DataTable dt1 = new DataTable();
DataView DV = new DataView(dt1);
//DV.RowFilter = "[P Date] IN (#11/01/2019#, #11/11/2019#)";
//DV.RowFilter = "[P Date] >=#"+dateTimePicker4.Text+"# and [P Date] <=#"+dateTimePicker5.Text+"#";
//dataView.RowFilter = "Date IN (#12/31/2008#, #1/1/2009#)" // date time values
//DV.RowFilter ="([P Date] >=CDate('dateTimePicker4.Text')) and ([P Date] <=CDate('dateTimePicker5.Text'))";
//DV.RowFilter = string.Format(CultureInfo.InvariantCulture.DateTimeFormat, "([P Date]>=#{dateTimePicker4.text}#) and ([P Date] <=#{dateTimePicker5.Text}#)");
dataGridView1.DataSource = DV;
For connection I am trying OLEDB connection.
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DatabasePath.accdb;
Jet OLEDB:Database Password=password";
connection.Close();
connection.Open();
OleDbCommand command1 = new OleDbCommand();
command1.Connection = connection;
//Select all column use belw query
string query = "select * from Table1";
command1.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command1);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
connection.Close();
this code is working but problem is that it shows all data not based on criteria based data.
I expect the output in Datagridview based on all criteria. Show only data which falls under all criteria.
我没看到你描述你的结果有什么问题。如果您正在寻求一般帮助如何做到这一点,这里有一个例子。请记住:示例很简单,没有使用参数查询。由你决定。
已转换 C#
string wQueryCreated, wQueryChanged, wQueryCreatedBy, wQueryChandedBy;
if (this.fiCreated.Checked == true)
wQueryCreated = " AND DateCreated >= '" + Format(this.dtpFiCreatedOd.Value, "yyyy-MM-dd 00:00") + "' AND DateCreated <= '" + Format(this.dtpFiCreatedDo.Value, "yyyy-MM-dd 23:59") + " ' ";
else
wQueryCreated = "";
if (this.fiChanged.Checked == true)
wQueryChanged = " AND DateModified BETWEEN '" + Format(this.dtpDateChangedFrom.Value, "yyyy-MM-dd") + "' AND '" + Format(this.dtpDateChangedTo.Value, "yyyy-MM-dd") + "' ";
else
wQueryChanged = "";
if (this.fiCreatedBy.Checked == true)
wQueryCreatedBy = " AND PersonCreatedBy = " + this.fiCreatedBy.SelectedValue + " ";
else
wQueryCreatedBy = "";
if (this.fiChandedBy.Checked == true)
wQueryChandedBy = " AND PersonModifiedBy = " + this.fiChandedBy.SelectedValue + " ";
else
wQueryChandedBy = "";
// use query conditions in SELECT statement
queryString = "SELECT * FROM Table1 WHERE DateDeleted IS NULL " + wQueryCreated + wQueryChanged + wQueryCreatedBy + wQueryChandedBy + "; ";
原文VB.NET:
Dim wQueryCreated, wQueryChanged, wQueryCreatedBy, wQueryChandedBy As String
If Me.fiCreated.Checked = True Then ' filter date created
wQueryCreated = " AND DateCreated >= '" & Format(Me.dtpFiCreatedOd.Value, "yyyy-MM-dd 00:00") & "' AND DateCreated <= '" & Format(Me.dtpFiCreatedDo.Value, "yyyy-MM-dd 23:59") & " ' "
else
wQueryCreated = ""
End If
If Me.fiChanged.Checked = True Then ' filter datechanged
wQueryChanged = " AND DateModified BETWEEN '" & Format(Me.dtpDateChangedFrom.Value, "yyyy-MM-dd") & "' AND '" & Format(Me.dtpDateChangedTo.Value, "yyyy-MM-dd") & "' "
else
wQueryChanged = ""
End If
If Me.fiCreatedBy.Checked = True Then ' filter chandged by person
wQueryCreatedBy = " AND PersonCreatedBy = " & Me.fiCreatedBy.SelectedValue & " "
else
wQueryCreatedBy = ""
End If
If Me.fiChandedBy.Checked = True Then ' filter modified by person
wQueryChandedBy = " AND PersonModifiedBy = " & Me.fiChandedBy.SelectedValue & " "
else
wQueryChandedBy = ""
End If
' use query conditions in SELECT statement
queryString = "SELECT * FROM Table1 WHERE DateDeleted IS NULL " & wQueryCreated & wQueryChanged & wQueryCreatedBy & wQueryChandedBy & "; "
这个例子使用了4个条件,总共从6个字段中获取信息(dtp前缀意味着DateTimePicker
)。注意第一个和第二个条件之间的日期处理有点不同,它们做类似的事情。
另请注意,此方法需要 1 个固定条件。它可能是 1=1
,但很明显,您通常会像我在示例中那样使用它,即排除软删除的行 (DateDeleted IS NULL
)。您需要 1 个固定条件,这样您就不必关心其余条件的开头 " AND "
,无论它们的用法如何。
您可以使用 "endless" 条件并组合它们,即使是不同的数据类型。如果您需要限制条件组合,请在 运行 查询之前验证它们是否已填写。显然,你不必测试检查CheckBoxes
,你可以检查ComboBox1.SelectedValue > -1
(在ComboBox
中选择了一些东西),或者TextBox1.Text.Length >= 3
(至少3个字符是进入TextBox
),等等