ASP.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a ASP.NET Expert!

Join 300,354 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 1,825 people online right now. Registration is fast and FREE... Join Now!




DataGrid in WPF Web application shows nothing

 

DataGrid in WPF Web application shows nothing

dmarinov

25 Jun, 2009 - 02:19 AM
Post #1

New D.I.C Head
*

Joined: 25 Jun, 2009
Posts: 2

Hello everybody!
I do my first steps in C# and WPF and I try to make a simple web WPF application to display the rows in my table.
The following is my code. I use a Firebird ADO NET client, but I guess it is the same with other clients. When I debug the application, I see the dataset is loaded with my records. I see the number of records on the screen too. But I still can't see it in the DataGrid. Do I miss something blink.gif ?

CODE


  private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            FbConnection MyConnection = new FbConnection("User=SYSDBA;Password=masterkey;Database=c:\\temp\\Database.FDB;DataSource=localhost; Port=3050;Dialect=3; Charset=NONE;Role=;Connection lifetime=15;Pooling=true; MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0;");
            MyConnection.Open();
            DataSet employee = new DataSet();
            FbDataAdapter da = new FbDataAdapter("select * from EMPLOYEE ", MyConnection);
        
            da.Fill(employee,"EMLOYEE");
            int recordnum = employee .Tables[0].Rows.Count;
            Label1.Text =" EMLOYEE RecordCount=" + recordnum.ToString();          
            dataGrid1.DataContext = employee.Tables[0];      
        }

and the XAML code:
CODE

<Page x:Class="WpfBrowserApplication2.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Page1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" Loaded="Page_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <my:DataGrid  Name="dataGrid1" >
          
        </my:DataGrid>
        <TextBlock Name="Label1" Text="Test" Grid.Row="1"/>
    </Grid>
</Page>


This post has been edited by dmarinov: 25 Jun, 2009 - 02:44 AM

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: DataGrid In WPF Web Application Shows Nothing

25 Jun, 2009 - 08:03 AM
Post #2

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,711



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Moved to ASP.NET smile.gif
User is offlineProfile CardPM
+Quote Post

dmarinov

RE: DataGrid In WPF Web Application Shows Nothing

26 Jun, 2009 - 01:39 AM
Post #3

New D.I.C Head
*

Joined: 25 Jun, 2009
Posts: 2

Hi Guys and Gals!

I answered my question myself. I share it with you. May be would be of interest to someone.
It seems setting the DataContext does nothing. I've fixed it. Now it works fine.

Here is the code:
CODE

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;
using System.Data.Metadata.Edm;
using Microsoft.Windows.Controls;
using FirebirdSql.Data.FirebirdClient;

namespace WpfBrowserApplication2
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
        }
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            FbConnection MyConnection = new FbConnection("User=SYSDBA;Password=masterkey;Database=c:\\temp\\MyData.FDB;DataSource=localhost; Port=3050;Dialect=3; Charset=NONE;Role=;Connection lifetime=15;Pooling=true; MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0;");
            MyConnection.Open();
            DataSet employee = new DataSet();
            FbDataAdapter da = new FbDataAdapter("select * from EMPLOYEE ", MyConnection);        
            da.Fill(stoki, "EMPLOYEE");
            int recordnum = employee.Tables[0].Rows.Count;
            Label1.Text =" EMPLOYEE RecordCount=" + recordnum.ToString();          
            Binding MyBinding = new Binding();
            MyBinding.Source = employee.Tables[0];
            dataGrid1.ItemsSource = employee.Tables[0].Rows;
            dataGrid1.SetBinding(DataGrid.ItemsSourceProperty, MyBinding);        
        }
    }
}

In order to access the DataGrid class from code you should add a refference in the project to WPF Toolkit dll which is (usually) in the Program Files\WPF Toolkit directory.

and the XAML part

CODE

<Page x:Class="WpfBrowserApplication2.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
    Loaded="Page_Loaded" Title="Page1"  >    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>          
        </Grid.RowDefinitions>
        <my:DataGrid  Name="dataGrid1" CanUserAddRows="True" CanUserDeleteRows="True" >            
        </my:DataGrid>
        <TextBlock Name="Label1" Text="Test" Grid.Row="1"/>      
    </Grid>
</Page>


In fact the binding from XAML could be easier - I can add
CODE

ItemsSource="{Binding EMPLOYEE}"

to the DataGrid.
Here are some links that I used to resolve my issue:
1.http://windowsclient.net/wpf/wpf35/wpf-35s...alkthrough.aspx
2.http://www.codeproject.com/KB/WPF/WPFDataG...es.aspx#dataset
3.http://dedjo.blogspot.com/2007/03/creating...-from-code.html

This post has been edited by dmarinov: 26 Jun, 2009 - 02:00 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 07:02PM

Live ASP.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

ASP.NET Tutorials

Reference Sheets

ASP.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month