 |
|
Windows Form ขอถามเรืองการเลือกดูข้อมูลตามวันที่ให้แสดงผ่าน DataGridView ใน C# ค่ะ |
|
 |
|
|
 |
 |
|
ต้องการจะให้แสดงข้อมูลตามระหว่างวันที่ที่เลือกค่ะ เช่น ให้แสดงลิสต์ข้อมูลตั้งแต่วันที่7ส.ค. ถึงวันที่ 10ส.ค.

Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form6 : Form
{
public Form6()
{
InitializeComponent();
}
private void logBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.logBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.internetCafe2DataSet);
}
private void Form6_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'internetCafe2DataSet.Log' table. You can move, or remove it, as needed.
this.logTableAdapter.Fill(this.internetCafe2DataSet.Log);
// TODO: This line of code loads data into the 'internetCafe2DataSet.Log' table. You can move, or remove it, as needed.
this.logTableAdapter.Fill(this.internetCafe2DataSet.Log);
}
private void logBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
{
this.Validate();
this.logBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.internetCafe2DataSet);
}
private void button1_Click(object sender, EventArgs e)
{
Form5 frm = new Form5();
frm.Show();
this.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
string StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/oil/ite-304/project/test/WindowsFormsApplication1/InternetCafe2.accdb;Persist Security Info=False";
OleDbConnection Conn = new OleDbConnection(StrConn);
Conn.Open();
StringBuilder sb = new StringBuilder();
sb.Append("SELECT ID,[DateTime],[Number],RCredit");
sb.Append(" FROM log");
sb.Append(" WHERE [DateTime]");
sb.Append(" between @DateTime1 and @DateTime2");
string login = sb.ToString();
OleDbCommand com = new OleDbCommand(login, Conn);
com.Parameters.Clear();
com.Parameters.Add("@DateTime1", SqlDbType.NVarChar).Value = dateTimePicker1.Text.Trim();
com.Parameters.Add("@DateTime2", SqlDbType.NVarChar).Value = dateTimePicker2.Text.Trim();
OleDbDataReader dr = com.ExecuteReader();
if (dr.HasRows)
{
string StrCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:/oil/ite-304/project/test/WindowsFormsApplication1/InternetCafe2.accdb;Persist Security Info=False";
OleDbConnection Con = new OleDbConnection(StrConn);
Con.Open();
dr.Read();
dr.Close();
OleDbCommand con1 = new OleDbCommand();
con1.Connection = Con;
con1.CommandType = CommandType.Text;
con1.CommandText = "select * from customer where [DateTime] between dateTimePicker1.Text and dateTimePicker2.Text";
DataSet ds1 = new DataSet();
OleDbDataAdapter adp1 = new OleDbDataAdapter(con1);
adp1.Fill(ds1,"customer");
logDataGridView.DataSource = ds1;
logDataGridView.DataMember = "customer";
Con.Close();
}
else
{
MessageBox.Show("Invalid");
}
}
}
}
ช่วยดูให้หน่อยคะ พยายามมาหลายวันแล้ว จากโค้ดดที่เห็นอาจจะงงๆไปเพราะว่า จับนู่นมาผสมนี่เต็มไปหมดเลย พยายามหามาหลายวิธีแต่ก็ยังทำไม่ได้ซักที TT
ขอบคุณค่ะ
Tag : .NET, C#
|
|
 |
 |
 |
 |
Date :
2011-08-07 12:31:28 |
By :
หัดเขียนc# |
View :
1408 |
Reply :
4 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กำลังงง ว่าทำไมคุณจะต้อง Query 2 รอบครับ
|
 |
 |
 |
 |
Date :
2011-08-07 16:38:04 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กลัวมันจะรันไม่ออกอ่ะค่ะ ก็เลยใส่กันเอาไว้ แล้วตกลงว่ามันต้องสั่งคิวรียังไงหรอคะเพื่อให้แสดงข้อมูลในdatagrid
|
 |
 |
 |
 |
Date :
2011-08-07 16:56:38 |
By :
หัดเขียนC# |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
con1.CommandText = "select * from customer where [DateTime] between '" + dateTimePicker1.Text + "' and '" + dateTimePicker2.Text + "' "
ใส่แบบนี้ครับ
|
 |
 |
 |
 |
Date :
2011-08-08 06:37:51 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ่อ ขอบคุณค่ะ ทำได้แล้วค่ะ พอดีว่าใส่ตารางผิดค่ะ TT
|
 |
 |
 |
 |
Date :
2011-08-08 07:20:32 |
By :
หัดเขียนC# |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|