Binding RadioButtons with Gender Property in WPF Using Value Converter

Many WPF developers face a common problem when working with RadioButtons . They try to bind RadioButtons with a Gender property, but the binding does not work as expected . Why does this happen? Because RadioButtons work with true/false values , but in real applications, Gender is stored as meaningful values like: Male Female So the big question is: How do we connect a true/false UI control with a Male/Female data value? Why RadioButton Binding Fails A RadioButton uses the IsChecked property, and this property accepts only: true false But your Gender property is usually: a string ( "Male" , "Female" ) or an enum So the UI value and the data value are different types . This mismatch is the root cause of the problem. The Solution: Value Converter WPF provides a powerful feature called a Value Converter . What is a Value Converter? A Value Converter converts one value type into another. In our case: Convert Gender (...

How to Import/Export data of DataGrid from/to CSV file in wpf using MVVM

In this post i will explain how to Import/Export data of DataGrid from/to CSV file in wpf using MVVM

To Import CSV to DataGrid i have created method called LoadCSV() which will load CSV data and update the DataGrid


 private void LoadCSV()

        {

            LstEmployees.AddRange(File.ReadAllLines("csvFilePath")

                                         .Skip(1)

                                         .Select(v => CsvToEmployees(v))

                                         .ToList());


        }

        

In LoadCSV() method i am first reading the CSV using File clas ReadAllLines() method

After that i am skip the first line becuase in first line we used to have Header for CSV file

And then i am calling CsvToEmployees() which take a single line and split the text by comma and then convert it to Employees.

After converting All text to Employees list i am adding the list to ObservableCollection which is Binded with DataGrid


        Employees CsvToEmployees(string csvLine)

        {

            var emp = new Employees();

            if(!string.IsNullOrEmpty(csvLine))

            {

                string[] values = csvLine.Split(',');

                emp.EmpId = Convert.ToInt32(values[0]);

                emp.EmpName = values[1];

                emp.JobTitle = values[2];

                emp.Experience = values[3];

                emp.Salary = Convert.ToDouble(values[4]);

            }

            return emp;

        }

        

To Export DataGrid data to CSV i have added command called ExportCommand which will bind to Export Button in View


 private DelegateCommand _exportCommand;

        public DelegateCommand ExportCommand =>

            _exportCommand ?? (_exportCommand = new DelegateCommand(Export));


        private void Export()

        {

            using(var sw = new StreamWriter("CSVPath")

            {

                string header = String.Join(",", "Emp Id", "Emp Name", "Jop Title", "Experience", "Salary");

                sw.Write($"{header}{Environment.NewLine}");

                foreach(var emp in LstEmployees)

                {

                    string csv = String.Join(",", emp.EmpId, emp.EmpName,emp.JobTitle, emp.Experience, emp.Salary);

                    sw.Write($"{csv}{Environment.NewLine}");


                }

            }

        }    


In Export() method i am using StreamWriter to write to the CSV file.

For the Header i created the string with comma seperated Header text and call the Write() method to Write header to CSV file.

After That i am iterating the LstEmployees and make the comma seperated string with employee value and again call the Write() method to Write data to CSV file.


check this link for the code

Comments

Popular posts from this blog

Filter DataGrid and ListView in wpf using ICollectionView

Pagination of DataGrid in WPF using MVVM

How to Create TabControl using Prism Region