Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Project as following:
1- WPF C# project
2- Datasource linked to MySQL
3- Datagrid is hidden (No edit Cell)
4- Button to perform cell value change in datagrid
Required: How to update the datasource when click the button?

XML
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Wpf4" x:Class="Wpf4.MainWindow"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">

    <Window.Resources>
        <local:hqdDataSet x:Key="hqdDataSet"/>
        <CollectionViewSource x:Key="ayaViewSource" Source="{Binding aya, Source={StaticResource hqdDataSet}}"/>
    </Window.Resources>

    <Grid DataContext="{StaticResource ayaViewSource}">
        <Button x:Name="FBUTT" Content="Button" HorizontalAlignment="Left" Margin="409,275,0,0" VerticalAlignment="Top" Width="75" Click="FBUTT_Click"/>

        <DataGrid x:Name="DGA" Visibility="Hidden" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="330,10,10,68" RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="ANO" Binding="{Binding AYANO}" Header="AYANO" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="SID" Binding="{Binding SORID}" Header="SORID" Width="SizeToHeader"/>
                <DataGridTextColumn x:Name="RDF" Binding="{Binding RDQ}" Header="RDQ" Width="SizeToHeader"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>


C#
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.Navigation;
using System.Windows.Shapes;

namespace Wpf4
{    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Wpf4.hqdDataSet hqdDataSet = ((Wpf4.hqdDataSet)(this.FindResource("hqdDataSet")));
            Wpf4.hqdDataSetTableAdapters.ayaTableAdapter hqdDataSetayaTableAdapter = new Wpf4.hqdDataSetTableAdapters.ayaTableAdapter();
            hqdDataSetayaTableAdapter.Fill(hqdDataSet.aya);
            System.Windows.Data.CollectionViewSource ayaViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("ayaViewSource")));
            ayaViewSource.View.MoveCurrentToFirst();
        }

        private void FBUTT_Click(object sender, RoutedEventArgs e)
        {
            for (int kw = 0; kw < 100; kw++)
            {               
                DataGridCell PCA = Datagrid.GetCell(DGA, kw, 2);                
                TextBlock TPCA = PCA.Content as TextBlock;
                if (int.Parse(TPCA.Text) == 1)
                {
                    Datagrid.GetCell(DGA, kw, 2).Content = 0;                    
                    return;
                }
            }              
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 11-Jan-16 17:13pm    
All right, where is your problem? Note that hidden state does not affect the update, is unrelated to is.
—SA
Ehab Al Borm 12-Jan-16 6:17am    
ok what is the alternative way to access the database (get, set, update) in hidden mode by click button function
thanks Sergey.
Sinisa Hajnal 12-Jan-16 2:41am    
How to update?....what have you tried? Which part doesn't work?
Ehab Al Borm 12-Jan-16 6:08am    
the problem that I can't update database through datagrid
because I change the datagrid cell programmatically, not edit cell
Sergey Alexandrovich Kryukov 12-Jan-16 10:40am    
And what prevents you from changing it? Do you use data binding?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900