I'm creating an app with two forms, one is for understanding which file I am choosing, and the second one is just to make a filter before, which means you can choose which properties you want to see in this file. I have a checkboxlist and class with my properties.
I also have a button in my first form where I have:
foreach (var item in ds)
{
DataGridViewRow row = new DataGridViewRow();
fileListDataGridView.Rows.Add(
item.Path,
item.PatientName,
item.PatientID);
}
I'm not sure that this is the correct way how to add data from the list in DataGridWiev, but right now I have this solution. Cause it's just adding a new item at the end of the list.
The problem is that I have checkedListBox in the second form and I need to bind it somehow with my properties.
Properties:
public string Path { get; set; }
public string PatientName { get; set; }
public string PatientID { get; set; }
When you click on checkbox with Patient Name that means you will get the information in your first form with only this property. I know that when we are making a checkedListBox, we also have an index there, but how I can get this index and bind it with my prop?
People who are new to programming a DataGridView tend to fiddle with the rows and cells of the DataGridView. This is cumbersome, hard to understand, hard to reuse and very difficult to unit test.
Using databinding is way more easy.
Apparently you want to show several properties of a
Patientin your DataGridView.Using visual studio you have added a DataGridView and the columns that you want to show. In the constructor of your form:
Somewhere in your form you have a method to fetch the Patients that must be displayed:
To display Patients we use a BindingList:
Now to fill the DataGridView when the form is loaded:
That is all! The Patients are displayed; if allowed, the operator can add / remove / edit Patients. When finished editing, he notifies the program by pressing the
OKorApply Nowbutton: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:
Maybe the following properties might be useful:
And if you allow multiple selection:
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.