vb.net combobox population getting index of the 1st combobox - vb.net

I populated my combobox1 with this code in load
sql = "select name1,id1 from table1"
da = New Odbc.OdbcDataAdapter(sql, con)
da.Fill(ds, "cbtbl1")
ComboBox1.DataSource = ds.Tables("cbtbl1")
ComboBox1.DisplayMember = ds.Tables("cbtbl1").Columns("name1").Caption
I have my 2nd combobox2 related to combobox1. I inserted this code in combobox1 selectedvaluechanged. This to change to value of combobox2 based on their related ids
sql = "select name2,id2 from table2 where id1=" & ???????
da = New Odbc.OdbcDataAdapter(sql, con)
da.Fill(ds, "cbtbl2")
ComboBox2.DataSource = ds.Tables("cbtbl2")
ComboBox2.DisplayMember = ds.Tables("cbtbl2").Columns("name2").Caption
In my code i have question marks. It supposed to be the id of table1 which i don't know how to get :( or what to put

You should set the ValueMember of Combobox1 to be the ID you retrieved from the database and the use the SelectedValue property to retrieve the ID of the selected item.
I don't think it's gonna work unless you specify the ValueMember property when you databind Combobox1, so don't forget to do that first.
OK, I knocked something together quickly with a database I am working on at the moment (it's OLEDB, but shouldn't matter for this)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim ds As New DataSet()
Dim test As New OleDbDataAdapter("SELECT [ID], [名前] FROM [Tレイヤ管理]", DBConnections.PrimaryAccessDBConnection)
Call test.Fill(ds, "testTable")
Me.ComboBox1.DataSource = ds.Tables("testTable")
Me.ComboBox1.ValueMember = "ID"
Me.ComboBox1.DisplayMember = "名前"
AddHandler Me.ComboBox1.SelectedValueChanged, AddressOf Something
End Sub
Private Sub Something(sender As Object, e As EventArgs)
Call MessageBox.Show(String.Format("ID {0}", Me.ComboBox1.SelectedValue))
End Sub
I get the ID showing just fine with this.
UPDATE:
If this still doesn't work then you can get the selected item this way:
Private Sub Something(sender As Object, e As EventArgs)
Dim selectedItem As DataRowView = CType(Me.ComboBox1.SelectedItem, DataRowView)
Call MessageBox.Show(String.Format("ID {0} Name {1}", New Object() {selectedItem("ID"), selectedItem("名前")}))
End Sub

Related

How to populate master detail combobox in VB?

