当我选择该日期时,我试图显示有关日期的记录。我已经填充了我的datagridview:
con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Users\\Fakhar Zaman\\documents\\visual studio2010\\Projects\\LabManagement\\LabManagement\\Management.mdf\";Integrated Security=True;User Instance=True");
con.Open();
SqlDataAdapter adap = new SqlDataAdapter("select id, expenseTitle, expenseAmount, date, paidBy, expenseType from Expense", con);
DataSet ds = new System.Data.DataSet();
adap.Fill(ds, "Expense");
dataGridView2.DataSource = ds.Tables[0];
adap = new SqlDataAdapter("select id, date, itemName, quantity, itemSold, priceBuy, soldPrice, totalSold, profit from ItemRecord", con);
ds = new System.Data.DataSet();
adap.Fill(ds, "ItemRecord");
dataGridView1.DataSource = ds.Tables[0];
此代码工作正常并显示datagridview中的值。但是,当我想过滤特定日期的内容时,它不会这样做,而是再次显示完整的记录。 过滤代码如下:
string plus = dateTimePicker1.Value.AddDays(1).ToString("yyyy-MM-dd");
string date = dateTimePicker1.Value.ToString("yyyy-MM-dd");
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("SELECT * FROM ItemRecord", con);
var dataset = new DataSet();
adapter.Fill(dataset);
//MessageBox.Show(plus +" , " + date);
this.itemRecordBindingSource.Filter = string.Format("date >= '{0:yyyy-MM-dd}' AND date < '{1:yyyy-MM-dd}'", date, plus);
//string date = dateTimePicker1.Value.ToString();
请建议我解决一下。
每当我使用自动生成的代码填充数据集时,它工作正常并过滤特定内容,但我需要以上代码 工作正常
this.expenseTableAdapter.Fill(this.managementDataSet21.Expense); this.itemRecordTableAdapter.Fill(this.managementDataSet21.ItemRecord);*/
以下是表单的完整代码:
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.SqlClient;
namespace LabManagement
{
public partial class calculation : Form
{
SqlConnection con;
public calculation()
{
InitializeComponent();
}
private void calculation_Load(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Users\\Fakhar Zaman\\documents\\visual studio 2010\\Projects\\LabManagement\\LabManagement\\Management.mdf\";Integrated Security=True;User Instance=True");
con.Open();
SqlDataAdapter adap = new SqlDataAdapter("select id, expenseTitle, expenseAmount, date, paidBy, expenseType from Expense", con);
DataSet ds = new System.Data.DataSet();
adap.Fill(ds, "Expense");
dataGridView2.DataSource = ds.Tables[0];
adap = new SqlDataAdapter("select id, date, itemName, quantity, itemSold, priceBuy, soldPrice, totalSold, profit from ItemRecord", con);
ds = new System.Data.DataSet();
adap.Fill(ds, "ItemRecord");
dataGridView1.DataSource = ds.Tables[0];
/*
// TODO: This line of code loads data into the 'managementDataSet21.Expense' table. You can move, or remove it, as needed.
this.expenseTableAdapter.Fill(this.managementDataSet21.Expense);
// TODO: This line of code loads data into the 'managementDataSet21.ItemRecord' table. You can move, or remove it, as needed.
this.itemRecordTableAdapter.Fill(this.managementDataSet21.ItemRecord);*/
dateTimePicker1.MaxDate = DateTime.Today;
}
private void button1_Click(object sender, EventArgs e)
{
// this.itemRecordBindingSource.Filter = "date = '" + dateTimePicker1.Value.ToString() + "'";
string plus = dateTimePicker1.Value.AddDays(1).ToString("yyyy-MM-dd");
string date = dateTimePicker1.Value.ToString("yyyy-MM-dd");
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand("SELECT * FROM ItemRecord Where date >='" + dateTimePicker1.Value.AddDays(1).ToString("yyyy-MM-dd") + "' AND date < '" + date + "'", con);
//adapter.SelectCommand = new SqlCommand("SELECT * FROM ItemRecord", con);
adapter.SelectCommand.BeginExecuteNonQuery();
var dataset = new DataSet();
adapter.Fill(dataset);
//MessageBox.Show(plus +" , " + date);
//this.itemRecordBindingSource.Filter = string.Format("date >= '{0:yyyy-MM-dd}' AND date < '{1:yyyy-MM-dd}'", date, plus);
/*string date = dateTimePicker1.Value.ToString();
*/
int sum = 0;
for (int i = 0; i < dataGridView1.Rows.Count; ++i)
{
sum += int.Parse(dataGridView1.Rows[i].Cells[8].Value.ToString());
}
label2.Text = sum.ToString();
///// for expense table:
SqlDataAdapter myadapter = new SqlDataAdapter();
myadapter.SelectCommand = new SqlCommand("SELECT * FROM Expense Where date >='" + dateTimePicker1.Value.AddDays(1).ToString("yyyy-MM-dd") + "' AND date < '" + date + "'", con);
//myadapter.SelectCommand = new SqlCommand("SELECT * FROM Expense", con);
var expensedataset = new DataSet();
myadapter.Fill(expensedataset);
//MessageBox.Show(plus +" , " + date);
//this.expenseBindingSource.Filter = string.Format("date >= '{0:yyyy-MM-dd}' AND date < '{1:yyyy-MM-dd}'", date, plus);
/*string date = dateTimePicker1.Value.ToString();
*/
sum = 0;
for (int i = 0; i < dataGridView2.Rows.Count; ++i)
{
sum += int.Parse(dataGridView2.Rows[i].Cells[2].Value.ToString());
}
label3.Text = sum.ToString();
int overall = int.Parse(label2.Text) - int.Parse(label3.Text);
label5.Text = "Overall profit for date " + dateTimePicker1.Value.ToString("dd-MM-yyyy") +" = Rs." + overall.ToString() ;
/*string namequery = "SELECT itemName from ItemRecord where date = '" + date +"'";
string itemname;
using (var command = new SqlCommand(namequery, con))
{
command.Parameters.AddWithValue("@Parameter", 5);
con.Open();
itemname = (string)command.ExecuteScalar();
con.Close();
}
*/
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form1 frm = new Form1();
frm.ShowDialog();
this.Close();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
}
答案 0 :(得分:0)
当您再次呼叫DB时,为什么不选择那些满足您日期检查的记录。看一下修改后的select命令。
SqlCommand sqlcmd = new SqlCommand("SELECT * FROM ItemRecord Where date >='"+dateTimePicker1.Value.AddDays(1).ToString("yyyy-MM-dd")+"' AND date < '"+date+"'" , con);
SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd);
adapter.Fill(yourDataSource);
希望它有所帮助。 如果你想要别的东西,请记下来。
答案 1 :(得分:0)
我不确定是否有必要在代码显示时过滤数据。您可以简单地将行过滤器应用于当前DataTable
,而不是从原始数据库中过滤/选择。假设您要过滤数据以显示两个日期之间的所有日期。使用两个文本框,用户在一个文本框中键入第一个开始日期,然后在第二个文本框中键入结束的第二个日期。用户点击“过滤器”按钮过滤DataTable
,仅显示这两个日期之间的日期。如果用户清除文本框并单击过滤器按钮,则将显示原始未过滤的数据。希望这会有所帮助。
DataTable dt;
public Form1() {
InitializeComponent();
dt = GetYourDataTable();
dataGridView1.DataSource = dt;
}
private void buttonFilter_Click(object sender, EventArgs e) {
if (tbDate1.Text == "" || tbDate2.Text == "") {
dt.DefaultView.RowFilter = "";
}
else {
DateTime sDate;
DateTime eDate;
if ((DateTime.TryParse(tbDate1.Text, out sDate)) && (DateTime.TryParse(tbDate2.Text, out eDate))) {
dt.DefaultView.RowFilter = string.Format("Date >= #" + sDate + "# AND Date <= #" + eDate + "#");
}
}
}
答案 2 :(得分:0)
在Button1_Click
adapter.Fill(dataset);
行
adapter.Fill(dataset, "ItemRecord");
dataGridView1.DataSource = dataset.Tables["ItemRecord"];