【发布时间】:2017-03-06 12:28:59
【问题描述】:
我正在尝试使用存储过程中的名称填充组合框。
public frmAddEdit(Book book, Author author)
{
_book = book;
_author = author;
_dataAccess = DataAccess.GetInstance;
_dataAccess.SetConnectionString(_dataAccess.GetServerConnectionString());
ComboFill();
InitializeComponent();
}
这是我的加载表单
public void AddEditBook_Load(object sender, EventArgs e)
{
txtTitle.Text = _book.Title;
//comboBox1.DataSource = _book.FullName;
//comboBox1.ValueMember = "AuthorId";
//comboBox1.DisplayMember = "FullName";
ComboFill();
//txtAuthor.Text = _author.FullName;
//txtAdd.Text = book.Title;
}
我的组合框
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView view = comboBox1.SelectedItem as DataRowView;
string fullName = view["FullName"].ToString();
}
我在网上看到并尝试过。
public void ComboFill()
{
DataRow dr;
DataTable dt = new DataTable();
dr = dt.NewRow();
// dr.ItemArray = new object[] { 0, "Select Author" };
dt.Rows.InsertAt(dr, 0);
comboBox1.DataSource = _book.FullName;
comboBox1.ValueMember = "AuthorId";
comboBox1.DisplayMember = "FullName";
}
【问题讨论】:
-
既然可以用简单的方式做复杂的事情,为什么还要做复杂的事情?
-
键入
System.Windows.Forms.ComboBox作为没有名为ValueMember或DisplayMember的属性。 -
那么,您的问题是什么?你有什么错误吗?有什么问题?
-
此代码可以进行大量修改以进行改进,但获得此(可能)功能的最简单方法是在您的构造函数中调用
InitializeComponent()之后移动ComboFill()方法。InitializeComponent方法是 ComboBox 被实例化的地方。我很惊讶您的应用程序甚至可以运行。当您第一次执行ComboFill时,ComboBox 甚至还不存在。 -
@Pikoh 我正在尝试将保存在我的 sql 中的全名填充到我的组合框中