C# .NET - 将信息从 SQL DB 加载到按钮数组,这些按钮不按顺序

问题描述 投票:0回答:1

我正在编写一个销售点 (POS) 应用程序,其中 SQL 数据库中有布局编号 (1-35)、文本(价格和产品名称)和按钮图像。数据库中有一个 ItemLayout 表,其中包括项目 ID (PLU)、布局编号 (1-35)、按钮是否应该首先可见(所有工作)以及项目的组。它们通过以下功能读取:

public ILMButtonsProps[] getILMButtonProps(string GroupName)
{
    ILMButtonsProps[] result = new ILMButtonsProps[getILMAmount(GroupName)];
    try
    {
        SqlConnection con = new SqlConnection(returnConString());
        con.Open();

        string operation = "SELECT * FROM ItemLayout WHERE ItemGroup=@ItemGroup";
        SqlCommand cmd = new SqlCommand(operation, con);
        cmd.Parameters.AddWithValue("@ItemGroup", GroupName);
        SqlDataReader reader = cmd.ExecuteReader();

        int count = 0;
        while (reader.Read())
        {
            result[count] = new ILMButtonsProps();
            result[count].ItemGroup = reader["ItemGroup"].ToString();
            result[count].LayoutNumber = reader["Number"].ToString();
            result[count].Enable = Convert.ToInt32(reader["Enable"].ToString());
            result[count].PLU = reader["PLU"].ToString();

            count++;
        }
        reader.Close();
        con.Close();

    }
    catch (Exception ex)
    {
        helperClass.throwError(ex.Message, ex.Source);
    }
    return result;
}

用于获取数组长度的函数:

public int getILMAmount(string GroupName)
{
    int result = 0;
    try
    {
        SqlConnection con = new SqlConnection(returnConString());
        con.Open();

        string operation = "SELECT * FROM ItemLayout WHERE ItemGroup=@GroupName";
        SqlCommand cmd = new SqlCommand(operation, con);
        cmd.Parameters.AddWithValue("@GroupName", GroupName);
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            result++;
        }
        reader.Close();
        con.Close();

    }
    catch (Exception ex)
    {
        helperClass.throwError(ex.Message, ex.Source);
    }
    return result;
}

以及使用的类(我认为这在这里不是很重要,但无论如何:)

internal class  ILMButtonsProps
{
    internal string ItemGroup;
    internal string LayoutNumber;
    internal int Enable;
    internal string PLU;
}

这也是表格设计器中的表格:

当我使用下一段代码调用

getILMButtonProps
函数时,我以奇怪的模式得到所有结果。

//Load buttons
ILMButtonsProps[] array = SQL.getILMButtonProps(cmbILMGroups.SelectedItem.ToString());
int counter = 0;
int max = array.Length;
Button[] btns = panel4.Controls.OfType<Button>().ToArray();


if (array == null)
{
    helperClass.throwError("There was an error reading the arrangements for the buttons in the database! Please try again...", "NPManager");
    return;
}

for (int x = 0; x < max; x++)
{
    //If all props are set, exit loop
    if (counter == max || counter == 35) { break; }

    //If there is a null in the layout, skip it.
    if (array[counter] == null)
    {
        counter++;
        return;
    }

    //Enable t/f
    //Get details from DB and input to buttons
    int i = Convert.ToInt32(array[counter].LayoutNumber);
    if (array[counter].Enable == 1)
    {


        btns[i].Text = "";
        btns[i].BackgroundImage = NPManager.Properties.Resources._109_AllAnnotations_Error_24x24_72;
    }

    if (array[counter].Enable == 2)
    {
        ILMDataPoints dp = SQL.getDataRowFromPLU(array[counter].PLU);

        btns[i].Text = dp.itemName + " @ " + dp.itemPrice;
        if(dp.image == null)
        {
            btns[i].BackgroundImage = null;
            btns[i].ForeColor = Color.Black;
        }
        if (dp.image != null && string.IsNullOrEmpty(dp.image.ToString()) && dp.image == System.DBNull.Value && dp.image.ToString() == "{}")
        {
            btns[i].BackgroundImage = GetImageFromByteArray((byte[])dp.image);

            PictureAnalysis pictureAnalysis = new PictureAnalysis();
            btns[i].ForeColor = GetReadableForeColor(pictureAnalysis.GetMostUsedColor(GetImageFromByteArray((byte[])dp.image))); //N
        }
    }

    counter++;
}

ILMDatapoints (dp) 变量是自定义类

结果如下: Output

如您所见,按钮排列是从上到下交替的,但我希望它是从左到右。我不明白这是怎么可能的,因为我使用多种方法来确保按钮和属性处于正确的顺序(数字顺序)

如果有人可以帮忙,那就太好了。

谢谢你, EB.

c# sql .net winforms point-of-sale
1个回答
0
投票

尝试在 SQL 查询中按布局编号排序

© www.soinside.com 2019 - 2024. All rights reserved.