我試圖顯示當我選擇該日期時的具體日期記錄。我已經填補了我的DataGridView這樣的:錯誤C#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();
}
}
}
在過濾代碼中加入這一行,你可以分享你如何將數據綁定到網格? –
這是我使用的總代碼。我可以分享表單的完整代碼。 – stackuser