TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Armins
NA
1
2.1k
How to drag and drop listbox items WPF and SQLite or SQL
Oct 31 2013 1:00 PM
Here i created very easy code just for easy way to understand question.
SQLite:
Table = MyTable
Fields:
ID = primary key
Name = varchar(200)
current_ID = FLOAT
Wpf code:
<Grid>
<ListBox x:Name="listBox" AllowDrop="True" ItemsSource="{Binding con}" DisplayMemberPath="Name" Drop="listBoxl_Drop" PreviewMouseLeftButtonDown="listBox_PreviewMouseLeftButtonDown" />
</Grid>
C# code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
listBox.DataContext = Show_Data();
}
private void listBox_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
ListBox parent = (ListBox)sender;
listBox = parent;
object data = GetDataFromListBox(listBox, e.GetPosition(parent));
if (listBox.SelectedIndex >= 0)
{
DataRowView row = (DataRowView)listBox.SelectedItem;
string id = row["current_ID"].ToString();
var float_db = Return_Float(id);
}
if (data != null)
{
DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
}
}
public float fload_db { get; set; }
public float Return_Float(string id)
{
using (SQLiteConnection sqCon = new SQLiteConnection(con_String))
{
try
{
string CommandText = "select * from MyTable Where ID=" + id;
SQLiteCommand cmd = new SQLiteCommand(CommandText, sqCon);
SQLiteDataAdapter adapter = new SQLiteDataAdapter();
sqCon.Open();
adapter.SelectCommand = cmd;
SQLiteDataReader sdr = cmd.ExecuteReader();
fload_db = Convert.ToSingle(sdr["current_ID"].ToString());
}
catch { }
sqCon.Close();
return fload_db;
}
}
private void listBox_Drop(object sender, DragEventArgs e)
{
// Here i don't now how to Get target ListBoxItem "current_ID" from SQLite !!!
}
private static object GetDataFromListBox(ListBox source, Point point)
{
UIElement element = source.InputHitTest(point) as UIElement;
if (element != null)
{
object data = DependencyProperty.UnsetValue;
while (data == DependencyProperty.UnsetValue)
{
data = source.ItemContainerGenerator.ItemFromContainer(element);
if (data == DependencyProperty.UnsetValue)
{
element = VisualTreeHelper.GetParent(element) as UIElement;
}
if (element == source)
{
return null;
}
}
if (data != DependencyProperty.UnsetValue)
{
return data;
}
}
return null;
}
public DataSet Show_Data()
{
DataSet dtSet = new DataSet();
using (SQLiteConnection sqCon = new SQLiteConnection(con_String))
{
sqCon.Open();
try
{
string CommandText = "select * from MyTable order by current_ID";
SQLiteCommand cmd = new SQLiteCommand(CommandText, sqCon);
SQLiteDataAdapter adapter = new SQLiteDataAdapter();
adapter.SelectCommand = cmd;
adapter.Fill(dtSet, "con");
}
catch
{
}
sqCon.Close();
}
return dtSet;
}
Thanks!
Reply
Answers (
0
)
Listen to Keyboard input inside usercontrol
Getting HTML data from webbrowser.document property