Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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";

C#
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;
      }
Posted
Comments
Mastersev 8-Dec-11 16:15pm    
Can you explain more?

1 solution

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:
XML
<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:
C#
//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
C#
//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
C#
//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();
}



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:
XML
//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;
}



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
XML
//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..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900