检查datagridview单元格中的值是否存在于另一个表中 [英] check if values in datagridview cells exists in another table

查看:153
本文介绍了检查datagridview单元格中的值是否存在于另一个表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个fileupload应用程序并且有一个挑战阻止我。到目前为止,该应用程序允许用户使用openfiledialog控件上传和excel文件,然后他们可以在将数据发送到数据库之前在datagridview中预览数据。



问题是我需要确认数据网格中两列单元格中的值已经存在于我的数据库中的另一个表中,然后才能提交datagridview数据库。



为了帮助理解我的问题,我们假设我有 DatagridView1 ,我需要确认<单元格中的值<在将 DatagridView1 发送到数据库之前,b> Column1 和 Column2 已存在于 TableProducts 中。



我们将不胜感激任何帮助或想法。

I am building an fileupload app and have got one challenge holding me back. So far, the app allows users upload and excel file using the openfiledialog control, and then they can preview the data in a datagridview before sending it to the database.

The issue is I need to confirm that the values in the cells of two columns in the datagrid already exists in another table in the my database before they can submit the datagridview to the database.

To help understand my issue, lets assume I have DatagridView1 and I need to confirm that the Values in cells of Column1 and Column2 already exist in TableProducts before sending the DatagridView1 to the database.

Any help or ideas will be appreciated.

推荐答案

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace console_poc
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Entity> lstDBData = new List<Entity>();
            // fetch all the db data from that table which u want to compare and store it in a list
            // or a datatable for LINQ i have used List<T>


            List<Entity> lstUploadData = new List<Entity>();
            // store the excel upload data in this list

            List<Entity> lstDataWhichExistsInDb = new List<Entity>();
            // this list will store the data which is available in the db

            List<Entity> lstDataWhichDoesNotExistsInDb = new List<Entity>();
            // this list will store the data which is not available in the db

            // processs..
            // storing the existing db data in one list and new value in another list
            lstUploadData.ForEach(k =>
                {
                    var item = lstDBData.FirstOrDefault(l => l.Column1 == k.Column1 && l.Column2 == k.Column2);
                    if (item == null)
                        lstDataWhichDoesNotExistsInDb.Add(k);
                    else
                        lstDataWhichExistsInDb.Add(k);
                });

            // now based on ur requirement u can use which list u want..


        }
    }
    public class Entity
    {
        public string Column1 { get; set; }
        public string Column2 { get; set; }
    }
}


简单,只需获取您要检查的2个值然后试试这个



its simple, just get the 2 values you want to check then try this

Datarow[] Dr = Datatable1.Select(" Name='Value 1' or Name='Value2'");//replace "Value 1" & "Vaue 2" with your value
if(Dr.Length > 0)
{
    //Means Record Exists
}
else
{
    //Else Not
}


这篇关于检查datagridview单元格中的值是否存在于另一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