列表框不会填充数据

时间:2015-06-02 20:10:13

标签: c# listbox

我已经设置了一个名为lboxsupplier的列表框,我还创建了一个数据适配器,然后我用它来填充供应商列表框。当我运行表单时,列表框为空。我希望列表框中填充供应商ID和公司,然后我将点击该列表框以填充产品的另一个列表框。

Namespace Pennyburn_Greg
{
public partial class FormProcess : Form
{
    SqlDataAdapter daSupplier;
    DataSet dsPennyburnGreg = new DataSet();
    SqlConnection conn;
    SqlCommand cmdSupplierDetails;
    SqlCommandBuilder cmdBSupplier;
    DataRow drSupplier;
    String connstr, sqlSupplier;

    public FormProcess()
    {
        InitializeComponent();
    }



private void FormProcess_Load(object sender, EventArgs e)
{
    connstr = @"Data Source= arlene-pc; Initial Catalog= PennyburnGreg; Integrated Security=True";

    //dataAdapter for supplier listbox

        sqlSupplier = @"Select* from Supplier";
        conn = new SqlConnection(connstr);
        cmdSupplierDetails = new SqlCommand(sqlSupplier, conn);
        daSupplier = new SqlDataAdapter(cmdSupplierDetails);
        daSupplier.FillSchema(dsPennyburnGreg, SchemaType.Source, "Supplier");
}
    private void filllboxsupplier(string str)
    {
        daSupplier.Fill(dsPennyburnGreg, "Supplier");
        lboxsupplier.DataSource = dsPennyburnGreg.Tables["Supplier"];
        lboxsupplier.DisplayMember = "Company";
        lboxsupplier.ValueMember = "SupplierID";


    }

    }
}

3 个答案:

答案 0 :(得分:1)

首先,为什么要调用var chartheight = 500; var gap = 5; var count = 20; var spaceForScales = 70; c.fixedBarHeight((chartheight - (count + 1) * gap - spaceForScales) / count); ,而应该调用FillSchema方法来获取数据,例如

Fill

填好数据集后,在daSupplier.Fill(dsPennyburnGreg, "Supplier"); 中,您可以将数据集作为数据源添加到列表框中,如

FormProcess_Load()

答案 1 :(得分:1)

您需要做的第一件事是稍微松散地耦合您的UI和数据。试试这段代码:

// Returns a DataTable of ALL suppliers
private DataTable GetSuppliers()
{
    return GetSuppliers(0);
}

// Returns a DataTable of the given supplier
private DataTable GetSuppliers(int supplierId)
{
    using (var connection = new SqlCommand())
    {
        connection.ConnectionString = @"Data Source= arlene-pc; Initial Catalog= PennyburnGreg; Integrated Security=True";
        using (var command = new SqlCommand())
        {
            connection.Open();
            command.CommandType = CommandType.Text;
            command.Connection = connection;

            if (supplierId == 0)
            { 
                command.commandText = "SELECT * FROM Supplier";
            }
            else
            {
                command.commandText = "SELECT * FROM Supplier WHERE SupplierId=@id";
                command.Parameters.AddWithValue("@id", supplierId);
            }
            using (var adapter = new SqlDataAdapter())
            {
                using (var ds = new DataSet())
                {
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);

                    if (ds.Tables.Count > 0)
                        return ds.Tables[0];
                }
            }
        }
    }

    return null;
}

现在你可以这样做:

lboxsupplier.DataSource = GetSuppliers(int.Parse(lboxsupplier.SelectedValue));
lboxsupplier.DisplayMember = "Company";
lboxsupplier.ValueMember = "SupplierID";

如果您需要所有供应商,请执行以下操作:

lboxsupplier.DataSource = GetSuppliers();
lboxsupplier.DisplayMember = "Company";
lboxsupplier.ValueMember = "SupplierID";

此代码将提供一些分离。这仍然不是理想的,但胜过你所拥有的。

答案 2 :(得分:0)

您在FormProcess_Load中没有对列表框控件执行任何操作,因此在首次加载时它将为空。我假设您lboxsupplier_Click绑定Click事件lboxsupplier?如果是这样,那么你需要点击该列表框才能填充数据集(这是一种非常奇怪的用户体验,但如果这真的是你需要的话......)。如果lboxsupplier_Click不是事件处理程序,那么您将不得不手动调用它。

如果仍然没有填充,则尝试直接对数据库运行查询,并确保它返回数据并且具有名为“Company”和“SupplierID”的列