Здравстуйте.
Я тут, как я понимаю, ошибочку нашел в работе Combobox-а, думаю может кому и понадобится, поэтому и пишу, сам вроде разобрался в причине, хотя почему так происходить объяснить не могу.
ситуация такая, если у Combobox-а задать sorted = true, то он начинает глючить, т.е. он сортирует свой список по алфавиту, а коды строк остаются такими же, и соответственно неправильно показывает текст соответствующего кода. к примеру если есть таблица
1 - БББ
2 - ААА
то при установке сортировки комбо отсортирует так:
1 - ААА
2 - БББ
т.е. он сменит коды!!!
Для иллюстрации сказанного привожу пример:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
namespace ComboBag
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmTest());
}
}
public class frmTest : Form
{
private DataSet _ds = new DataSet();
private DataGridView _grid = new DataGridView();
private BindingSource _personBS = new BindingSource();
private BindingSource _cityBS = new BindingSource();
private TextBox _idTxt = new TextBox();
private ComboBox _cityCmb = new ComboBox();
private TextBox _nameTxt = new TextBox();
public frmTest()
{
this.InitializeComponents();
this.InitData();
this.BindControls();
}
private void BindControls()
{
this._cityBS.DataSource = this._ds.Tables["City"];
this._personBS.DataSource = this._ds.Tables["Person"];
this._grid.DataSource = this._personBS;
this._idTxt.DataBindings.Add(new Binding("Text", this._personBS, "id", true));
this._nameTxt.DataBindings.Add(new Binding("Text", this._personBS, "name", true));
// если включить сортировку, то combobox начинает работать некорректно!!
// this._cityCmb.Sorted = true;
this._cityCmb.DataSource = this._cityBS;
this._cityCmb.DisplayMember = "name";
this._cityCmb.ValueMember = "id";
this._cityCmb.DataBindings.Add(new Binding("SelectedValue", this._personBS, "city_id", true));
}
private void InitializeComponents()
{
// grid
this._grid.Size = new Size(400, 200);
this._grid.Location = new Point(10, 10);
// idTxt
this._idTxt.Location = new Point(10, 220);
// cityCmb
this._cityCmb.Location = new Point(10, 260);
// nameTxt
this._nameTxt.Location = new Point(10, 300);
this.Controls.Add(this._idTxt);
this.Controls.Add(this._cityCmb);
this.Controls.Add(this._nameTxt);
this.Controls.Add(this._grid);
this.Size = new Size(440, 360);
}
private void InitData()
{
DataTable tCity = new DataTable();
tCity.TableName = "City";
DataColumn col;
col = new DataColumn("id", typeof(string));
tCity.Columns.Add(col);
col = new DataColumn("name", typeof(string));
tCity.Columns.Add(col);
tCity.PrimaryKey = new DataColumn[] { tCity.Columns[0] };
this._ds.Tables.Add(tCity);
DataTable tPerson = new DataTable();
tPerson.TableName = "Person";
col = new DataColumn("id", typeof(string));
tPerson.Columns.Add(col);
col = new DataColumn("city_id", typeof(string));
tPerson.Columns.Add(col);
col = new DataColumn("name", typeof(string));
tPerson.Columns.Add(col);
tPerson.PrimaryKey = new DataColumn[] { tPerson.Columns[0] };
this._ds.Tables.Add(tPerson);
DataRelation rel = new DataRelation("FK_City_Person", tCity.Columns[0], tPerson.Columns[1]);
this._ds.Relations.Add(rel);
DataRow row;
row = tCity.NewRow();
row["id"] = "1";
row["name"] = "Yerevan";
tCity.Rows.Add(row);
row = tCity.NewRow();
row["id"] = "2";
row["name"] = "Moscow";
tCity.Rows.Add(row);
row = tCity.NewRow();
row["id"] = "3";
row["name"] = "Volgograd";
tCity.Rows.Add(row);
row = tPerson.NewRow();
row["id"] = "1";
row["city_id"] = "1";
row["name"] = "Ashot";
tPerson.Rows.Add(row);
row = tPerson.NewRow();
row["id"] = "2";
row["city_id"] = "3";
row["name"] = "Vasya";
tPerson.Rows.Add(row);
row = tPerson.NewRow();
row["id"] = "3";
row["city_id"] = "3";
row["name"] = "Petya";
tPerson.Rows.Add(row);
}
}
}
Всего лишь включите закоментированную строку и комбо начнет глючить....