Как сравнить новую запись строки с существующими строками в datagridview c # и добавить sample_no, зависит от условий - PullRequest
0 голосов
/ 06 октября 2019

Я занимаюсь разработкой системы лабораторий и образцов, у меня есть экран запроса, я выбираю лабораторные тесты и добавляю выбранные строки в DATAGRIDVIEW,

каждая строка будет иметь следующий номер образца в зависимости от вида и сечения образца. Все тесты имеют один и тот же тип выборки, и в одном и том же разделе будет использоваться один и тот же номер образца, для разных типов образцов и / или в разделе будет использоваться следующий номер образца.

Я выполнил следующий код, но он работает не так, как ожидалось. пробел также при увеличении номеров образцов, также не принимая следующий номер образца для новых строк и проверьте файл вложения.

private void textDiscount_KeyDown(object sender, KeyEventArgs e)
        {
                int sampleNo1;
                for (int i = 0; i < TestsDataGrid.Rows.Count - 1; i++)
                {
                    if (TestsDataGrid.Rows[i].Cells[0].Value.ToString() == textTestId.Text)
                    {
                        MessageBox.Show("This test added already ", "Duplicate Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    foreach (DataGridViewRow row in TestsDataGrid.Rows)
                    {
                        string sampleKind = TestsDataGrid.Rows[i].Cells[8].Value.ToString();
                        string Section = TestsDataGrid.Rows[i].Cells[9].Value.ToString();

                        if (sampleKind != null && sampleKind == txtSampleKind.Text && Section != null && Section == txtdept.Text)

                        {

                            if (int.TryParse(TestsDataGrid.Rows[i].Cells[7].Value.ToString(), out sampleNo1) == true)
                            {
                                if (sampleNo1 > 0)
                                {
                                    row.Cells[7].Value = sampleNo1;
                                }

                            }
                        }
                        else if (sampleKind != null && sampleKind != txtSampleKind.Text && Section != null && Section != txtdept.Text)
                        {
                            if (int.TryParse(TestsDataGrid.Rows[i].Cells[7].Value.ToString(), out sampleNo1) == true)
                            {
                                if (sampleNo1 > 0)
                                {
                                    row.Cells[7].Value = sampleNo1 + 1;
                                }

                            }
                        }
                    }



            }
            DataRow r = dt.NewRow();
            r[0] = textTestId.Text;
            r[1] = textNane.Text;
            r[2] = textPrice.Text;
            r[3] = textQty.Text;
            r[4] = textAmount.Text;
            r[5] = textDiscount.Text;
            r[6] = textTotal.Text;
            r[7] = txtSampleNo.Text;
            r[8] = txtSampleKind.Text;
            r[9] = txtdept.Text;


            dt.Rows.Add(r);
            TestsDataGrid.DataSource = dt;


            textOrderTotal.Text = (from DataGridViewRow row in TestsDataGrid.Rows
                                   where row.Cells[6].FormattedValue.ToString() != string.Empty
                                   select Convert.ToDouble(row.Cells[6].FormattedValue)).Sum().ToString();


        }
    }

Как обновить мой код, чтобы получить правильный результат и получить тот же номер образца, если (вид образца и раздел для строки одинаковы)

enter image description here

...