How do you set a DataGid cell value programmatically?

RONALD BEAUMAN 81 Reputation points
2021-05-06T02:07:13.497+00:00

Gentlemen:

I have a WPF DataGrid that will be used to accept general journal postings from an operator.
I accept a general ledger account number in column zero and would like to display the account
title in column one following entry and acceptance of the account number. I am attempting
to do this in the CellEndEdit method. Nothing that I have tried seems to work. Any input
would be appreciated. Samples of my code follows. Ron.

XAML code:

    <DataGrid x:Name="db_GL_Dist" GridLinesVisibility="All" VerticalScrollBarVisibility="Visible" 
                HorizontalScrollBarVisibility="Hidden" Height="225" Width="605"
                AlternatingRowBackground="LightGray" AlternationCount="2"
                CanUserResizeRows="false" CanUserResizeColumns="false"
                HorizontalContentAlignment="Center" Margin="40,175,47,170" AutoGenerateColumns="False" 
                CellEditEnding="db_GL_Dist_CellEndEdit" BeginningEdit="db_GL_Dist_BeginningEdit" RowEditEnding="db_GL_Dist_RowEditEnding">
        <DataGrid.Columns>
            <DataGridTextColumn DisplayIndex="0" Header="Account No." Width="130"
                IsReadOnly="False" Binding="{Binding accno}"/>
            <DataGridTextColumn DisplayIndex="1" Header="Description" Width="190"
                IsReadOnly="True" Binding="{Binding accdesc, NotifyOnSourceUpdated=True, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
            <DataGridTextColumn DisplayIndex="2" Header="            Debit Amount" Width="130"
                IsReadOnly="false" Binding="{Binding dramount, StringFormat={}{0:N2}}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Right" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn DisplayIndex="3" Header="           Credit Amount" Width="130"
                IsReadOnly="false" Binding="{Binding cramount, StringFormat={}{0:N2}}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Right" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

C# code:

        if (col == 0)
        {
            str3 = "select accdesc1, accstat from accmas where accno = \"" + content + "\";";
            cmd3 = new MySqlCommand(str3, con3);

            readin3 = cmd3.ExecuteReader();

            readresult3 = readin3.Read();

            if (!readresult3)
            {
                MessageBox.Show("Account number is not on file.  Please re-enter.", "", MessageBoxButton.OK, 
                    MessageBoxImage.Error);

                OK_to_proceed = false;
            }
            else
            {
                glpdesc = readin3.GetString(0);
                accstat = readin3.GetString(1);

                if (accstat == "I")
                {
                    MessageBox.Show("Account number is inactive.  Please re-enter.", "", MessageBoxButton.OK, 
                        MessageBoxImage.Error);

                    OK_to_proceed = false;
                }
                else
                {
                    // instruction to load account title into column 1.
                }
            }

            readin3.Close();
        }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,760 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2021-05-25T03:27:04.983+00:00

    I added an Event MouseDoubleClick="db_GL_Dist_MouseDoubleClick" for DataGrid and its C# code is:

    private void DataGridRow_MouseDoubleClick(object sender, MouseButtonEventArgs e)  
            {  
                var dgtc = db_GL_Dist.Columns[1] as DataGridTextColumn;  
                dgtc.Header = "Account Title";  
                dgtc.Binding = new Binding("acctit");  
            }  
    

    When double click the DataGrid, your Columns1 will update with Account Title like below:
    99325-3.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.