I have two Comboboxes with Master Detail Relationship Table Bank and Branch
My VB code behind:-
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\New\Test.accdb")
Using cmd As New OleDbCommand("Select Bank, ID from Bank", con)
Dim da As New OleDbDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "Bank"
ComboBox1.ValueMember = "ID"
ComboBox1.Text = "Select"
End Using
End Using
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim bankId = ComboBox1.SelectedValue.ToString
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\New\Test.accdb")
Using branch_cmd As New OleDbCommand("Select Branch from Branch where Bank_id ='" & bankId & "'", con)
Dim da As New OleDbDataAdapter(branch_cmd)
Dim dt As New DataTable
da.Fill(dt)
ComboBox2.DataSource = dt
ComboBox2.DisplayMember = "Bank"
ComboBox2.ValueMember = "ID"
End Using
End Using
End Sub
End Class
I want to populate in second combo box based on first combobox selected value, but the code got error on
ComboBox1_SelectedIndexChanged function:
And, from debugging the branch_cmd sql is:
Select Branch from Branch where Bank_id ='System.Data.DataRowView'
EDIT:
I was about to add a note and then I realised that this note is actually the solution to your actual problem. You are setting the DataSource first and then the DisplayMember and ValueMember afterwards. That is wrong and the reason for your issue. When you set the DataSource you have done the binding, so everything happens then and there. The first item in the list is selected so your SelectedIndexChanged handler is executed. You haven't set the DisplayMember or ValueMember yet, so the SelectedValue won't return the appropriate value. ALWAYS set the DataSource last, as I have done in my example below.
ORIGINAL:
Unless you have a large amount of data, you should just get all the data from both tables upfront, bind the data and then let the binding take care of the filtering automatically. You do that using BindingSources and binding the child to a DataRelation rather than a DataTable. Behold!
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim parentTable = GetParentTable()
Dim childTable = GetChildTable()
Dim data As New DataSet
'Create a foreign key relation between the tables.
data.Tables.Add(parentTable)
data.Tables.Add(childTable)
data.Relations.Add("ParentChild", parentTable.Columns("ParentId"), childTable.Columns("ParentId"))
'Bind the parent BindingSource to the parent table.
parentBindingSource.DataMember = "Parent"
parentBindingSource.DataSource = data
'Bind the child BindingSource to the relation.
childBindingSource.DataMember = "ParentChild"
childBindingSource.DataSource = parentBindingSource
parentComboBox.DisplayMember = "ParentName"
parentComboBox.ValueMember = "ParentId"
parentComboBox.DataSource = parentBindingSource
childComboBox.DisplayMember = "ChildName"
childComboBox.ValueMember = "ChildId"
childComboBox.DataSource = childBindingSource
End Sub
Private Function GetParentTable() As DataTable
Dim table As New DataTable("Parent")
table.PrimaryKey = {table.Columns.Add("ParentId", GetType(Integer))}
table.Columns.Add("ParentName", GetType(String))
table.Rows.Add(1, "Parent 1")
table.Rows.Add(2, "Parent 2")
table.Rows.Add(3, "Parent 3")
Return table
End Function
Private Function GetChildTable() As DataTable
Dim table As New DataTable("Child")
table.PrimaryKey = {table.Columns.Add("ChildId", GetType(Integer))}
table.Columns.Add("ChildName", GetType(String))
table.Columns.Add("ParentId", GetType(Integer))
table.Rows.Add(1, "Child 1.1", 1)
table.Rows.Add(2, "Child 1.2", 1)
table.Rows.Add(3, "Child 1.3", 1)
table.Rows.Add(4, "Child 2.1", 2)
table.Rows.Add(5, "Child 2.2", 2)
table.Rows.Add(6, "Child 2.3", 2)
table.Rows.Add(7, "Child 3.1", 3)
table.Rows.Add(8, "Child 3.2", 3)
table.Rows.Add(9, "Child 3.3", 3)
Return table
End Function
If you do that, selecting a parent will automatically filter the children displayed for selection.
In case it's not obvious, you would get the parent and child tables by querying a database rather than building them manually, as I have done in my example.
As it stands now I can see two errors. First, do not concatenate strings to build sql command texts. Use always a parameterized query, specifying exactly what is the datatype of the parameter that you are passing. Second error is in the DisplayMember and ValueMember for the second combo. You don't have here a Bank_Id or a Bank name but the Branch name
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
' Always check if you have a valid selection to avoid NRE.
if ComboBox1.SelectedValue Is Nothing Then
Return
End if
' If bankid is an integer then convert to an integer
Dim bankId as Integer = Convert.ToInt32(ComboBox1.SelectedValue)
Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\New\Test.accdb")
Using branch_cmd As New OleDbCommand("Select Branch from Branch where Bank_id =#id", con)
cmd.Parameters.Add("#id", OleDbType.Integer).Value = bankId
Dim da As New OleDbDataAdapter(branch_cmd)
Dim dt As New DataTable
da.Fill(dt)
ComboBox2.DataSource = dt
ComboBox2.DisplayMember = "Branch"
ComboBox2.ValueMember = "Branch"
End Using
End Using
End Sub
According to your comment below you get a System.Data.DataRowView as the element contained in the SelectedValue. This should not happen with the code shown in your question, so perhaps, there is something different that creates the problem. (For example, if the datatable fields names don't match with the ValueMember/DisplayMember properties)
In any case, from a DataRowView, you should be able to get the integer in this way
Dim drv = DirectCast(ComboBox1.SelectedValue, DataRowView)
if drv IsNot Nothing then
Dim bankid = Convert.ToInt32(drv("ID"))
...
End if

Combobox - display 2 Datatable columns

I created multiple-column drop-down list for a combobox from a DataTable, and now I want to display both columns in it too. So far only 1 column is displayed (with DisplayMember property). So basically I want Autocomplete with both columns displayed in combobox. I would be satisfied with displaying 2nd column in Textbox next to combobox too, but It must work as Autocomplete (when selected index changes, display value changes too). I need this because both Datable values (Name & Surname) will be added in another DB table together, and for user to see both values in same place.
EDITED:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim SQL As String = "SELECT ID,Name ||' ' || Surname as FullName from MyTable"
Dim dtb As New DataTable()
dtb.Columns.Add("Name", System.Type.GetType("System.String"))
dtb.Columns.Add("Surname", System.Type.GetType("System.String"))
Using con As OracleConnection = New OracleConnection("Data Source=MyDB;User Id=Lucky;Password=MyPassword;")
Try
con.Open()
Using dad As New OracleDataAdapter(SQL, con)
dad.Fill(dtb)
End Using
Combobox1.DataSource = dtb
Combobox1.DisplayMember = "FullName"
Combobox1.ValueMember= "ID"
con.Close()
Catch ex As Exception
'MessageBox.Show(ex.Message)
Finally
con.Dispose()
End Try
End Using
End Sub
And drawing line between combobox columns:
Private Sub Combobox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles Combobox1.DrawItem
e.DrawBackground()
Dim drv As DataRowView = CType(Combobox1.Items(e.Index), DataRowView)
Dim id As String = drv("Name").ToString()
Dim name As String = drv("Surname").ToString()
Dim r1 As Rectangle = e.Bounds
r1.Width = r1.Width / 2
Using sb As SolidBrush = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(id, e.Font, sb, r1)
End Using
Using p As Pen = New Pen(Color.AliceBlue)
e.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom)
End Using
Dim r2 As Rectangle = e.Bounds
r2.X = e.Bounds.Width / 2
r2.Width = r2.Width / 2
Using sb As SolidBrush = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(name, e.Font, sb, r2)
End Using
End Sub
Any suggestions ? Thanks in advance !!
Checkout this answer: https://stackoverflow.com/a/5570901/6550457
It suggests:
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
Only way to achieve all is to add another combobox and bind him to same datatable. This way when you select item from combobox you see both values in them. Autocomplete works too. Doing everything in same combobox is not good, displayed text is too wide, doesn't look good.

