How to make data from datasource show in datagrid in WPF?

Zarif J Nafee 0 Reputation points
2023-07-11T14:32:36.3766667+00:00

Hello, I am fairly new to using wpf and I working with visual studio and sql server. I wanted to have data from a sql sevrer appear on a application to be edited such as crud operations and saved. I am making the applicaiton using WPF. The issue I am having is having the data appear on the application. I connected the server to my project and also connected the data source I want to use. I am using a custom database for testing.

What I have done so far:
I used the following pages https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable?view=netframework-4.8 and https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datagrid?view=netframework-4.8, to make a instance of a DataTable and then bind a DataGrid to the table, but it didn't work. When I was implementing this method I only got a empty data grid with no rows or data appearing. The other approach I tried after was making a DataGrid and dragging the DataTable from the Data Source into the the grid. This gave me the fields of my table but no actual data inside it. When I run my application The datagrid doesn't have any information but I can write down information for the duration of the apploicaiton lifetime.

I want the data in the data table to appear when I open the applicaiton so I can edit it. This is what my XAML code looks like right now(I didn't include the code for cosmetic portions of the applicaiton):

   <Page.Resources>
        <CAMUI_Protoype_v2:TestDB1DataSet x:Key="testDB1DataSet"/>
        <CollectionViewSource x:Key="aTableViewSource" Source="{Binding aTable, Source={StaticResource testDB1DataSet}}"/>
    </Page.Resources>


    <Grid Background="AliceBlue" DataContext="{StaticResource aTableViewSource}">
        
        <DataGrid Grid.Column="1" Grid.Row="1" ItemsSource="{Binding}" Margin=" 10 10 10 10 "/>


    </Grid>

</Page>

Developer technologies Windows Presentation Foundation
Developer technologies XAML
SQL Server Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zarif J Nafee 0 Reputation points
    2023-07-11T19:52:53.51+00:00

    I found a solution to my problem, basically following th einstruction form this article. https://www.c-sharpcorner.com/UploadFile/deepak.sharma00/data-binding-in-wpf-datagrid-control-using-sql-server-databa/

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.