我正在創建一個具有兩種形式的應用程式,一種是為了了解我選擇的是哪個檔案,而第二種是為了之前制作過濾器,這意味著您可以選擇要在此檔案中查看哪些屬性。我有一個復選框串列和帶有我的屬性的類。
我的第一個表單中還有一個按鈕,其中有:
foreach (var item in ds)
{
DataGridViewRow row = new DataGridViewRow();
fileListDataGridView.Rows.Add(
item.Path,
item.PatientName,
item.PatientID);
}
我不確定這是如何從 DataGridWiev 的串列中添加資料的正確方法,但現在我有了這個解決方案。因為它只是在串列末尾添加一個新專案。
問題是我在第二種形式中檢查了ListBox,我需要以某種方式將它與我的屬性系結。
特性:
public string Path { get; set; }
public string PatientName { get; set; }
public string PatientID { get; set; }
當您單擊帶有“患者姓名”的復選框時,這意味著您將在第一個表單中獲得僅包含此屬性的資訊。我知道當我們制作一個checkedListBox時,我們在那里也有一個索引,但是我如何獲得這個索引并將它與我的道具系結?
uj5u.com熱心網友回復:
不熟悉 DataGridView 編程的人往往會擺弄 DataGridView 的行和單元格。這很麻煩,很難理解,很難重用,也很難進行單元測驗。
使用資料系結更容易。
顯然你想Patient在你的 DataGridView 中顯示 a 的幾個屬性。
class Patient
{
public int Id {get; set;}
public string Name {get; set;}
public string Path {get; set;}
... // other properties?
}
使用 Visual Studio,您添加了一個 DataGridView 和要顯示的列。在表單的建構式中:
public MyForm()
{
InitializeComponent();
// assign Patient properties to the columns:
this.columnId.DataPropertyName = nameof(Patient.Id);
this.columnName.DataPropertyName = nameof(Patient.Name);
this.columnPath.DataPropertyName = nameof(Patient.Path);
... // etc.
}
在表單中的某處,您有一種方法可以獲取必須顯示的患者:
IEnumerable<Patient> FetchPatientsToDisplay()
{
... // TODO: implement; out-of-scope of this question
}
要顯示患者,我們使用 BindingList:
BindingList<Patient> DisplayedPatients
{
get => (BindingList<Patient>)this.dataGridView1.DataSource;
set => this.dataGridView1.DataSource = value;
}
現在在加載表單時填充 DataGridView:
void OnFormLoading(object sender, ...)
{
this.ShowPatients();
}
void ShowPatients()
{
this.DisplayedPatients = new BindingList<Patient>(this.FetchPatientsToDisplay().ToList());
}
就這些!顯示患者;如果允許,操作員可以添加/洗掉/編輯患者。完成編輯后,他通過按OK或Apply Now按鈕通知程式:
void ButtonOk_Clicked(object sender, ...)
{
// Fetch the edited Patients:
ICollection<Patient> editedPatients = this.DisplayedPatients;
// find out which patients are changed and process them
this.ProcessPatients(editedPatients);
}
So you don't need to Add / Remove rows by yourself. Usually the operator does this. If a default Patient is not enough for you to show, use event BindingList.AddningNew:
void OnAddingNewPatient(object sender, AddingNewEventArgs e)
{
// create a new Patient and give it the initial values that you want
e.NewObject = new Patient()
{
Id = 0, // zero: this Patient has no Id yet, because it is not added to the database yet
Name = String.Empty,
Path = String.Empty,
}
}
Maybe the following properties might be useful:
Patient CurrentPatient => (Patient) this.dataGridView1.CurrentRow?.DataBoundItem;
And if you allow multiple selection:
IEnumerable<Patient> SelectedPatients = this.dataGridView1.SelectedRows
.Cast(row => row.DataGridViewRow)
.Select(row => row.DataBoundItem)
.Cast<Patient>();
In words: interpret every selected rows in the datagridView as a DataGridViewRow. From every DataGridViewRow get the item that is DataBound to it. We know that this is a Patient, so we can cast it to a Patient.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/332904.html
標籤:C# 视窗 winforms 数据网格视图 选中列表框
