question

RONALDBEAUMAN-1773 avatar image
0 Votes"
RONALDBEAUMAN-1773 asked DaisyTian-1203 edited

How do you set a DataGid cell value programmatically?

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-wpf
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@RONALDBEAUMAN-1773
Your question description makes me a little confused. Do you want to use C# code to implement the DataGrid implemented in XAMLCode? Could you give me some description of second part C# code? It doesn't seem to be working for creating the DataGid.

0 Votes 0 ·

Thank you for responding to my inquiry so quickly. The XAML code defines the datagrid. The C# code is a snippet from my CellEndEdit procedure. When column 0 is being edited, the account title and status are retrieved from the database. If an error condition exists, a switch is set and an appropriate error message is displayed to the user. e.Cancel is set to true at the bottom of my procedure and the user is returned to the column 0 prompt. If all is well with the account number, the desired result is to display the account title in column 1 of the datagrid. I would beg your patience in that all of my C# knowledge is self taught. I am by no means a seasoned c# programmer. Please let me know if there is anything else I can do to clarify the matter further.

0 Votes 0 ·

@RONALDBEAUMAN-1773
Could you give a graphic to show me what you want to implement of your When column 0 is being edited, the account title and status are retrieved from the database?Does your section set a DataGid cell value programmatically in the title mean that you want to generate and bind data to the DataGrid in the C# code?

0 Votes 0 ·
Show more comments

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered DaisyTian-1203 edited

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 Columns[1] 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.


3.gif (19.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.