如果记录已在datagrid视图中,则使用LINQ to SQL过滤数据 [英] filtering data using LINQ to SQL if the records are already in datagrid view

查看:76
本文介绍了如果记录已在datagrid视图中,则使用LINQ to SQL过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用数据库中的数据填充组合框.但是我需要为datagridview中的数据过滤数据.如果在datagrid视图中已经选择了数据,则不要在网格视图组合框中显示它.

I am trying to populate a combobox with the data from database. But I need to filter the data for the data in datagridview. if data already selected in datagrid view I am not to show it in the grid view combobox.

var values=(from c in context.ItemListTables
            where  !existing(c.itemid )
                  select new { id = c.itemid, name = c.itemName });
cell.DataSource = values;
cell.DisplayMember = "name";
cell.ValueMember = "id";


private bool existing(long itemid)
      {

          bool exist = false;
          foreach (DataGridViewRow r in dataGridView1.Rows)
          {
              if ((long)r.Cells["itemid"].Value == itemid)
              {
                  exist = true;
                  break;

              }

          }
          return exist;
      }

推荐答案

J.我不确定我是否理解正确,但您想确保下拉菜单中的该项目在您的系统中不存在gridview对吗?请在下面找到我的解决方案,尽管我懒于创建数据的db示例,所以我只是出于示例目的手动创建了它.

这是我的aspx:
Hi J. I am not sure if i understand it correctly but you wanna make sure that that item in your dropdown does not exist in your gridview right? please find my solution below, though I was so lazy to create a db sample of data so i just manually created it for example purposes.

this is my aspx:
<asp:GridView ID="myGrid" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="id" />
        <asp:BoundField DataField="Value" HeaderText="value" />
    </Columns>
</asp:GridView>
<asp:DropDownList ID="mydropdown" runat="server">
</asp:DropDownList>



后面的代码:

您基本上需要一个实体,例如代表您的表结构的类:



code behind:

you basically need an entity, like a class that represent your table structure:

//this is my entity
public class MyEntity
{
    public MyEntity(string id, string value)
    {
        ID = id;
        Value = value;
    }
    public string ID { get; set; }
    public string Value { get; set; }
}



页面加载事件处理程序,该方法调用方法来预填充控件



a page load event handler that calls method to prefill your controls

//at page load
        protected void Page_Load(object sender, EventArgs e)
        {
            this.FillGrid();
            this.FillDropdownUsingGenericList();
        }



手动添加功能以预填充控件.在页面加载事件中被调用



Manually added function to prefill your controls. this is being called in page load event

//function to prefill the dropdown control
private void FillDropdownUsingGenericList()
{
    this.mydropdown.DataTextField = "Value";
    this.mydropdown.DataValueField = "Id";

    this.mydropdown.DataSource = this.DropdownSource();
    this.mydropdown.DataBind();
}

//function to prefill the gridview control
private void FillGrid()
{
    this.myGrid.DataSource = this.GridSource();
    this.myGrid.DataBind();
}




只需将其想象为来自数据库的数据,然后返回您实体的通用列表即可.这是您的gridview的数据源:




Just imagine this as the data that''s coming from your database and thus return a generic list of your entity. this is the datasource for your gridview:

//function that's responsible of identifying the data to be loaded in your gridview
private List<MyEntity> GridSource()
{
    List<MyEntity> list = new List<MyEntity>();
    list.Add(new MyEntity("ID1", "Boy"));
    list.Add(new MyEntity("ID3", "Bakla"));
    list.Add(new MyEntity("ID5", "Bata"));
    return list;
}




这是重要的功能,以下是您的下拉菜单的数据源.在此函数内部,它将过滤要在下拉菜单中显示的数据,并且这些数据一定不能存在于gridview中.




This is the IMPORTANT FUNCTION, the following is your data source for your dropdown. inside this function it filters the data to be displayed in your dropdown and these data must not exist in the gridview

//function that's responsible identifying the data to be loaded in your dropdown
        private List<MyEntity> DropdownSource()
        {
            //dropdown source
            List<MyEntity> dropdownlist = new List<MyEntity>();
            dropdownlist.Add(new MyEntity("ID1", "Boy"));
            dropdownlist.Add(new MyEntity("ID2", "Girl"));
            dropdownlist.Add(new MyEntity("ID3", "Bakla"));
            dropdownlist.Add(new MyEntity("ID4", "Tomboy"));
            dropdownlist.Add(new MyEntity("ID5", "Bata"));
            dropdownlist.Add(new MyEntity("ID6", "Matanda"));
            dropdownlist.Add(new MyEntity("ID7", "Hayop"));

            //grid view source
            List<MyEntity> gridList = (List<MyEntity>)this.myGrid.DataSource;

            //before binding the dropdown source to the dropdown you have to remove items that was already in the gridview
            var x = from d in dropdownlist
                    join g in gridList
                    on d.ID equals g.ID into leftJoined
                    where !leftJoined.Any()
                    select d;

            return x.ToList<MyEntity>();
        }



如果这不能解决您的问题,如果我误解了您的要求,我深表歉意...
请让我知道这是否解决了您的问题,如果没有,请让我知道我在想什么. :)

干杯..



If this does not address your problem, my apology if i misunderstood ur requirements...
please lemme know if this one address ur problem and if not please lemme know what am i missing. :)

cheers..


这篇关于如果记录已在datagrid视图中,则使用LINQ to SQL过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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