Home > Enterprise >  How to bind a checkboxlist with properties in my class? C# (Windows Forms)
How to bind a checkboxlist with properties in my class? C# (Windows Forms)

Time:10-21

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?

CodePudding user response:

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 Patient in your DataGridView.

class Patient
{
    public int Id {get; set;}
    public string Name {get; set;}
    public string Path {get; set;}
    ... // other properties?
}

Using visual studio you have added a DataGridView and the columns that you want to show. In the constructor of your form:

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.
}

Somewhere in your form you have a method to fetch the Patients that must be displayed:

IEnumerable<Patient> FetchPatientsToDisplay()
{
    ... // TODO: implement; out-of-scope of this question
}

To display Patients we use a BindingList:

BindingList<Patient> DisplayedPatients
{
    get => (BindingList<Patient>)this.dataGridView1.DataSource;
    set => this.dataGridView1.DataSource = value;
}

Now to fill the DataGridView when the form is loaded:

void OnFormLoading(object sender, ...)
{
    this.ShowPatients();
}

void ShowPatients()
{
    this.DisplayedPatients = new BindingList<Patient>(this.FetchPatientsToDisplay().ToList());
}

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 OK or Apply Now button:

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.

  • Related