-
Jan 4th, 2022, 12:41 AM
#1
Thread Starter
Addicted Member
[RESOLVED] remove duplicate from combo box connected to database
I am trying to remove duplcates from a combobox however this doesn't seem to result in a correct combobox, since he fill the combobox with the following text.
"System.Linq.Enumerable+d__64`1[System.Char]"
I have the following code and are using a access database with generated datasets.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Linq.Expressions;
namespace Generator
{
private void matGroupBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.matGroupBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.matGroupDataSet);
}
private void frmImport_Load(object sender, EventArgs e)
{
this.matGroupTableAdapter.Fill(this.materialGroupDataSet.MatGroup);
//Distinct
var BaseM = materialGroupDataSet.MatGroup.Select(row => row["BaseMs"].ToString().Distinct());
foreach (var BaseMs in BaseM)
{
cmbBaseMat.Items.Add(BaseMs);
}
}
I have 3 comboboxes that i want to fill with data from the database. CoboBox 1 is filled with data and then it the removes the doubles (duplicates). When i select a value from ComboBox 1 it will update CoboBox2 with the possible choices based on ComboBox 1 and if I select a value from ComboBox 2 it fills ComboBox3 with resulting data.
I'm now first trying to filter out the duplicates.
-
Jan 4th, 2022, 02:39 AM
#2
Re: remove duplicate from combo box connected to database
Think about the logic and then write code to implement that logic. That is not what you have done. If you need to, pick up a pen and paper and write the logic out. What you want to do is select the BaseMs values from the rows and then get the distinct values from the result. Is that what you're doing? No it is not. That Distinct is being executed before the Select, so how can that do what you need it to? The Select must come before the Distinct.
-
Jan 4th, 2022, 02:41 AM
#3
Re: remove duplicate from combo box connected to database
Also, if you're going to use LINQ to DataSet then you should use it everywhere, i.e.
should be:
vb.net Code:
row.Field(Of String)("BaseMs")
-
Jan 4th, 2022, 03:00 AM
#4
Re: remove duplicate from combo box connected to database
Finally, you should also call AddRange or else set the DataSource rather than calling Add in a loop. You can call ToArray on your results first and then use that with either.
-
Jan 5th, 2022, 11:50 AM
#5
Thread Starter
Addicted Member
Re: remove duplicate from combo box connected to database
The following seems to work for me.
Code:
cmbBaseMat.DataSource = materialGroupDataSet.MatGroup.Select(x => x.BaseMs).Distinct().ToList();
now is the question how do i make 2 other comboboxes dependent on the selected value from the first choice. Thus filter the combobox based on the selection made.
-
Jan 5th, 2022, 10:30 PM
#6
Re: remove duplicate from combo box connected to database
Originally Posted by rakker
now is the question how do i make 2 other comboboxes dependent on the selected value from the first choice. Thus filter the combobox based on the selection made.
Look at the title of this thread:
remove duplicate from combo box connected to database
What has that question got to do with that subject? Nothing, so it doesn't belong in this thread. The issue you asked about is now resolved, so you should use the Thread Tools menu to mark this thread Resolved. If you have a different question on a different topic, create a new thread with a title that reflects that topic and a post that contains all and only the information relevant to that topic. One topic per thread and one thread per topic keeps the forum orderly and easier for everyone to use.
-
Jan 6th, 2022, 01:11 AM
#7
Thread Starter
Addicted Member
Re: remove duplicate from combo box connected to database
J*sus, man sorry for writing that, you dont need to react like that. I wrote both questions, som i'm not outside of my original question.
On topic thank you for pointing me in the right drection.
-
Jan 6th, 2022, 08:50 PM
#8
Re: remove duplicate from combo box connected to database
Originally Posted by rakker
you dont need to react like that
Like what? By pointing out what you did wrong and what you should do instead? Oh, you poor thing. Seriously, so many people around here react to the simplest bit of criticism like you're stabbing their mother. Get over it.
BTW, I see that you still haven't bothered to mark this thread Resolved. Maybe you'll get to that when your mother recovers.
-
Jan 7th, 2022, 03:01 AM
#9
Thread Starter
Addicted Member
Re: remove duplicate from combo box connected to database
No, you could just have reacted normal without sounding condescending. I included this question in the first place so i thought to continue in the same topic, that i assumed wrong is on me, but I would have understood if you just had said it at a normal tone. Just writing that my question was answered and that i should start a new thread would have been enough.
This is a forum where people ask questions opinion. You don't know me, i'm not easily offended or have a hard time with critique so this comment from me is totally on you.
I appreciate the help you gave, so nothing wrong with that.
-
Jan 7th, 2022, 04:52 AM
#10
Re: remove duplicate from combo box connected to database
Originally Posted by rakker
You don't know me, i'm not easily offended or have a hard time with critique so this comment from me is totally on you.
Seems you've misjudged yourself considerably.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|