הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, November 30, 2013 8:18 AM
I have datatable name dt
With column1 column2
A 2
A 3
B 1
C 4
AND i add this table to be datasource of combobox1 and combobox1
combobox1.datasource = dt
combobox1.displaymember = "Column1"
combobox2.datasource = dt
combobox2.displaymember = "Column2"
So when combobox1 or combobox2 select change other one will be auto change to the same row of datasource
But item of combobox1 is duplicate i want to remove duplicate from combobox1 and when combobox1 select A , combobox2
item will be only 2 , 3. How can i do this?
Thanks
All replies (3)
Saturday, November 30, 2013 10:10 AM ✅Answered
I would use some Enumerable funcions to get this work i.e.
comboBox1.Items.AddRange(dt.AsEnumerable().Select(i => i.ItemArray[0]).Distinct().ToArray());
comboBox2.Items.AddRange(dt.AsEnumerable().Select(i => i.ItemArray[1]).Distinct().ToArray());
Saturday, November 30, 2013 10:14 AM ✅Answered
If you use a Datatable like you seems to do then use the overloaded function of the DataView which returns a distinct table. It is special made for your question. A sample is on this good MSDN page.
http://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx
Success
Cor
Saturday, November 30, 2013 2:08 PM ✅Answered
Solution 1 works off Column1, note use of DataTable.Copy and BindingSource.Filter. We could do away with BindingSources yet I see it makes sense to easily filter. I tend to use them for all Windows form projects.
Public Class frmDemo
WithEvents bsData1 As New BindingSource
WithEvents bsData2 As New BindingSource
Private Sub frmDemo_Load(
ByVal sender As System.Object,
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add(New DataColumn With
{
.ColumnName = "Column1",
.DataType = GetType(String)
}
)
dt.Columns.Add(New DataColumn With
{
.ColumnName = "Column2",
.DataType = GetType(Integer)
}
)
dt.Rows.Add(New Object() {"A", 2})
dt.Rows.Add(New Object() {"A", 3})
dt.Rows.Add(New Object() {"B", 1})
dt.Rows.Add(New Object() {"C", 4})
bsData1.DataSource = dt
bsData2.DataSource = dt.Copy
ComboBox1.DisplayMember = "Column1"
ComboBox1.DataSource = bsData1
ComboBox2.DisplayMember = "Column1"
ComboBox2.DataSource = bsData2
End Sub
Private Sub bsData1_PositionChanged(
ByVal sender As Object,
ByVal e As System.EventArgs) _
Handles bsData1.PositionChanged
If bsData1.Current IsNot Nothing Then
Dim Identifier As String =
CType(bsData1.Current, DataRowView).Row.Field(Of String)("Column1")
bsData2.Filter = "Column1 <> '" & Identifier & "'"
End If
End Sub
Private Sub cmdRemoveFilter_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) _
Handles cmdRemoveFilter.Click
bsData2.Filter = ""
End Sub
End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.