Solved

C# WPF DataGrid - Populate from database

Posted on 2013-02-03
6
1,871 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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