Databinding one control per row in the same datasource

I've been trying to deal with a frustrating issue with regards to databinding in Winforms.
I have a data source, which is a DataTable in a DataSet. This DataTable has three rows. I have three CheckBox controls on my form, and a Button. I want to bind each CheckBox to one row in this data source, and have the data source reflect the value in the Checked property whenever the CheckBox is updated. I also want these changes to be correctly picked up by calls to HasChanges() and calls to GetChanges().
When the Button is clicked, EndCurrentEdit() is called and passed the data-source that was bound to, and the DataSet is checked for changes using the HasChanges() method.
However, in my attempts to do this, I encounter one of two scenarios after the call to EndCurrentEdit().
In the first scenario, only the first CheckBox has its changes detected. In the second scenario, all other CheckBoxes are updated to the value of the CheckBox that was last checked on the call to EndCurrentEdit().
In looking at the RowState values after the call to EndCurrentEdit(), in Scenario 1, only the first row ever has a state of Modified. In Scenario 2, only the third row ever has a state of Modified. For Scenario 2, it doesn't matter whether the user updated the third CheckBox or not.
To demonstrate my problem, I have created a simple example which demonstrates it.
It's a stock Windows form containing three CheckBox controls and a Button control, all with their default names.
Option Strict On
Option Explicit On
Public Class Form1
Public ds As DataSet
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
ds = New DataSet()
Dim dt As New DataTable()
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Selected", GetType(Boolean))
dt.Rows.Add(1, False)
dt.Rows.Add(2, False)
dt.Rows.Add(3, False)
dt.TableName = "Table1"
ds.Tables.Add(dt)
ds.AcceptChanges()
For i As Integer = 1 To 3
Dim bs As New BindingSource()
'After the call to Me.BindingContext(ds.Tables("Table1")).EndCurrentEdit() there are two scenarios:
'Scenario 1 - only changes to the first CheckBox are detected.
'Scenario 2 - when any CheckBox is checked, they all become checked.
'Uncomment the first and comment out the second to see Scenario 1.
'Uncomment the second and comment out the first to see Scenario 2.
'bs.DataSource = New DataView(ds.Tables("Table1")) 'Scenario 1
bs.DataSource = ds.Tables("Table1") 'Scenario 2
bs.Filter = "ID=" & i
Dim db As New Binding("Checked", bs, "Selected")
db.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
If i = 1
CheckBox1.DataBindings.Add(db)
ElseIf i = 2
CheckBox2.DataBindings.Add(db)
ElseIf i = 3
CheckBox3.DataBindings.Add(db)
End If
Next
End Sub
Private Sub Button1_Click( sender As Object, e As EventArgs) Handles Button1.Click
Me.BindingContext(ds.Tables("Table1")).EndCurrentEdit()
If ds.HasChanges()
MessageBox.Show("Number of rows changed: " & ds.GetChanges().Tables("Table1").Rows.Count)
ds.AcceptChanges()
End If
End Sub
End Class
I've done a lot of searching but haven't been able to work out what's happening, and am thus at a complete loss. It feels like what I'm trying to do is pretty simple, but I suspect that I must have misunderstood something somewhere or missed something important with regards to the binding process.
EDIT
It's already in the second paragraph, but just to make it clear, here is the basic outline of what I'm trying to do:
I have a DataSet containing a DataTable with values. For this example, there's two columns. ID could be any Integer, 'Selected' could be any Boolean value. None of these will ever be Nothing or DBNull.
I want to bind each row to ONE Checkbox control. One CheckBox per ID value. The Checked property should be bound to the Selected column in the DataTable.
When changes are made, and the user clicks the Button, I want to be able to tell what changes the user made, using the HasChanges() and GetChanges() methods of the DataSet (ie 2 rows updated if the user has changed the Checked property of two of the Checkbox controls).
EDIT 2
Thanks to #RezaAghaei I have come up with a solution. I have refined the code from my example of the problem, and made all controls generate based on the data (and position themselves accordingly) to make this example simple to copy-and-paste. Also, this uses a RowFilter on the DataView, rather than the Position property of the BindingSource.
Option Strict On
Option Explicit On
Public Class Form1
Public ds As DataSet
Private Sub Form1_Load( sender As Object, e As EventArgs) Handles MyBase.Load
ds = New DataSet()
Dim dt As New DataTable()
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Selected", GetType(Boolean))
dt.Rows.Add(1, False)
dt.Rows.Add(2, False)
dt.Rows.Add(3, False)
dt.TableName = "Table1"
ds.Tables.Add(dt)
ds.AcceptChanges()
AddHandler dt.ColumnChanged, Sub(sender2 As Object, e2 As DataColumnChangeEventArgs)
e2.Row.EndEdit()
End Sub
For i As Integer = 0 To dt.Rows.Count-1
Dim cb As New CheckBox() With {.Text = "CheckBox" & i+1, .Location = New Point(10, 25 * (i))}
Dim dv As New DataView(dt)
dv.RowFilter = "ID=" & DirectCast(dt.Rows(i)(0), Integer)
Dim bs As New BindingSource(dv, Nothing)
Dim db As New Binding("Checked", bs, "Selected")
db.DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged
cb.DataBindings.Add(db)
Me.Controls.Add(cb)
Next
Dim btn As New Button()
btn.Location = New Point(10, 30 * dt.Rows.Count)
btn.Text = "Submit"
AddHandler btn.Click, AddressOf Button1_Click
Me.Controls.Add(btn)
End Sub
Private Sub Button1_Click( sender As Object, e As EventArgs)
'Me.BindingContext(ds.Tables("Table1")).EndCurrentEdit() 'Doesn't cut the mustard!
If ds.HasChanges()
MessageBox.Show("Number of rows changed: " & ds.GetChanges().Tables("Table1").Rows.Count)
ds.AcceptChanges()
Else
MessageBox.Show("Number of rows changed: 0")
End If
End Sub
End Class
The key is the call to EndEdit() in the ColumnChanged event for DataTable (a general call to EndCurrentEdit() just doesn't appear to cut it), although one additional problem I encountered was that this code won't work if it's in the form's New() method, even if it's after the call to InitializeComponent(). I'm guessing this is because there's some initialisation that Winforms does after the call to New() which is necessary for data binding to work properly.
I hope this example saves others the time I spent looking into this.
Consider these corrections and the problem will be solved:
To bind a control to an specific index of a list, bind control to a binding source containing the list, and then set the Position of binding source to the specifixindex.
When adding data-binding to CheckBox controls, set update mode to OnPropertyChanged.
Handle CheckedChanged event of CheckBox controls and call Invalidate method of grid to show changes in grid.
In CheckedChanged also call EndEdit of the BindingSource which the CheckBox is bound to.
Note: Call EndEdit usnig BeginInvoke to let all processes of checking the checkbox (including updating data source) be completed.
C# Example
DataTable dt = new DataTable();
private void Form3_Load(object sender, EventArgs e)
{
dt.Columns.Add("Id");
dt.Columns.Add("Selected", typeof(bool));
dt.Rows.Add("1", true);
dt.Rows.Add("2", false);
dt.Rows.Add("3", true);
this.dataGridView1.DataSource = dt;
var chekBoxes = new CheckBox[] { checkBox1, checkBox2, checkBox3 };
for (int i = 0; i < dt.Rows.Count; i++)
{
var bs = new BindingSource(dt, null);
chekBoxes[i].DataBindings.Add("Checked", bs, "Selected",
true, DataSourceUpdateMode.OnPropertyChanged);
chekBoxes[i].CheckedChanged += (obj, arg) =>
{
this.dataGridView1.Invalidate();
var c = (CheckBox)obj;
var b = (BindingSource)(c.DataBindings[0].DataSource);
this.BeginInvoke(()=>{b.EndEdit();});
};
bs.Position = i;
}
}
VB Example
Dim dt As DataTable = New DataTable()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
dt.Columns.Add("Id")
dt.Columns.Add("Selected", GetType(Boolean))
dt.Rows.Add("1", True)
dt.Rows.Add("2", False)
dt.Rows.Add("3", True)
dt.AcceptChanges()
Me.DataGridView1.DataSource = dt
Dim chekBoxes = New CheckBox() {CheckBox1, CheckBox2, CheckBox3}
For i = 0 To dt.Rows.Count - 1
Dim bs = New BindingSource(dt, Nothing)
chekBoxes(i).DataBindings.Add("Checked", bs, "Selected", _
True, DataSourceUpdateMode.OnPropertyChanged)
AddHandler chekBoxes(i).CheckedChanged, _
Sub(obj, arg)
Me.DataGridView1.Invalidate()
Dim c = DirectCast(obj, CheckBox)
Dim b = DirectCast(c.DataBindings(0).DataSource, BindingSource)
Me.BeginInvoke(Sub() b.EndEdit())
End Sub
bs.Position = i
Next i
End Sub

Populating Listbox with Data from DataGridView

I've got 2 forms. Form A has a listbox and a Combobox. Form B has a DataGridView. In my A, my combobox is meant to represent groups for a task. e.g. Inbox, Important etc. So whenever I select an item from the comobobox e.g. I select the "Inbox" Item the DataGridViewer will sort all the rows which contain "Inbox" in one of the columns. This all works fine. I can view the sorted data.
To Load my data into the DataGridView I use:
Dim ds As DataSet
Dim dataset1 As New DataSet("datasetTasks")
Dim table1 As New DataTable("tableTask")
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ds = CreateDataset()
frm_Tasks.DataGridView1.DataSource = ds.Tables("tableTask")
LoadFromXMLfile("C:\Users\Beta4K\Documents\Tasks.FILE")
For Each dr As DataRow In ds.Tables(0).Rows
ListBox1.Items.Add(dr("TaskName").ToString())
Next
End Sub
Private Sub LoadFromXMLfile(filename As String)
If System.IO.File.Exists(filename) Then
Dim xmlSerializer As XmlSerializer = New XmlSerializer(ds.GetType)
Dim readStream As FileStream = New FileStream(filename, FileMode.Open)
ds = CType(xmlSerializer.Deserialize(readStream), DataSet)
readStream.Close()
frm_Tasks.DataGridView1.DataSource = ds.Tables("tableTask")
Else
MsgBox("file not found! add data and press save button first.", MsgBoxStyle.Exclamation, "")
End If
End Sub
Private Function CreateDataset() As DataSet
table1.Columns.Add("TaskID")
table1.Columns.Add("TaskName")
table1.Columns.Add("TaskMessage")
table1.Columns.Add("TaskDate")
table1.Columns.Add("TaskTime")
table1.Columns.Add("TaskGroup")
dataset1.Tables.Add(table1)
Return dataset1
End Function
Here's the code for my Combobox:
ListBox1.Items.Clear()
Dim dv As New DataView(ds.Tables("tableTask"))
dv.RowFilter = "TaskGroup = '" + ComboBox1.SelectedItem + "'"
frm_Tasks.DataGridView1.DataSource = dv.ToTable("tableTask")
For Each dr As DataRow In ds.Tables(0).Rows
ListBox1.Items.Add(dr("TaskName").ToString())
Next
What this is meant to do is that it clears the listbox, and then reloads the items into the listbox from reading all the data in the DataGridViewer. Since it's already sorted all it has to do is add the items but it doesn't. Instead it just adds all the items regardless of the filter.
Can someone help me.
You loop over the datatable while you need to loop over the dataview
For Each dr As DataRowView In dv
ListBox1.Items.Add(dr("TaskName").ToString())
Next

How can I have 2 gridviews in one form with same dataset, but other population?

Here you see my code of a form with 2 gridviews. Both have the same dataset, bindingsource. The dataset which is made out of a datasource, has 2 different sql queries.
filld() and fillauswahl() filld shows in the gridview a "select distinct" query.
When the user hits the button1, the selected item from that gridview is saved in "verzeichnis1" this var gets pasted to fillauswahl() which is
select* from mytable where columnx = verzeichnis1
The problem I have is that both gridviews get filled during formload with filld() and by clicking the button with fillverzeichnis() i dont know how to seperate that!? i guess it´s very easy. Cheers and thanks
Public Class Importverzeichnis
Public verzeichnis1 As String
Private Sub Importverzeichnis_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Me.SKM2TableAdapter.Filld(Me.SLXADRIUMDEVDataSet.SKM2)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each cell As DataGridViewCell In DataGridView1.SelectedCells
verzeichnis1 = cell.Value
Next
Me.SKM2TableAdapter.Fillauswahl(Me.SLXADRIUMDEVDataSet.SKM2, verzeichnis1)
End Sub
End Class
Edit: I created a new connection a new datset and new dataadapter and now it works:
Dim connectionString As String = My.Settings.SLXADRIUMDEVConnectionString
Dim sql As String = "SELECT * FROM SKM2 where
Benutzerdefiniert_10 ='" & verzeichnis1 & "' "
Dim connection As New SqlConnection(connectionString)
Dim dataadapter As New SqlDataAdapter(sql, connection)
Dim ds As New DataSet()
connection.Open()
dataadapter.Fill(ds, "verzeichnis")
connection.Close()
datagridview2.DataSource = ds
datagridview2.DataMember = "verzeichnis"
but I would be more happy if can use my first dataset and my first adapter. If anyobdy knows how I can do this, I would be happy for the answer
To me the best way would be to just pull down the data as a your normal select statement and then filter the data in your code-behind. By populating a dataset with the same data twice your just making the traffic from the database slower. However, if you wish to keep your current dataset, I would assume that there are two tables in it, one for each select. If that is the case then change:
datagridview2.DataSource = ds
to:
datagridview2.DataSource = ds.Tables(1) 'assumes the second table is used for this datasource