English 中文(简体)
在数据来源连接点时从 com子中获取价值
原标题:Getting values from combo box when connected by data source in c#
  • 时间:2012-05-08 03:55:37
  •  标签:
  • c#

我在这里是新的。 还有一个大问题,如果你能够帮助,我会真心感激。

研制一种软件,该软件刚输入到查阅档案中,并在一些过滤后取回(其中一台是利用日间收听器、无线电塔顿、 com博箱......etc)。

当我想更新任何条目时,我只是想写出一个特定的问题,并将这些价值观添加到相关领域。 但是,我无法更新我的 com子。 其中2人。 当我尝试遵守法典时,就会犯错误。

我能做些什么? 插手帮助我。 感谢!

cmb_district.ValueMember = dt1.Rows[0][1].ToString();

我利用数据来源的财产将 com子与查阅数据库连接起来。 它显然有显示和价值的成员。 但是,如果查询回去,其价值不会更新 com子,并造成以下错误。

Cannot bind to the new value member. Parameter name: value

感谢。 助我!

最佳回答

你提供的代码行不正确。 Value Members of the combo Box should be set to the ,name of the property or栏, whichyou to be the of their selected Object (unique ID for example). 但是,你正在填满一个领域内容的“密码>。

When you bind the combo box in this way (setting the datasource with combination of DisplayMember and ValueMember), the SelectedValue property of the combo box is filled with the value of ValueMember field of the selected row.

例如,假定你有一栏数据:ActorID,NameBirthDate,其中有一些数据:

DataTable dt = new DataTable();
dt.Columns.Add("ActorID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("BirthDate", typeof(DateTime));


dt.Rows.Add(1, "Will Smith", new DateTime(1968,9,25));
dt.Rows.Add(2, "Bruce Willis", new DateTime(1955,3,19));
dt.Rows.Add(3, "Jim Carrey", new DateTime(1962, 1, 17));
dt.Rows.Add(18, "Nicole Kidman", new DateTime(1967,6,20));

ComboBox cb = new ComboBox();
cb.DropDownStyle = ComboBoxStyle.DropDownList;
cb.Location = new Point(20, 100);
cb.Width = 100;

cb.DisplayMember = "Name";  // *****
cb.ValueMember = "ActorID"; // ***** The important part
cb.DataSource = dt;

Button btn = new Button();
btn.Text = "Show ID";
btn.Location = new Point(10, 140);
btn.Click += (sender, e) =>
    {
        MessageBox.Show(cb.SelectedValue.ToString()); // **** The other important part.
    };

Form f = new Form();
f.Controls.Add(cb);
f.Controls.Add(btn);

f.ShowDialog();

当我们读到 com子的<代码>S selectedValue时,它使我们获得了选定行的“ActorID”。

问题回答

它喜欢这样做。

cmb_district.DataSource = dt1;
cmb_district.ValueMember = "SNO"; 
cmb_district.DisplayMember = "Name";
// where SNO and Name are column names in dt1.

如果伊乌想要在具有约束力的使用之后选择某种特定价值:

cmb_district.SelectedValue = dt1.Rows[0][1].ToString(); 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签