如何比较来自2个DataGridView单元格的数据?
个dataGridViews,我需要在datagridview中的单元3值中插入数据
。
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());
}
}
我有2 具有不同值的行:
if (dgvResult.Rows[i].Cells[3].Value.ToString() != dgvresultold.Rows[i].Cells[3].Value.ToString())
这是用于将数据插入数据库的Updated_Results存储的过程:
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)
这是2个DataGridViews的映像:
现在,当单击保存所有保存在数据库中的行时,现在的问题我只需要仅插入不同的结果,我的代码中缺少什么以及如何求解它并仅在单元格3中的结果不同时保存行?
编辑:
我更改了代码,但现在它没有保存,而不是将数据插入数据库中,我认为与之相比,问题不正确:
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());
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 :
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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后,我在存储过程中发现了它的错误,它的顺序不正确
@Order_id和@testid在参数列表中与插入列表不同(testID,order_id),
我更改了其顺序,并且正确的代码正常工作,这是错误
现在正常工作的错误比较成功
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