c# - 如何在C#中的一个循环中运行两个foreach循环

标签 c# sqlite

我不知道如何描述我的问题,但是我想要的结果是See Image

我的代码:

using (SQLiteConnection con = new SQLiteConnection(AppSettings.ConnectionString()))
        {
            con.Open();
            using (SQLiteDataAdapter sda = new SQLiteDataAdapter("Select * From Deals_FF Where Deal_Code = '" + Deal_Code + "'", con))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);

                foreach (DataRow dr in dt.Rows)
                {
                    int n = SalesRegisterControl.Instance.Sales_Grid.Rows.Add();
                    SalesRegisterControl.Instance.Sales_Grid.ClearSelection();
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[1].Value = dr["Deal_Name"].ToString();
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[2].Value = dr["Deal_Price"].ToString();
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[3].Value = 1;
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[4].Value = dr["Deal_Price"].ToString();
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[5].Value = "FF";
                    break;
                }

                foreach (DataRow dr in dt.Rows)
                {
                    int n1 = SalesRegisterControl.Instance.Sales_Grid.Rows.Add();
                    SalesRegisterControl.Instance.Sales_Grid.ClearSelection();
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n1].Cells[1].Value = dr["Item_Name"].ToString();
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n1].Cells[2].Value = 0;
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n1].Cells[3].Value = dr["Quantity"].ToString();
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n1].Cells[4].Value = 0;
                    SalesRegisterControl.Instance.Sales_Grid.Rows[n1].Cells[5].Value = "FF";
                }
            }
        }


有解决这个问题的更好方法吗?
我添加了两个foreach循环来解决我的问题,但是我做的代码不好,请问如何以更好的方式做到这一点?

最佳答案

您可以尝试使用bool控制循环,然后组合两个foreach。第一次运行时,bool设置为false。

bool IsFirst = true;
foreach (DataRow dr in dt.Rows)
{
    int n = SalesRegisterControl.Instance.Sales_Grid.Rows.Add();
    if (IsFirst)
    {
        SalesRegisterControl.Instance.Sales_Grid.ClearSelection();
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[1].Value = dr["Deal_Name"].ToString();
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[2].Value = dr["Deal_Price"].ToString();
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[3].Value = 1;
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[4].Value = dr["Deal_Price"].ToString();
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[5].Value = "FF";
        IsFirst = false;
    } else
    {
        SalesRegisterControl.Instance.Sales_Grid.ClearSelection();
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[1].Value = dr["Item_Name"].ToString();
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[2].Value = 0;
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[3].Value = dr["Quantity"].ToString();
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[4].Value = 0;
        SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[5].Value = "FF";
    }
}


编辑

或者,您不需要第一个循环仅分配这样的值。

int n = SalesRegisterControl.Instance.Sales_Grid.Rows.Add();
SalesRegisterControl.Instance.Sales_Grid.ClearSelection();
SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[1].Value = dr["Deal_Name"].ToString();
SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[2].Value = dr["Deal_Price"].ToString();
SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[3].Value = 1;
SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[4].Value = dr["Deal_Price"].ToString();
SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[5].Value = "FF";

foreach (DataRow dr in dt.Rows)
{
    n = SalesRegisterControl.Instance.Sales_Grid.Rows.Add();
    SalesRegisterControl.Instance.Sales_Grid.ClearSelection();
    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[1].Value = dr["Item_Name"].ToString();
    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[2].Value = 0;
    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[3].Value = dr["Quantity"].ToString();
    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[4].Value = 0;
    SalesRegisterControl.Instance.Sales_Grid.Rows[n].Cells[5].Value = "FF";
}

关于c# - 如何在C#中的一个循环中运行两个foreach循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53143790/

相关文章:

sql - 全文搜索只匹配某些词?

Android sqlite 更新问题

c# - 将自动化 ID 添加到绑定(bind)组合框项目

c# - C# .net 中的 win32 dll

c# - xml文件拆分算法

android - SQLite异常: No such table found whereas a table was created

sqlite - 如何在 Electron 应用程序中使用带有sqlite3的变量

c# - 如何在 Windows Phone 8.1 上使用 Zxing 创建二维码图像

c# - 联合扇出查询

swift - 如何使用 SQLite.SWIFT 删除行?