2017-01-30 47 views
1

我試圖顯示當我選擇該日期時的具體日期記錄。我已經填補了我的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(); 

     } 
    } 
} 
+0

在過濾代碼中加入這一行,你可以分享你如何將數據綁定到網格? –

+0

這是我使用的總代碼。我可以分享表單的完整代碼。 – stackuser

回答

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); 

希望它有幫助。 如果你想要別的東西,寫下來。

+0

它仍然顯示完整的表 – stackuser

+0

執行此命令後,您是否設置了DataGridView的DataSource?如果沒有,然後設置它。它會設置新的數據到gridview。 –

+0

i jusr替換了'adapter.SelectCommand = new SqlCommand(「SELECT * FROM ItemRecord」,con);'轉換爲'adapter.SelectCommand = new SqlCommand(「SELECT * FROM ItemRecord Where date> ='」+ dateTimePicker1.Value。 addDays(1).ToString(「yyyy-MM-dd」)+「'AND date <'」+ date +「'」,con);' – stackuser

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 + "#"); 
    } 
    } 
} 
0

Button1_Clickadapter.Fill(dataset);

adapter.Fill(dataset, "ItemRecord"); 
dataGridView1.DataSource = dataset.Tables["ItemRecord"]; 
+0

它說「無法找到表0」。 – stackuser

+0

現在,它不顯示任何數據的dataGridView – stackuser

+0

寫表名,而不是指數 –