Is this a right way to change DataGrid's value programmicality in C# WPF?

Mojtaba_Hakim 321 Reputation points
2023-01-03T22:03:03.73+00:00

I want to be able to easily change the values of datagrid cells at any time
Is this helper function a good solution? :

 public static DataGridCell GetCellConfig(DataGrid DGR1, string NAME_SUTUN, int ROWINEX)  
    {  
        var TheCol = DGR1.Columns.FirstOrDefault(c => c.SortMemberPath == NAME_SUTUN).DisplayIndex;  
        var DGCInf = new DataGridCellInfo(DGR1.Items[ROWINEX], DGR1.Columns[TheCol]);  
        var cellContent = DGCInf.Column.GetCellContent(DGCInf.Item);  
        if (cellContent != null)  
        {  
            return (DataGridCell)cellContent.Parent;  
        }  
        return null;  
    }  
  
        public static void CellTexSet(DataGrid TheDataGrid, string COLUMN_NAME, int ROW_INDEXY, object THE_VALUE)  
        {  
            var ELMNTY = GetCellConfig(TheDataGrid, COLUMN_NAME, ROW_INDEXY).Content;  
            switch (ELMNTY)  
            {  
                case TextBox _:  
                    ((TextBox)ELMNTY).Text = Convert.ToString(THE_VALUE);  
                    TheDataGrid.Items[ROW_INDEXY].GetType().GetProperty(COLUMN_NAME).SetValue(TheDataGrid.Items[ROW_INDEXY], THE_VALUE, new object[] { });  
                    break;  
                case TextBlock _:  
                    ((TextBlock)ELMNTY).Text = Convert.ToString(THE_VALUE);  
                    TheDataGrid.Items[ROW_INDEXY].GetType().GetProperty(COLUMN_NAME).SetValue(TheDataGrid.Items[ROW_INDEXY], THE_VALUE, new object[] { });  
                    break;  
                case ComboBox _:  
                    ((Selector)ELMNTY).SelectedValue = Convert.ToInt32(THE_VALUE);  
                    break;  
            }  
        }  

using : CellTexSet(pERSONELDataGrid, "REMARKS", 0, 12345);

Because now I give a number to a column that takes a string value, even though it has a conversion command, it still has this error

***System.ArgumentException: 'Object of type 'System.Int32' cannot be converted to type 'System.String'.'***  

275737-untitled.png

Note: This error happens if I am not paying attention to the type of data I am giving it

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,824 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,285 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
830 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,616 Reputation points Microsoft Vendor
    2023-01-05T06:42:03.217+00:00

    Hi,@Mojtaba_Hakim . Welcome Microsoft Q&A.

    For the problem System.ArgumentException: 'Object of type 'System.Int32' cannot be converted to type 'System.String'.' , you did not pay attention to type matching when setting the value.
    If you want to set the value in the String column, its type should be String property='4' . If you set the value to property=4, you could convert the type through property.ToString(); .
    I also read your question here.

    For the problem of editing and updating DataGrid data, it is recommended that you use MVVM. You can refer to the documentation Walkthrough: Display data from a SQL Server database in a DataGrid control and How to: Group, sort, and filter data in the DataGrid control for examples. Among them, the data class implements the interface INotifyPropertyChanged, and IEditableObject can implement notifications to edit and update data

    ----------------------------------------------------------------------------

    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.


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.