Hello,
I have a problem that eats my free time.
I have a SQLite database and a textbox on the form. I do not know how to make textbox show data from table.
I figured a way to display a data on listview, and this is how I do it.
//my class that I use to conenct to SQLite db
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;
using System.Data;
namespace Firma
{
public class konekcija // Glavno konektovanje u bazu
{
public DataTable datatable()
{
SQLiteConnection conn = new SQLiteConnection(@"data source=...\Debug\firma.s3db");
conn.Open();
SQLiteCommand cmd = new SQLiteCommand(" Select * from radnici", conn);
//DataSet1 dt = new DataSet1();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable tbl = new DataTable();
da.Fill(tbl);
conn.Close();
return tbl;
}
public void noviunos(string ime, string prezime) //Novi unos u bazu
{
SQLiteConnection conn = new SQLiteConnection(@"data source=...\Debug\firma.s3db");
conn.Open();
SQLiteCommand comm = new SQLiteCommand("insert into korisnici(ime,prezime) values(@ime,@prezime)", conn);
comm.Parameters.AddWithValue("@ime", ime);
comm.Parameters.AddWithValue("@prezime", prezime);
comm.ExecuteNonQuery();
conn.Close();
}
public void editunosa(string id, string ime, string prezime) //Ispravak postojeceg unosa
{
SQLiteConnection conn = new SQLiteConnection(@"data source=...\Debug\firma.s3db");
conn.Open();
SQLiteCommand comm = new SQLiteCommand("update korisnici set ime=@ime,prezime=@prezime where id=@id", conn);
comm.Parameters.AddWithValue("@id", id);
comm.Parameters.AddWithValue("@ime", ime);
comm.Parameters.AddWithValue("@prezime", prezime);
comm.ExecuteNonQuery();
conn.Close();
}
public void brisanjeunosa(string id, string ime, string prezime) //Brisanje postojeceg unosa
{
SQLiteConnection conn = new SQLiteConnection(@"data source=...\Debug\firma.s3db");
conn.Open();
SQLiteCommand comm = new SQLiteCommand("delete from korisnici where id=@id", conn);
comm.Parameters.AddWithValue("@id", id);
comm.Parameters.AddWithValue("@ime", ime);
comm.Parameters.AddWithValue("@prezime", prezime);
comm.ExecuteNonQuery();
conn.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SQLite;
using System.Data;
namespace Firma
{
public class konekcija // Glavno konektovanje u bazu
{
public DataTable datatable()
{
SQLiteConnection conn = new SQLiteConnection(@"data source=...\Debug\firma.s3db");
conn.Open();
SQLiteCommand cmd = new SQLiteCommand(" Select * from radnici", conn);
//DataSet1 dt = new DataSet1();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataTable tbl = new DataTable();
da.Fill(tbl);
conn.Close();
return tbl;
}
public void noviunos(string ime, string prezime) //Novi unos u bazu
{
SQLiteConnection conn = new SQLiteConnection(@"data source=...\Debug\firma.s3db");
conn.Open();
SQLiteCommand comm = new SQLiteCommand("insert into korisnici(ime,prezime) values(@ime,@prezime)", conn);
comm.Parameters.AddWithValue("@ime", ime);
comm.Parameters.AddWithValue("@prezime", prezime);
comm.ExecuteNonQuery();
conn.Close();
}
public void editunosa(string id, string ime, string prezime) //Ispravak postojeceg unosa
{
SQLiteConnection conn = new SQLiteConnection(@"data source=...\Debug\firma.s3db");
conn.Open();
SQLiteCommand comm = new SQLiteCommand("update korisnici set ime=@ime,prezime=@prezime where id=@id", conn);
comm.Parameters.AddWithValue("@id", id);
comm.Parameters.AddWithValue("@ime", ime);
comm.Parameters.AddWithValue("@prezime", prezime);
comm.ExecuteNonQuery();
conn.Close();
}
public void brisanjeunosa(string id, string ime, string prezime) //Brisanje postojeceg unosa
{
SQLiteConnection conn = new SQLiteConnection(@"data source=...\Debug\firma.s3db");
conn.Open();
SQLiteCommand comm = new SQLiteCommand("delete from korisnici where id=@id", conn);
comm.Parameters.AddWithValue("@id", id);
comm.Parameters.AddWithValue("@ime", ime);
comm.Parameters.AddWithValue("@prezime", prezime);
comm.ExecuteNonQuery();
conn.Close();
}
}
}
and this is my listview XAML.
<Grid>
<ListView ItemsSource="{Binding}" Name="listView1" Margin="25.722,32.867,51.444,155.761">
<ListView.Effect>
<DropShadowEffect/>
</ListView.Effect>
<ListView.View>
<GridView>
<GridViewColumn Header="Member" DisplayMemberBinding="{Binding Path=ID}"></GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Ime}"></GridViewColumn>
<GridViewColumn Header="Lastname " DisplayMemberBinding="{Binding Path=Prezime}"></GridViewColumn>
<GridViewColumn Header="Position " DisplayMemberBinding="{Binding Path=Pozicija}"></GridViewColumn>
<GridViewColumn Header="Work Duration " DisplayMemberBinding="{Binding Path=RadniStaz}"></GridViewColumn>
<GridViewColumn Header="Salary " DisplayMemberBinding="{Binding Path=Plata}"></GridViewColumn>
<GridViewColumn Header="Eq. List " DisplayMemberBinding="{Binding Path=Zaduzenje}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
<ListView ItemsSource="{Binding}" Name="listView1" Margin="25.722,32.867,51.444,155.761">
<ListView.Effect>
<DropShadowEffect/>
</ListView.Effect>
<ListView.View>
<GridView>
<GridViewColumn Header="Member" DisplayMemberBinding="{Binding Path=ID}"></GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Ime}"></GridViewColumn>
<GridViewColumn Header="Lastname " DisplayMemberBinding="{Binding Path=Prezime}"></GridViewColumn>
<GridViewColumn Header="Position " DisplayMemberBinding="{Binding Path=Pozicija}"></GridViewColumn>
<GridViewColumn Header="Work Duration " DisplayMemberBinding="{Binding Path=RadniStaz}"></GridViewColumn>
<GridViewColumn Header="Salary " DisplayMemberBinding="{Binding Path=Plata}"></GridViewColumn>
<GridViewColumn Header="Eq. List " DisplayMemberBinding="{Binding Path=Zaduzenje}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
And this is how I bind the listview to database in code.
konekcija etoga = new konekcija();//Object instantiated from konekcija class
private void Window_Loaded(object sender, RoutedEventArgs e)
{
listView1.DataContext = etoga.datatable();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
listView1.DataContext = etoga.datatable();
}
This works fine, but how do I do the same thing with textbox?
Please help :)
05/17/2010 - 14:54
Here's a tutorial that explains how to place textboxes inside the ListView to edit the contents. Does this help?
05/17/2010 - 15:11
Before you posted your reply, I have figured a way to do it.
I am going to read the tutorial and try to implement it.
I do not feel comfortable with DataContext solution.
It seemed that I need to use DataContext for that thing
DataContext = etoga.datatable();
And this is new xaml code:
<TextBox Text="{Binding Ime}" Height="23" HorizontalAlignment="Left" Margin="110,111,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding Prezime}" Height="23" HorizontalAlignment="Left" Margin="110,153,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding Pozicija}" Height="23" HorizontalAlignment="Left" Margin="110,194,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
05/17/2010 - 15:29
I have checked the tutorial you provided, and it is extremely difficult for the simple thing that i need. I appreciate your time in making it.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.