Solved

C# WPF DataGrid - Populate from database

Posted on 2013-02-03
6
1,798 Views
Last Modified: 2013-02-05
Hi,

I am building a application in Visual Studio 2012 using the .NET 4.5 framework. I have two issues currently and think they are probably related.

Firstly I have DataGrid named "dgSecurities". However when I go to the cs file, it does not recognise the name and I can't see how to reference the datagrid?

<!-- Parent Grid that contains all other grids -->
        <Grid Name="ParentGrid" Grid.IsSharedSizeScope="True">
            <Grid.RowDefinitions>
                <RowDefinition Height="12*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>                
            </Grid.RowDefinitions>                        
            <Button Grid.Column="0" Grid.Row="1">Second</Button>

            <!-- Simualtor Securities Panel -->
            <Grid Grid.Column="0" Grid.Row="0" Name="paneSimSec" Visibility="Visible">
                <DataGrid Name="dgSecurities" AutoGenerateColumns="True">


                </DataGrid>
            </Grid>
</Grid>

Open in new window


I also have a ribbon button which I want to add a Click event. But have the following error message. "Window1 does not contain a definition for buttSimSec_Click and no extension method buttSimSec_Click accepting a first argument of type Window1 could be found (are you missing a using directive or an assembly reference"

<RibbonGroup x:Name="grpSecurities" Header="Securities">
                    <RibbonButton x:Name="buttSimSec" Label="Select Securities" Click="buttSimSec_Click"></RibbonButton>
                </RibbonGroup>

Open in new window


private void buttSimSec_Click(object sender, RoutedEventArgs e)
        {
            // show the simulator pane and hide the rest
            
        }

Open in new window


Any help would be great!

M
0
Comment
Question by:mcs26
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:dj_alik
ID: 38850792
1. change  Name="dgSecurities" to x:Name ="dgSecurities"
2.use  Command="" in  RibbonButton
more: http://social.msdn.microsoft.com/Forums/en/wpf/thread/81ac7a69-86c7-492c-abe6-31b7dd3b214e
0
 

Author Comment

by:mcs26
ID: 38851649
Hi dj_alik,

Thanks for the reply. I tried x:Name originally it still made no difference though?

I'll try the Command="". However why would Click not work? I though this was an event asscociated with the wpf button?

Cheers,
M
0
 
LVL 18

Expert Comment

by:dj_alik
ID: 38852049
please post all your project code  and i tried to help you.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:mcs26
ID: 38852438
Ok great thanks! Its pretty bad but here it is,

