Home Forums WPF controls Xceed DataGrid for WPF Data Binding for WPF DataGrid..

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #25665 |

    Good Day,

    I need to find out if there is any way available so that we can bind the WPF DataGrid with a datatable that doesn’t exist in App.Xaml.cs class….

    e.g i have another page called Page1.Xaml.cs… It is having code for opening the database and filling the datatable… How can i bound the DataGrid with this datatable…

    I am not able to find this in samples coz all the samples are talking about binding with “App.Current” datasource…

    If anybody has any idea about this, i will deeply appreciate if he can provide me a piece of sample code.

    thanks,

    tayyab…

    Imported from legacy forums. Posted by tayyabhussain (had 4893 views)

    User (Old forums)
    Member
    Post count: 23064

    heres how i did it in vb.net, i hope this helps….there was no examples that i could find on how to populate a dataset and bind it to the datagrid…..

    Public WithEvents t_repos As System.Data.DataTable
    Public adapter As New System.Data.SqlClient.SqlDataAdapter()
    Public dataSet As New System.Data.DataSet()

    Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

    Dim connString As String = “Connection string”
    Dim conn As New System.Data.SqlClient.SqlConnection(connString)
    Dim command As New System.Data.SqlClient.SqlCommand(“SELECT * FROM collectionrepos”, conn)

    adapter.SelectCommand = command

    Dim cBuilder As New System.Data.SqlClient.SqlCommandBuilder(adapter)
    cBuilder.RefreshSchema()
    cBuilder.SetAllValues = False

    adapter.UpdateCommand = cBuilder.GetUpdateCommand
    adapter.InsertCommand = cBuilder.GetInsertCommand
    adapter.DeleteCommand = cBuilder.GetDeleteCommand

    t_repos = dataSet.Tables(0)
    ReposGrid.Items.Clear()
    ReposGrid.ReadOnly = False

    Dim dgvs As New Xceed.Wpf.DataGrid.DataGridCollectionViewSource
    dgvs.Source = t_repos

    ReposGrid.ItemsSource = dgvs.View

    conn.Close()

    Imported from legacy forums. Posted by Edward (had 3191 views)

    User (Old forums)
    Member
    Post count: 23064

    Also, I used this code to do the row updating

    Private Sub t_repos_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles t_repos.RowChanged
    If Not (e.Action = System.Data.DataRowAction.Nothing Or _
    e.Action = System.Data.DataRowAction.Commit) Then
    If Not e.Row.HasErrors Then
    adapter.Update(t_repos)
    End If
    End If
    End Sub

    Imported from legacy forums. Posted by Edward (had 634 views)

    User (Old forums)
    Member
    Post count: 23064

    And the XAML code

    <Window x:Class=”Window1″
    xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
    xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
    xmlns:local=”clr-namespace:TestApplication.Validators”
    xmlns:my=”http://schemas.xceed.com/wpf/xaml/datagrid&#8221;
    Title=”Window1″ Height=”300″ Width=”300″ Name=”Window1″>
    <Grid>
    <my:DataGridControl Margin=”0,0,0,0″ Name=”ReposGrid” NavigationBehavior=”RowOrCell” ValidationMode=”CellEndingEdit”>
    <my:DataGridControl.View>
    <my:TableView>
    <my:TableView.FixedHeaders>
    <DataTemplate>
    <my:InsertionRow/>
    </DataTemplate>
    </my:TableView.FixedHeaders>
    </my:TableView>
    </my:DataGridControl.View>
    </my:DataGridControl>
    </Grid>
    </Window>

    Imported from legacy forums. Posted by Edward (had 677 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks! I was looking for the same solution and it worked like a charm.

    Imported from legacy forums. Posted by Ken (had 4202 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks, for your example. I have got it to work great for updates and inserts but I can’t get it to work for deletes.

    I am using an example from this forum to delete rows like this:

     Private Sub DeleteSelectedRows(ByVal sender As Object, ByVal e As KeyEventArgs)
            Dim grid As DataGridControl = TryCast(sender, DataGridControl)
            If Not grid Is Nothing Then
                If e.Key = Key.Delete Then
                    If grid.SelectedItems.Count > 0 Then
                        ‘ we keep a copy of the selected items because once the first item is deleted,
                        ‘ the selected items are reset to 0.
                        Dim items As ArrayList = New ArrayList(grid.SelectedItems)
                        Dim dataView As DataView = TryCast((CType(grid.ItemsSource,  _
                                       DataGridCollectionView)).SourceCollection, DataView)
                        For i As Integer = items.Count – 1 To 0 Step -1
                            dataView.Table.Rows.Remove(CType(items(i), System.Data.DataRow))
                        Next i
                        grid.CurrentItem = grid.Items(0)
                    End If
                End If
            End If
        End Sub

     

     This works great in removing the rows from the grid but it doesn’t delete from the underlying database. In the same way as I am using your example to update and add records I added this:

      Private Sub dtTimePeriods_RowDeleted(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles dtTimePeriods.RowDeleted
            adapter.Delete(dtTimePeriods)
        End Sub

     But I am getting an error on the adapter.Delete command saying that:”Public member ‘Delete’ on type ‘SqlCeDataAdapter’ not found.”

    I do have a delete command on the adapter so it seems like the adapter doesn’t have a reference to the the right object?

     

    Any ideas?

    Thanks,

    Andreas

     

    Imported from legacy forums. Posted by Andreas (had 787 views)

    User (Old forums)
    Member
    Post count: 23064

    I think you need an Update line in there.

      Private Sub dtTimePeriods_RowDeleted(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles dtTimePeriods.RowDeleted
            adapter.Delete(dtTimePeriods)
            adapter.Update(dtTimePeriods)
        End Sub

    Imported from legacy forums. Posted by Edward (had 547 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Edward,

    Thanks for the suggestion but it will never get to the update line as it falls over on the delete line already. I will keep this in mind though once I get past the delete hurdle.

    Any other ideas?

    Thanks,

    Andreas

    Imported from legacy forums. Posted by Andreas (had 540 views)

    User (Old forums)
    Member
    Post count: 23064

    Actually all I needed was the Update command.

    I have to get my head around this command builder stuff. I have always been hardcoding my SQLs this far.

    Thanks heaps!

    /Andreas

    Imported from legacy forums. Posted by Andreas (had 1445 views)

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.