开发者

ComboBox did not Bind

i have the object of class like

MYDepartment myDepartment = new MYDepartment();

then List defined as following

Ilist<MYDepartment> listDepartment=new Ilist<MYDepartment>();

add objects in list one by one

listDepartment.Add(myDepartment);

Now I've defined DataTable and DataRow

DataTable dtDepartments=new DataTable();
DataRow dr = dtDepartments.NewRow();
dr["DepartmentID"] = myDepartment.DepartmentID;
dr["Name"] = myDepartment.DepartmenrName;
dr["Description"] = myDepartment.DepartmentDescription;
dtDepartments.Rows.Add(dr);

DepartmentForm azhaform = new DepartmentForm();
azhaform.combox开发者_如何学Python1.DataSource = dtDepartments;
azhaform.combobox1.ValueMember = "DepartmentID";
azhaform.combobox1.DisplayMember = "Name";

but it did not show ant thing in Combobox.Whats wrong with my code please help


Please make your DisplayMember property to DepartmenrName.

Like this:

   azhaform.combobox1.DisplayMember = "DepartmenrName";

Are you sure it did not show anything? It should have shown just one item, since you have added just one row to your source table.

Edit: Sorry for the blunder I posted above. I am too used to populating with List<> :). I tried your code and it works fine. Trouble must be with something else. Please cancel my vote :)


Though I haven't check it with my editor but it seems a little problem as I have modified it as below...

    DepartmentForm azhaform = new DepartmentForm();
    azhaform.combobox1.DataSource = dtDepartments;
    azhaform.combobox1.ValueMember = "DepartmentID";
    azhaform.combobox1.DisplayMember = "Name";


There seem to be quite a few mistakes in your code, so I'm not sure if I should take it too literally (e.g. "combox1" vs "combobox1"), but the one mistake I can see immediately is that you are setting the datasource on the combobox before you have specified what the ValueMember and DisplayMember are. I haven't tested whether this is going to cause your problem, but it definitely seems wrong.


This may not what you want, but I usualy archive this using BindingSource component, just click on smart tag in ComboBox and check Use Data Bound Items, next click Add Project Data Source and select Object on Data Source form and choose your MYDepartment class.

ComboBox did not Bind

This will automaticaly add BindingSource component on your form (in my VS it named mYDepartmentBindingSource).

Next click again on ComboBox smart tag and do this :

ComboBox did not Bind

Now you can add data :

  mYDepartmentBindingSource.Add(new MYDepartment() { DepartmentID=1, DepartmenrName="Abc" } );
  mYDepartmentBindingSource.Add(new MYDepartment() { DepartmentID = 2, DepartmenrName = "Bca" });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