我有三個 Grid View
Grid View 1 包含這些列
IdProject ;IdItem ;Length ;Quantity
網格視圖 2 包含這些列
IdInventory ;IdItem ;Length ;QuantityAvailable
網格視圖 3 包含這些列
IdProject ;IdInventory ;IdItem ;Length ;ReservedQuantity ;UnavailableQuantity
操作是這樣進行的:
我將 Grid View1 中的 (IdItem, Length, Quantity) 與 Grid View2 進行比較 如果匹配,我有兩個選擇:
- Quantity <= QuantityAvailable
我向 Grid View3 添加一行(ReservedQuantity= Quantity 和 UnavailableQuantity=0) - Quantity > QuantityAvailable
我有兩個選項:
a- Grid View2:匹配的行不是最后
我向 Grid View3 添加一行(ReservedQuantity= QuantityAvailable and UnavailableQuantity=0)并繼續到下一個匹配的行
b- Grid View2:匹配的/ 不匹配的行是
我向 Grid View3 添加一行的最后一行(ReservedQuantity= QuantityAvailable 和 UnavailableQuantity= Quantity-Sum(ReservedQuantity))
我習慣使用這個代碼
int[] selectedRows = gridView1.GetSelectedRows();
for (int i = 0; i < selectedRows.Length; i )
{
DataRow rowGridView1 = (gridView1.GetRow(selectedRows[i]) as DataRowView).Row;
for (int j = 0; j < gridView2.RowCount; j )
{
//check for comparison
//add rows
}
}
uj5u.com熱心網友回復:
不熟悉 DataGridViews 的人傾向于直接擺弄 DataGridView 的單元格,盡管使用 DataSource 更容易。
您寫道: 操作是這樣進行的:我將 Grid View1 中的 (IdItem, Length, Quantity) 與 Grid View2 進行比較。
唉,Grid View1 沒有 (IdItem, Length, Quantity)。但是,此 DataGridView 中的每一行都有。我假設您打算比較 Dgv 中的行
我想您的意思是,您將 Dgv1 的第 0 行與 Dgv2 的第 0 行進行比較,并根據您從 Dgv3 創建的第 0 行的值。同樣,您對所有行都執行此操作。
顯然 Dgv1 顯示了一系列相似的專案。我不知道它們是什么,所以假設它們是 A 類的專案。這個類類似于以下內容:
class A
{
public int IdProject {get; set;}
public int IdItem {get; set;}
public decimal Length {get; set;}
public int Quantity {get; set;}
}
同樣,Dgv2 顯示 B 類專案:
class B
{
public int IdInventory {get; set;}
public int IdItem {get; set;}
public decimal Length {get; set;}
public int QuantityAvailable {get; set;}
}
Dgv3 顯示 C 類:
class C
{
public int IdProject {get; set;}
public int IdInventory {get; set;}
public int IdItem {get; set;}
public decimal Length {get; set;}
public int ReservedQuantity {get; set;}
public int UnavailableQuantity {get; set;}
}
可能您的類有幾個其他屬性,或其他識別符號或型別,但您已了解要點。
使用 Visual Studio 設計器,您已經添加了三個 DataGridViews 和列。在建構式中,您可以提及哪個列應該顯示哪個屬性。
為此,我們使用屬性DataGridViewColumn.DataPropertyName
// constructor
public MyForm()
{
InitializeComponent();
// Dgv1 shows properties of class A
dgv1ColumnIdProject.DataPropertyName = nameof(A.IdProject);
dgv1ColumnIdItem.DataPropertyName = nameof(A.IdItem);
dgv1ColumnLength.DataPropertyName = nameof(A.Length);
dgv1ColumnQuantity.DataPropertyName = nameof(B.Length);
// Dgv2 shows properties of class B
dgv2ColumnIdInventory.DataPropertyName = nameof(B.IdInventory);
... // etc also Dgv3 and class C
}
在某處,您有一些方法可以獲取應顯示在 Dgv1 和 Dgv2 中的資料:
(As 是 A 的復數;Bs 是 B 的復數,以此類推)
private IEnumerable<A> FetchAsToDisplay() {...}
private IEnumerable<B> FetchBsToDisplay() {...}
要填充 DataGridViews 并獲取 DataGridVies 中的專案,請創建以下屬性:
private BindingList<A> DisplayedAs
{
get => (BindingList<A>)this.Dgv1.DataSource;
set => this.Dgv1.DataSource = value;
}
private BindingList<B> DisplayedBs
{
get => (BindingList<B>)this.Dgv2.DataSource;
set => this.Dgv2.DataSource = value;
}
private BindingList<C> DisplayedCs
{
get => (BindingList<C>)this.Dgv3.DataSource;
set => this.Dgv3.DataSource = value;
}
現在用它們的初始值填充 Dgv1 和 Dgv2,執行以下操作:
IEnumerable<A> asToDisplay = this.FetchAsToDisplay();
this.DisplayedAs = new BindingList<A>(asToDisplay.ToList());
IEnumerable<B> bsToDisplay = this.FetchBsToDisplay();
this.DisplayedBs = new BindingList<B>(bsToDisplay.ToList());
要計算 Dgv3 中應該包含的內容,您可以重用 asToDisplay 和 bsToDisplay,或者如果您稍后必須這樣做,例如在單擊“確定”按鈕后:
private void OnButtonOk_Clicked(object sender, ...)
{
ICollection<A> displayedAs = this.DisplayedAs;
ICollection<B> displayedBs = this.DisplayedBs;
IEnumerable<C> csToDisplay = this.CreateCsToDisplay(displayedAs, displayedBs);
this.DisplayedCs = new BindingList(csToDisplay.ToList();
}
CreateCsToDisplay(TODO:發明一個更好的名稱)是根據您定義的兩個選項決定應該顯示什么的程序。
private IEnumerable<C> CreateCsToDisplay(IEnumerable<A> as, IEnumerable<B> bs)
{
// assume Dgv1 and Dgv2 has the same number of rows,
// so sequence as has the same number of elements as sequence bs
// use ZIP to combine A and B. For every [a,b] combination create a C:
return as.Zip(bs, (a, b) => this.CreateC(a, b);
}
private C CreateC(A a, B b)
{
// use the conditions you defined
if (a.Quantity < b.QuantityAvailable)
{
return new C
{
ReservedQuantity = a.Quantity,
UnavailableQuantity = 0,
};
}
else
{
return new C ... etc, see your specification
}
}
概括
通過使用 DataSource 和 BindingList,您不必再擺弄 Rows 和 Cells,您可以將 Rows 解釋為類似專案的序列。使用諸如DisplayedAs. 獲取和設定顯示的專案現在是小菜一碟:只需使用屬性。
為了計算如何從一個 [A, B] 組合創建一個 C,我們創建了一個方法 CreateC。此方法隱藏您的規范。
類似地,我們有一種方法可以從 As 和 Bs 序列創建一系列 Cs:CreateCsToDisplay。此方法還隱藏了 C 的創建方式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/379708.html
上一篇:Datagridview行高問題
下一篇:我找不到任何使用WindowsFormsControls和(或)Infragistics構建的.NET4.0應用程式
