c# - c#如何在一个循环中运行两个foreach循环
问题描述
我不知道如何描述我的问题,但我想要的结果是查看图片
我的代码:
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 循环来解决我的问题,但是我做的代码不好,我该如何以更好的方式做到这一点,请指导?
解决方案
您可以尝试使用 abool
来控制您的循环,然后将两个foreach
. 当你第一次运行时bool
设置为假。
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";
}
推荐阅读
- reactjs - React 中 JSX.Element 的 TypeScript 接口问题
- android - 如何用手指移动元素,但从它所在的位置?
- php - 此路由不支持 PUT 方法。支持的方法:POST。在我的项目中
- c - 实现定义的行为是未定义的行为吗
- shell - Xamarin Form Shell:Flyout 和 TabBar - 仅在 TabBar 中隐藏项目?
- c++ - 确定可移植可执行文件是否是 ssh 客户端应用程序
- android - Android 模拟器已终止
- ios - 如何通过 Swift 生成 App Clip 代码?
- python - 在为连续数据(Python)创建直方图后如何获取概率列表?
- python - 在标签框中加载 CEF