MarwanAbu
MarwanAbu

Reputation: 179

How to compare data from 2 DATAGRIDVIEW cells?

I have 2 DATAGRIDVIEWS and I need to insert the data in case of cell 3 value in datagridview 1 not equal cell 3 value in datagridview 2

This is the code of save button :

private void btnSave_Click(object sender, EventArgs e)
        {
            
       for (int i = 0; i < dgvResult.Rows.Count; i++)
            {
                if (!String.IsNullOrEmpty(dgvResult.Rows[i].Cells[3].Value.ToString()))
                {
                    if (dgvResult.Rows[i].Cells[3].Value.ToString() != dgvresultold.Rows[i].Cells[3].Value.ToString())
                    {
                        result.UPDATED_RESULTS(Convert.ToInt32(txtOrder.Text),
                                          Convert.ToInt32(dgvResult.Rows[i].Cells[0].Value),
                                                 txtApprovedby.Text,
                                         DateTime.Parse(dateTimeApprove.Value.ToString()),
                                         dgvResult.Rows[i].Cells[3].Value.ToString(),
                                         
                                        Convert.ToInt32(dgvResult.Rows[i].Cells[4].Value.ToString()),
                                         Convert.ToInt32(txtMRN.Text),
                                         Convert.ToInt32(textCustId.Text),
                                         txtApprovedby.Text,
                                         DateTime.Parse(dateTimeApprove.Value.ToString()), 1,
                                         Convert.ToInt32(dgvResult.Rows[i].Cells[7].Value),
                                         dgvResult.Rows[i].Cells[8].Value.ToString());

                      }
        

    }

I used this condition to compare cells value in both datagridviews then insert the rows with different values :

 if (dgvResult.Rows[i].Cells[3].Value.ToString() != dgvresultold.Rows[i].Cells[3].Value.ToString())

This is the UPDATED_RESULTS stored procedure used for insert the data into database :

ALTER proc [dbo].[UPDATED_RESULTS]
@ORDER_ID int,
@TESTID int,
@APPROVED_BY varchar(50),
@APPROVED_DATE datetime, 
@RESULT_NUMBER varchar(50),
@MACHINE_ID int,
@patient_no int,
@custid int,
@CORRECTED_BY varchar(50),
@CORRECTED_DATE datetime,
@messageid int,
@deptid int,
@REQ_FORM_NO varchar(50)
AS
INSERT INTO [dbo].[LAB_RESULTS_UPDATED]
           ([TESTID],[ORDER_ID],[APPROVED_BY],[APPROVED_DATE],[RESULT_NUMBER],[machine_id],[patient_no],[custid],[CORRECTED_BY],[CORRECTED_DATE]
           ,[messageid],deptid,REQ_FORM_NO)
           values 
           (@ORDER_ID,@TESTID,@APPROVED_BY,@APPROVED_DATE,@RESULT_NUMBER,@MACHINE_ID,@patient_no,@custid,@CORRECTED_BY,@CORRECTED_DATE,
           @messageid,@deptid,@REQ_FORM_NO)
    

And this is the image of 2 datagridviews :

enter image description here

The issue now when click save all the rows saved in the database but I need only to insert different results only what is the missing in my code and how to solve it and save the rows only if the result in cell 3 different ?

EDIT :

I changed the code but now its not saving and not inserting data into database I think the problem in comparison its not working correct :

for (int i = 0; i < dgvResult.Rows.Count-1; i++)
            {
                if (!String.IsNullOrEmpty(dgvResult.Rows[i].Cells[3].Value.ToString()))
                {
                    if (!String.Equals(dgvResult.Rows[i].Cells[3].Value.ToString() , dgvresultold.Rows[i].Cells[3].Value.ToString()))
                    {

                                          
                            result.UPDATED_RESULTS(Convert.ToInt32(txtOrder.Text),
                                              Convert.ToInt32(dgvResult.Rows[i].Cells[0].Value),
                                                     txtApprovedby.Text,
                                             DateTime.Parse(dateTimeApprove.Value.ToString()),
                                             dgvResult.Rows[i].Cells[3].Value.ToString(),
                                             Convert.ToInt32(dgvResult.Rows[i].Cells[4].Value.ToString()),
                                             Convert.ToInt32(txtMRN.Text),
                                             Convert.ToInt32(textCustId.Text),
                                             txtApprovedby.Text,
                                             DateTime.Parse(dateTimeApprove.Value.ToString()), 1,
                                             Convert.ToInt32(dgvResult.Rows[i].Cells[7].Value),
                                             dgvResult.Rows[i].Cells[8].Value.ToString());

Upvotes: 0

Views: 142

Answers (1)

MarwanAbu
MarwanAbu

Reputation: 179

Finally I found the error its in the stored procedure its not in correct order

@ORDER_ID and @TESTID in parameter list different from insert list (TESTID,ORDER_ID)

I changed its order and the code working correct this is the error

THE CODE NOW WORKING CORRECT AND COMPARISON SUCCEEDED

ALTER proc [dbo].[UPDATED_RESULTS]
@ORDER_ID int,
@TESTID int,
@APPROVED_BY varchar(50),
@APPROVED_DATE datetime, 
@RESULT_NUMBER varchar(50),
@MACHINE_ID int,
@patient_no int,
@custid int,
@CORRECTED_BY varchar(50),
@CORRECTED_DATE datetime,
@messageid int,
@deptid int,
@REQ_FORM_NO varchar(50)
AS
INSERT INTO [dbo].[LAB_RESULTS_UPDATED]
           ([ORDER_ID],[TESTID],[APPROVED_BY],[APPROVED_DATE],[RESULT_NUMBER],[machine_id],[patient_no],[custid],[CORRECTED_BY],[CORRECTED_DATE]
           ,[messageid],deptid,REQ_FORM_NO)
           values 
           (@ORDER_ID,@TESTID,@APPROVED_BY,@APPROVED_DATE,@RESULT_NUMBER,@MACHINE_ID,@patient_no,@custid,@CORRECTED_BY,@CORRECTED_DATE,
           @messageid,@deptid,@REQ_FORM_NO)

Upvotes: 0

Related Questions