【问题标题】:fill combobox with data in a stored procedure用存储过程中的数据填充组合框
【发布时间】: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 作为没有名为 ValueMemberDisplayMember 的属性。
  • 那么,您的问题是什么?你有什么错误吗?有什么问题?
  • 此代码可以进行大量修改以进行改进,但获得此(可能)功能的最简单方法是在您的构造函数中调用InitializeComponent() 之后移动ComboFill() 方法。 InitializeComponent 方法是 ComboBox 被实例化的地方。我很惊讶您的应用程序甚至可以运行。当您第一次执行ComboFill 时,ComboBox 甚至还不存在。
  • @Pikoh 我正在尝试将保存在我的 sql 中的全名填充到我的组合框中

标签: c# sql winforms


【解决方案1】:
//assume this that you have datatable filled from stored procedure
//This is sample for populating your combo
DataTable dt = new DataTable();
dt.Columns.Add("AuthorID", typeof(int));
dt.Columns.Add("FullName", typeof(string));

DataRow dr = null;
for (int i = 0; i < 10; i++)
{
   dr = dt.NewRow();
   dr[0] = i;
   dr[1] = "ABC" + i + 2 * i;

   dt.Rows.Add(dr);
}

comboBox1.DataSource = dt; // this will be populated from SP
comboBox1.ValueMember = "AuthorID";
comboBox1.DisplayMember = "FullName";

【讨论】:

  • DataRow dr = null; for (int i = 0; i
  • 我希望它将数据中的四个作者姓名带入组合框
【解决方案2】:

comboBox1.DataSource = _bool.FullName; 是错误的。应该是:

comboBox1.DataSource = _book;

【讨论】:

  • 我想知道只用一个项目填充组合框有什么意义,如果可能有多个元素,您应该查看 J.SMTBCJ15 的答案stackoverflow.com/posts/42625890/edit
  • 是的,现在 mu 组合框中有数据,但我希望保存在我的数据库中的全名显示在组合框中
  • 在 SO,人们只能帮助而不是整个代码 @SMG。你很幸运,这个问题没有被否决。如果您仍然无法从上面的答案中理解,请查看此链接c-sharpcorner.com/UploadFile/009464/…
【解决方案3】:

我遇到了同样的问题,我使用 SqlDataAdapter 修复了它并将其传递给我的存储过程

public void PopulateDropdown()    
{   
    // Check to see if connection is closed, to be safe, if closed then open
    if (connection_db.State == ConnectionState.Closed)
        connection_db.Open();

    SqlDataAdapter sqlData = new SqlDataAdapter("StoredProcedureHere", connection_db);

    // Must specify 'SelectCommand' when using get queries
    sqlData.SelectCommand.CommandType = CommandType.StoredProcedure;
    DataTable table = new DataTable();

    // Store data in table
    sqlData.Fill(table);


    dropdownForNames.ValueMember = "player_id";
    dropdownForNames.DisplayMember = "name";
    dropdownForNames.DataSource = table;


    // Close connection
    connection_db.Close();
}

【讨论】:

    猜你喜欢
    • 2013-07-08
    • 2015-03-05
    • 1970-01-01
    • 2023-03-05
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多