Datagrid Simple update???

Dec 20, 2012 at 4:54 PM
Edited Dec 20, 2012 at 4:58 PM

I have some Data in a SQL table  that I want to update based on some Geo Data.  I get the data into a datagrid, but I seem to be fighting with how to grab the data out of the grid and update the column based on my Calc. 

 

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:local="clr-namespace:DbView" x:Class="DbView.PartofCity"
        Title="PartofCity" Height="700" Width="700" Loaded="Window_Loaded_1">
    <Window.Resources>
        <local:SQL1DataSet x:Key="SQL1DataSet"/>
        <CollectionViewSource x:Key="customerViewSource" Source="{Binding customer, Source={StaticResource SQL1DataSet}}"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource customerViewSource}">
        <TextBox x:Name="tbLat1" HorizontalAlignment="Left" Height="23" Margin="86,10,0,0" TextWrapping="Wrap" Text="44.591951" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="tbLong1" HorizontalAlignment="Left" Height="23" Margin="86,38,0,0" TextWrapping="Wrap" Text="-93.161269" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="tbLat2" HorizontalAlignment="Left" Height="23" Margin="86,71,0,0" TextWrapping="Wrap" Text="45.22476196" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="tbLong2" HorizontalAlignment="Left" Height="23" Margin="86,99,0,0" TextWrapping="Wrap" Text="-93.57265091" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="tbCalc" HorizontalAlignment="Left" Height="23" Margin="86,132,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="tbPOC" HorizontalAlignment="Left" Height="23" Margin="86,175,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

        <telerik:Label Content="Center City" HorizontalAlignment="Left" Margin="10,26,0,0" VerticalAlignment="Top" Width="71"/>
        <telerik:Label Content="Find Point" HorizontalAlignment="Left" Margin="10,86,0,0" VerticalAlignment="Top" Width="71" RenderTransformOrigin="0.352,3.182"/>
        <telerik:Label Content="Calulation" HorizontalAlignment="Left" Margin="18,133,0,0" VerticalAlignment="Top"/>
        <telerik:Label Content="Part Of City" HorizontalAlignment="Left" Margin="10,175,0,0" VerticalAlignment="Top"/>
        <Button Content="Calculate" HorizontalAlignment="Left" Margin="97,213,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>

        <DataGrid x:Name="dg" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="278,10,10,10" RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="cust_idColumn" Binding="{Binding cust_id}" Header="cust id" IsReadOnly="True" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="custnoColumn" Binding="{Binding custno}" Header="custno" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="custnameColumn" Binding="{Binding custname}" Header="custname" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="geoLatColumn" Binding="{Binding GeoLat}" Header="Geo Lat" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="geoLongColumn" Binding="{Binding GeoLong}" Header="Geo Long" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="custRegionColumn" Binding="{Binding custRegion}" Header="cust Region" Width="SizeToHeader"/>
            </DataGrid.Columns>
        </DataGrid>
        <Button x:Name="btnUpdate" Content="Update Table" HorizontalAlignment="Left" Margin="86,240,0,0" VerticalAlignment="Top" Width="87" Click="btnUpdate_Click"/>

    </Grid>
</Window>

C# Code for XAML code behind
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.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DbBusiness;

namespace DbView
{
    /// <summary>
    /// Interaction logic for PartofCity.xaml
    /// </summary>
    /// 
    public partial class PartofCity : Window
    {   
        public PartofCity()
        {
            InitializeComponent();
        }
        
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            // calculate the Great Circle Bearing
            // θ = atan2( sin(Δλ).cos(φ2), cos(φ1).sin(φ2) − sin(φ1).cos(φ2).cos(Δλ) )
            float lat1 = float.Parse(tbLat1.Text);
            float lat2 = float.Parse(tbLat2.Text);
            float dLon = float.Parse(tbLat1.Text); 

            
            var y = Math.Sin(dLon) * Math.Cos(lat2);
            var x = Math.Cos(lat1) * Math.Sin(lat2) -
                    Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(dLon);
            var brng = (Math.Atan2(y, x)/(Math.PI/180.0));
            //double brng = Math.Atan2(y, x);
            tbCalc.Text = brng.ToString();
            tbPOC.Text = Cardinal(brng);
        }

        public string Cardinal(double degrees)
        {
            if (degrees > 315.0 || degrees < 45.0)
                {
                    return "N";
                }
            if (degrees >= 45.0 && degrees < 135)
                {
                    return "E";
                }
            if (degrees >= 135.0 && degrees < 225)
                {
                    return "S";
                }
            if (degrees >= 225.0 && degrees < 315)
                {
                    return "W";
                }
            return "ZZ";
            // Etc for the whole 360 degrees.  Segment finer if you want NW, WNW, etc.
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {

            DbView.SQL1DataSet SQL1DataSet = ((DbView.SQL1DataSet)(this.FindResource("SQL1DataSet")));
            // Load data into the table customer. You can modify this code as needed.
            DbView.SQL1DataSetTableAdapters.customerTableAdapter SQL1DataSetcustomerTableAdapter = new DbView.SQL1DataSetTableAdapters.customerTableAdapter();
            SQL1DataSetcustomerTableAdapter.Fill(SQL1DataSet.customer);
            System.Windows.Data.CollectionViewSource customerViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("customerViewSource")));
            customerViewSource.View.MoveCurrentToFirst();
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            // calculate the Great Circle Bearing
            // θ = atan2( sin(Δλ).cos(φ2), cos(φ1).sin(φ2) − sin(φ1).cos(φ2).cos(Δλ) )
            float lat1 = float.Parse(tbLat1.Text);
            float lat2 = float.Parse(dg.Columns[3].ToString());
            float dLon = float.Parse(tbLat1.Text);
            

            var y = Math.Sin(dLon) * Math.Cos(lat2);
            var x = Math.Cos(lat1) * Math.Sin(lat2) -
                    Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(dLon);
            var brng = (Math.Atan2(y, x) / (Math.PI / 180.0));
            //double brng = Math.Atan2(y, x);
            tbCalc.Text = brng.ToString();
            dg.Columns[5] = Cardinal(brng);
        }
    }
}
 
Errors I am getting
Error 1 The name "SQL1DataSet" does not exist in the namespace "clr-namespace:DbView". 
Error 2 Cannot implicitly convert type 'string' to 'System.Windows.Controls.DataGridColumn' 
Error 2 has me confused becasue it should be a string.