Reasons for System.Data.DataRowView in ComboBox and timing of designation of ValueMember

Source: Internet
Author: User

When using the ComboBox control, there are two reasons why the SelectedValue value is "System.Data.DataRowView": in the case that the datasource of the ComboBox is not empty , Either there is no assignment for ValueMember, or the assignment is wrong, in both cases the system will output the default value of SelectedValue (note the red part, if the DataSource is empty, then the value of SelectedValue is null). But sometimes even if you assign the correct value to ValueMember, there is a problem with the assignment timing, see the following example:

First, you construct a form that looks like this:

The form has a drop-down box named Combobox1, and a text box named TextBox1, which is very simple, and when you select an item in the drop-down box, the value of the valuemember corresponding to the selection is displayed in the text box. Here is the core code to implement the feature:

Code-1:

[C-sharp]View Plaincopy
  1. Namespace Frmfortest
  2. {
  3. public partial class Form1:form
  4. {
  5. Public Form1 ()
  6. {
  7. InitializeComponent ();
  8. This. Initialcombobox ();
  9. }
  10. private void Initialcombobox ()
  11. {
  12. DataTable table = new DataTable ();
  13. DataColumn column;
  14. DataRow Row;
  15. column = new DataColumn ("Name");
  16. Table. Columns.Add (column);
  17. column = new DataColumn ("Value");
  18. Table. Columns.Add (column);
  19. for (int i = 0; i < 5; i++)
  20. {
  21. row = table. NewRow ();
  22. row["Name"] = "Test" + i;
  23. row["Value"] = i.ToString ();
  24. Table. Rows.Add (row);
  25. }
  26. This.comboBox1.DataSource = table;
  27. This.comboBox1.DisplayMember = "Name";
  28. This.comboBox1.ValueMember = "Value";
  29. }
  30. private void ComboBox1_SelectedIndexChanged (object sender, EventArgs e)
  31. {
  32. This.textBox1.Text = This.comboBox1.SelectedValue.ToString ();
  33. }
  34. }
  35. }

Run the above code, and the output is as follows when the form is initialized:

The text box shows "System.Data.DataRowView", which was previously said to be the string only if no value for ValueMember was specified for the ComboBox or if the error was specified. The program has correctly specified the ValueMember why is it still the result? I try to select the other drop-down items and find out what the results of the display have turned into the expected values, why?

Careful observation of the program found that the reason lies in the timing of the ValueMember assignment, the above code when the completion of the DataSource assignment statement, triggered the SelectedIndexChanged event, and at this time did not specify ValueMember, So the initial initialization of the form shows "System.Data.DataRowView", and the workaround is simple: simply place the DataSource assignment statement after the DisplayMember, ValueMember statement.

OK, this problem is solved, the new question comes again, please look at the following description:

Suddenly think of the ComboBox there is an event is selectedvaluechanged, or the above program Code-1, the SelectedIndexChanged to replace the selectedvaluechanged, what will be the effect? Try it out without an error, and the expected value of 0 is displayed when the form is initialized. Oh, and found a way to solve. The problem is not over yet, please look down:

Above the selectedvaluechanged experiment, DataSource is in front of DisplayMember, ValueMember, if put to their two behind it? Continue to try, this time the error-"Do not reference object to the example", the program is tracked, when the execution of this.comboBox1.ValueMember = "Value" statement, the program goes to Combobox1_ SelectedIndexChanged method, This.textBox1.Text = this.comboBox1.SelectedValue.ToString (), statement error, SelectedValue value is empty, Because at this time has not given the DataSource assignment, therefore reported this mistake.

It is now possible to summarize that when using SelectedIndexChanged, ValueMember is assigned before DataSource, and when SelectedValueChanged is used, The ValueMember is assigned after datasource.

There is one more interesting question, as follows:

This.comboBox1.DisplayMember = "Name";

This.comboBox1.ValueMember = "ValueError";

This.comboBox1.DataSource = table;

The above deliberately put the value of ValueMember wrong, run without any problem (refers to no error, at this time the SelectedValue value is System.Data.DataRowView), and then we do the order of the three lines of code to adjust, as follows:

This.comboBox1.DataSource = table;

This.comboBox1.DisplayMember = "Name";

This.comboBox1.ValueMember = "ValueError";

The value of the above valuemember is still wrong, but after assigning the operation to the DataSource statement, run the program, at the ValueMember assignment statement, the error is as follows: "Cannot bind to value member".

When the value of datasource is specified and then the ValueMember is assigned a wrong, if the assignment is wrong, the runtime will error, if the DataSource statement before the ValueMember assignment will not error, we must pay attention to when the application.

Reasons for System.Data.DataRowView in ComboBox and timing of designation of ValueMember

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.