i am making simple wpf project, have to connect to DB and bind it to DataGrid. The thing is: i can display data from DB, but i cannot update it back. I've been looking for answers, tried 100 different things, i do not know what can it be.
My xaml data grid:
<Grid>
<DataGrid x:Name="dataGrid1" ItemsSource="{Binding Path=GridData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="0" VerticalAlignment="Stretch"/>
</Grid>
My View
class View
{
SqlConnection con;
DataTable dt = new DataTable("emp");
SqlDataAdapter da;
SqlCommandBuilder builder;
public DataView GridData
{
get
{
con = new SqlConnection(@"Server=MAXMAD-\R;Initial Catalog=WOC;Integrated Security=True");
try
{
con.Open();
string Get_Data = "SELECT * FROM Player";
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = Get_Data;
da = new SqlDataAdapter(cmd);
builder = new SqlCommandBuilder(da);
da.Fill(dt);
}
catch (SqlException ex)
{
MessageBox.Show("Error occured, can not connect to db!");
return null;
}
return dt.DefaultView;
}
set
{
da.Update(dt);
}
}
}
code behind
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
View myView = new View();
DataContext = myView;
}
}