Заполнить Datagridview из БД VB.NET - PullRequest
1 голос
/ 23 февраля 2012

У меня есть MySQL запрос выбранных полей (item_code, item_name, количество товара, item_selltype, item_selldetail), но некоторые из этих полей являются вариантами.

Dim sqlConn As String = ("SELECT item_code, item_name, item_quantity, item_selltype, item_selldetail FROM qa_items")
Dim objDataAdapter As New MySqlDataAdapter(sqlConn, objConn)

Я полагаю, что запрос возвращает:

item_code, item_name, item_quantity, item_selltype, item_selldetail
01         Ball       5              unit           10.52
02         Keyoard    10             unit           50.00
03         Gasoline   5              gallon         70.45

Элемент управления DataGridView выглядит так:

Code, Name, Inv, Unit Price, Box Price, Pound Price, Gallon Price

тогда мне нужен этот результат в datagridview:

Code, Name,    Inv, Unit Price, Box Price, Pound Price, Gallon Price
01    Ball     5    10.52       0          0            0
02    Keyboard 10   50.00       0          0            0
03    Gasoline 5    0           0          0            70.45

Я искал во многих отношениях некоторый код, который может это сделать, но у меня ничего не получается, помогите ..

1 Ответ

1 голос
/ 23 февраля 2012

Я думаю, вам нужен объект-переводчик. Получите ваши данные из базы данных и используйте их для заполнения List<> примерно так:

class Item
{
    public int Code { get; private set; }
    public string Name { get; private set; }
    public int Inv { get; private set; }
    [DisplayName("Unit Price")]
    public double UnitPrice { get; private set; }
    [DisplayName("Box Price")]
    public double BoxPrice { get; private set; }
    [DisplayName("Pound Price")]
    public double PoundPrice { get; private set; }
    [DisplayName("Gallon Price")]
    public double GallonPrice { get; private set; }

    public Item(int item_code, string item_name, int item_quantity, string item_selltype, double item_selldetail)
    {
        Code = item_code;
        Name = item_name;
        Inv = item_quantity;
        UnitPrice = 0;
        BoxPrice = 0;
        PoundPrice = 0;
        GallonPrice = 0;
        switch (item_selltype)
        {
            case "unit": UnitPrice = item_selldetail; break;
            case "box": BoxPrice = item_selldetail; break;
            case "pound": PoundPrice = item_selldetail; break;
            case "gallon": GallonPrice = item_selldetail; break;
            default: throw new InvalidExpressionException();
        }
    }
}

Затем установите DataSource сетки на это List<Item>.

...