Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# WPF DataGrid - Populate from database

Posted on 2013-02-03
6
Medium Priority
?
1,948 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…

886 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