[英]How to filter DataGridView in my application
我有兩個使用winforms DataGridView向導設置的網格。 一個綁定到行程表(我的交易表),另一個綁定到我的費用表。 我知道一些linq2sql,現在我的費用表插入了fk(TripId)。
我想做的是基於tripId過濾費用網格。 我已經在檢索當前所選行程的TripId PK,以便完成該部分。 考慮到我使用的是linq,但是我使用內置的向導來綁定表,我不確定該如何進行過濾。
任何幫助,將不勝感激!
編輯:我已經在以下bluefeet的幫助下走了很遠。 現在的問題是,當我執行過濾器時,它只是清除網格而不是基於pk進行過濾。 這是完整的代碼
private void tripsBindingSource_PositionChanged(object sender, EventArgs e)
{
if (dgvTripGrid.CurrentRow != null)
{
//get selected row index
int index = this.dgvTripGrid.CurrentRow.Index;
//get pk of selected row using index
string cellValue = dgvTripGrid["pkTrips", index].Value.ToString();
//change pk string to int
int pKey = Int32.Parse(cellValue);
//int tripPrimKey = getPkRowTrips();
this.tripExpenseBindingSource.Filter = String.Format("tripNo = {0}",
pKey.ToString());
}
}
聽起來像是你要填寫你的第二個datagridview
根據您的第一個選擇datagridview
。 這種方法可以做到這一點:
datagridview
,使用事件DataBindingComplete
然后填充第二datagridview
基礎上在第一個選定的記錄的ID datagridview
。 BindingSource_PositionChanged
上的事件重新填充第二個網格。 代碼樣例
// this populates the grid.
private void SearchButton_Click(object sender, EventArgs e)
{
// your code to load your grid goes here
}
private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
var drv = datagridview1bindingSource.Current as DataRowView;
if(drv != null)
// your method to load datagridview2 goes here if the selected row is not null
LoadDataGridView2();
}
private void LoadDataGridView2()
{
//populate datagridview2 using the selected row id from datagridview1
}
// finally when the position is changed on the datagridview1 binding source, then re-populate // the datagridview2
private void datagridview2BindingSource_PositionChanged(object sender, EventArgs e)
{
LoadDataGridView2();
}
這是根據第一個網格中的選擇填充第二個網格的基本方法。
編輯:
您的評論說,您將用所有費用填充datagridview
,因此要進行過濾,您將需要使用BindingSource
上的datagridview
的Filter
屬性。 Filter
屬性使您可以查看數據源的子集。
來自MSDN的示例:
private void PopulateDataViewAndFilter()
{
DataSet set1 = new DataSet();
// Some xml data to populate the DataSet with.
string musicXml =
"<?xml version='1.0' encoding='UTF-8'?>" +
"<music>" +
"<recording><artist>Coldplay</artist><cd>X&Y</cd></recording>" +
"<recording><artist>Dave Matthews</artist><cd>Under the Table and Dreaming</cd></recording>" +
"<recording><artist>Dave Matthews</artist><cd>Live at Red Rocks</cd></recording>" +
"<recording><artist>Natalie Merchant</artist><cd>Tigerlily</cd></recording>" +
"<recording><artist>U2</artist><cd>How to Dismantle an Atomic Bomb</cd></recording>" +
"</music>";
// Read the xml.
StringReader reader = new StringReader(musicXml);
set1.ReadXml(reader);
// Get a DataView of the table contained in the dataset.
DataTableCollection tables = set1.Tables;
DataView view1 = new DataView(tables[0]);
// Create a DataGridView control and add it to the form.
DataGridView datagridview1 = new DataGridView();
datagridview1.AutoGenerateColumns = true;
this.Controls.Add(datagridview1);
// Create a BindingSource and set its DataSource property to
// the DataView.
BindingSource source1 = new BindingSource();
source1.DataSource = view1;
// Set the data source for the DataGridView.
datagridview1.DataSource = source1;
//The Filter string can include Boolean expressions.
source1.Filter = "artist = 'Dave Matthews' OR cd = 'Tigerlily'";
}
我使用這種類型的過濾器來顯示基於帳戶的數據。 對於帳戶,當用戶放置帳號時會有一個文本框,並且我使用TextChanged事件來應用過濾器。 然后,我有一個按鈕,用於從綁定源中刪除篩選器。
您可以使用第一個datagridview中的三元組將相同的內容應用於費用datagridview。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.