I have a listbox that is loaded from a SQL table. Here is the code:
private void DisplaySubjects()
{
SqlConnection conn = new SqlConnection(zSQL_Variables.csNotebook);
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT DISTINCT Subject,ID from Subjects", conn);
adapter.Fill(ds);
this.lbxSubjects.DataSource = ds.Tables[0];
this.lbxSubjects.DisplayMember = "Subject";
this.lbxSubjects.ValueMember = "Subject";
lblSubjectsCount.Text = "[" + lbxSubjects.Items.Count.ToString() + "]";
}
The table loads correctly. I have set up a drag and drop operation which takes an item from the listbox and drops it in a textbox. Instead of getting the selected field from the listbox I get "System.Data.DataRowView" instead. Here is my code for the Drag and Drop operations.
private void txtFindSubject_DragDrop(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void txtFindSubject_DragEnter(object sender, DragEventArgs e)
{
txtFindSubject.Text = (string)e.Data.GetData(DataFormats.Text);
}
private void lbxSubjects_MouseDown(object sender, MouseEventArgs e)
{
lbxSubjects.DoDragDrop(lbxSubjects.SelectedItem.ToString(), DragDropEffects.Copy);
}
What am I doing wrong?