1

I don't know how to describe my problem but what result I wanted is See Image

My Code:

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";
                }
            }
        }

Any Better way to solve this problem? I add two foreach loop to solve my problem but the code I do is not good how can I do it in better way please guide?

Bryan Bell
  • 27
  • 7
  • What's the difference between two loop? – D-Shih Nov 04 '18 at 18:04
  • @D-Shih Actually I have deals table and every deal has multiple items like Deal 1 has `Pizza`,`Fries`,`Drink` I just want to select deal name first and then it's items with price 0 I don't want to repeat the price with every item that's why in the first loop I break it. – Bryan Bell Nov 04 '18 at 18:11
  • Possible duplicate of [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) – mjwills Nov 04 '18 at 22:46

1 Answers1

2

You can try to use a bool to control your loop then combine two foreach. when you had run first time bool set 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";
    }
}

Edit

Or you don't need the first loop only assign values like this.

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";
}
D-Shih
  • 44,943
  • 6
  • 31
  • 51
  • 2
    Better to put the whole `IsFirst` outside the loop and assign values from the first row. Inside the loop is a hack. – CodingYoshi Nov 04 '18 at 18:43