Avoid changing DataSource member before ValueMember/DisplayMember | CAST Appmarq

Avoid changing DataSource member before ValueMember/DisplayMember


Rule Definition
When populating data bound controls such as ComboBox or ListBox, it is more efficient to set the DataSource property last, after ValueMember and DisplayMember are set. Otherwise your control will be repopulated as a result of the ValueMember change. BindingSource.SuspendBinding and BindingSource.ResumeBinding are two methods that allow the temporary suspension and resumption of data binding. SuspendBinding prevents changes from being pushed into the data source until ResumeBinding is called. These methods are designed to be used with simple bound scenarios such as TextBox or ComboBox data binding. However, Controls that implement complex data binding, such as the DataGridView control, update their values based on change events such as ListChanged, so calling SuspendBinding will not prevent them from receiving changes to the data source. You can use these methods in complex binding scenarios if you suppress ListChanged events by setting the RaiseListChangedEvents property to false.

Remediation
Set the DataSource member after the ValueMember and DisplayMember.

Violation Code Sample
comboBox1.DataSource = test;
comboBox1.ValueMember = "Name";
comboBox1.DisplayMember = "Name";
Fixed Code Sample
comboBox1.ValueMember = "Name";
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = test;

Reference
MSDN

Related Technologies

Health Factor

  Efficiency


Technical Criterion
Efficiency - SQL and Data Handling Performance

About CAST Appmarq

CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.

Benchmark Statistics

Global Compliance

39.71%

Total Violations
1,333
Total Opportunities
2,211
Average Violations / App.
18.01
The compliance score represents 1 minus the ratio between the number of times a rule has been violated compared to the number of opportunities in a set of applications that the rule could have been violated.

Industry Insights

Select from drop-down

24.62%

Insurance

33.11%

Financial Services

23.62%