<RibbonWindow x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    Title="My First Ribbon Form" ResizeMode="CanResizeWithGrip" WindowStartupLocation="CenterScreen" WindowState="Maximized" 
    Height="600" Width="800" MinHeight="300" MinWidth="400" Background="LightSteelBlue">

    <DockPanel Name="dockMain">
        <!--<DockPanel.Background>
            <ImageBrush ImageSource="C:\Users\Mark\Documents\Visual Studio 2012\Projects\SimulatorFX_WPF\Images\fxChart9.jpg" Opacity="0.85"></ImageBrush>
        </DockPanel.Background>-->
        <Ribbon DockPanel.Dock="Top" Title="My First Ribbon Form" x:Name="mainRibbon">
            
            <!--Quick Access Buttons -->
            <Ribbon.QuickAccessToolBar>
                <RibbonQuickAccessToolBar>
                    <RibbonButton x:Name="qAccImport"></RibbonButton>
                    <RibbonButton x:Name="qAccExport"></RibbonButton>
                </RibbonQuickAccessToolBar>
            </Ribbon.QuickAccessToolBar>
            
            <!-- Application Menu Bar -->
            <Ribbon.ApplicationMenu>
                <RibbonApplicationMenu Label="FX">
                    <RibbonApplicationMenuItem Header="Import"></RibbonApplicationMenuItem>
                    <RibbonApplicationMenuItem Header="Export"></RibbonApplicationMenuItem>
                    <RibbonApplicationMenuItem Header="Charts"></RibbonApplicationMenuItem>
                    <RibbonApplicationMenuItem Header="Exit"></RibbonApplicationMenuItem>
                </RibbonApplicationMenu>
            </Ribbon.ApplicationMenu>
            
            <!-- Home Tab -->
            <RibbonTab x:Name="Home" Header="Home"/>
            
            <!-- Simulator Tab -->
            <RibbonTab x:Name="Simulator" Header="Simulator">            
                <RibbonGroup x:Name="grpSecurities" Header="Securities">
                    <RibbonButton x:Name="buttSimSec" Label="Select Securities" Click="buttSimSec_Click"></RibbonButton>
                </RibbonGroup>
                <RibbonGroup x:Name="grpModels" Header="Models">
                    <RibbonButton x:Name="buttModels" Label="View Models"></RibbonButton>
                </RibbonGroup>
                <RibbonGroup x:Name="grpMoneyManagement" Header="Money Mangement and Position Sizing">
                    <RibbonButton x:Name="buttMM" Label="Money Management"></RibbonButton>
                    <RibbonButton x:Name="buttPos" Label="Position Sizing"></RibbonButton>
                </RibbonGroup>
                <RibbonGroup x:Name="grpStops" Header="Stops and Exits">
                    <RibbonButton x:Name="buttStops" Label="Stops And Exits"></RibbonButton>                        
                </RibbonGroup>
                <RibbonGroup x:Name="grpRun" Header="Run Simulation">
                    <RibbonButton x:Name="buttRunSim" Label="Run Simulation"></RibbonButton>
                </RibbonGroup>
                <RibbonGroup x:Name="grpImportExport" Header="Import or Export Resultss">
                    <RibbonButton x:Name="buttSimImport" Label="Import"></RibbonButton>
                    <RibbonButton x:Name="buttSimExport" Label="Export"></RibbonButton>
                </RibbonGroup>
                <RibbonGroup x:Name="grpUserPreferences" Header="Select Preferences">
                </RibbonGroup>
            </RibbonTab>                                                                     
            
            <!-- Chart tab -->
            <RibbonTab x:Name="Charts" Header="Charts"/>
            
            <!-- Repot tb -->
            <RibbonTab x:Name="Reports" Header="Reports"/>
        </Ribbon>
        
        <!-- Parent Grid that contains all other grids -->
        <Grid Name="ParentGrid" Grid.IsSharedSizeScope="True">
            <Grid.RowDefinitions>
                <RowDefinition Height="12*"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>                
            </Grid.RowDefinitions>                        
            <Button Grid.Column="0" Grid.Row="1">Second</Button>

            <!-- Simualtor Securities Panel -->
            <Grid Grid.Column="0" Grid.Row="0" Name="paneSimSec" Visibility="Visible">
                <DataGrid Name="dgSecurities" AutoGenerateColumns="True">


                </DataGrid>
            </Grid>
            <!-- Simualtor Model Panel -->
            <Grid Grid.Column="0" Grid.Row="0" Name="paneSimModels" Visibility="Collapsed">


            </Grid>
            <!-- Simualtor Money Mangment and Position Sizing Panel -->
            <Grid Grid.Column="0" Grid.Row="0" Name="paneSimMM" Visibility="Collapsed">


            </Grid>
            <!-- Simualtor Exits Panel -->
            <Grid Grid.Column="0" Grid.Row="0" Name="paneSimExits" Visibility="Collapsed">


            </Grid>
            <!-- Simualtor Import Export Panel -->
            <Grid Grid.Column="0" Grid.Row="0" Name="paneSimImportExport" Visibility="Collapsed">


            </Grid>
            <!-- Simualtor User Perf Panel -->
            <Grid Grid.Column="0" Grid.Row="0" Name="paneSimPref" Visibility="Collapsed">


            </Grid>

        </Grid>        

        
    </DockPanel>
</RibbonWindow>

Open in new window


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Ribbon;
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.OleDb;
using System.Data;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private const string dbName = "dbFX_1Min_db.mdb";
        private const string cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
        private const string dbPath = @"C:\Users\user\Documents\Markets\VBA Files\dbFX_DBs\";

        OleDbConnection dbConnection;
        OleDbCommand SQLQuery;
        OleDbDataAdapter dbAdapter;

        DataSet dSet;
        

        public MainWindow()
        {
            //InitializeComponent();

            // load the datagrid for the securities



        }

        private void FillDataGrid_Securities()
        {
            // load the datagrid for the securities
            ConnectionOpen();
            string strSQL = "SELECT [_Securities].Security, [_Securities].[Select] FROM _Securities ORDER BY [_Securities].Security;";
         
            SQLQuery = dbConnection.CreateCommand();
            SQLQuery.CommandText = strSQL;

            dbAdapter = new OleDbDataAdapter(SQLQuery);
            DataTable dTable = new DataTable("Securities");
            dbAdapter.Fill(dTable); 
            
            
            ConnectionClose();
        }


        private void buttSimSec_Click(object sender, RoutedEventArgs e)
        {
            // show the simulator pane and hide the rest
            
        }

        private void ConnectionOpen()
        {
            dbConnection = new OleDbConnection(cn + dbPath + dbName);
            dbConnection.Open();
        }

        private void ConnectionClose()
        {
            dbConnection.Close();
        }
    }
}

Open in new window

0
 
LVL 18

Accepted Solution

by:
dj_alik earned 500 total points
ID: 38853892
I found the problems:

In XAML:
<RibbonWindow x:Class="Window1"

replace to :  <RibbonWindow x:Class="WpfApplication1.MainWindow "

In code behind:
1. public partial class MainWindow : Window

replace to  MainWindow : RibbonWindow

2. public MainWindow()
        {
            //InitializeComponent();
modify code and please remove   the remark of  InitializeComponent();
in order to
reactivate the   call of InitializeComponent() in constructor of window

all problem will disappear.
0
 

Author Closing Comment

by:mcs26
ID: 38856932
Great thanks for your help!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now