Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# WPF DataGrid - Populate from database

Posted on 2013-02-03
6
Medium Priority
?
1,922 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
Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

 

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 2000 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

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…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 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