Migrate windows form to MVVM pattern

Srikant S 46 Reputation points
2020-09-18T07:50:29.577+00:00

Hi Team,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Matrix
{
    public partial class Form1 : Form
    {
        int matrix;
        Form2 frm2;
        Dictionary<int, int> adicChecked = new Dictionary<int, int>();
        DataTable dt;

        public Form1()
        {
            InitializeComponent();
        }

        public void GetTable()
        {
            dt = GetData();
            dataGridView1.DataSource = dt;
        }

        /// <summary>
        /// Method for Datagrid Values
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            DataTable dt = new DataTable();

            for (int i = 0; i < matrix; i++)
            {
                dt.Columns.Add();
                DataRow row = dt.NewRow();
                dt.Rows.Add(row);
            }
            this.dataGridView1.Visible = true;
            return dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("Enter value greater than zero");
            }
            matrix = Convert.ToInt32(textBox1.Text);
            if (matrix > 0)
            {
                GetTable();
                if (frm2 != null)
                {
                    frm2.Close();
                }
                frm2 = new Form2(matrix);
                frm2.Show();
            }
            else
            {
                MessageBox.Show("Enter value greater than zero");
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            bool isChecked = true;
            int cellRow = e.RowIndex;
            int cellCoumn = e.ColumnIndex;
            if (dataGridView1.Rows[cellRow].Cells[cellCoumn].Value.ToString() == "True")
            {
                isChecked = false;
                dataGridView1.Rows[cellRow].Cells[cellCoumn].Value = isChecked;
            }
            else
            {
                isChecked = true;
                dataGridView1.Rows[cellRow].Cells[cellCoumn].Value = isChecked;

            }

            if (adicChecked.ContainsKey(cellRow))
            {
                adicChecked.Add(cellRow, cellCoumn);
            }
            List<string> list = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (dt.Rows[i][j].ToString() == "True")
                    {
                        list.Add("Row:" + i + " " + "column:" + j);
                    }
                }
            }

            frm2.CellClicked(cellRow, cellCoumn, isChecked,list);
        }
    }
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Matrix
{
    public partial class Form2 : Form
    {
        public delegate void MyDel(int cellRow,int cellCoumn);
        public event MyDel MyEvent;
        public int matrixValue { get; set; }
        public Form2(int mat)
        {
            InitializeComponent();
            matrixValue = mat;
            DataTable dt = GetData();
            dataGridView1.DataSource = dt;
        }
        /// <summary>
        /// Testing summary details
        /// </summary>
        /// <param name="cellRow"></param>
        /// <param name="cellCoumn"></param>
        /// <param name="isChecked"></param>
        /// <param name="list"></param>
        public void CellClicked(int cellRow, int cellCoumn, bool isChecked,List<string> list)
        {
            listBox1.Items.Clear();
            listBox1.Items.AddRange(list.ToArray());
            dataGridView1.Rows[cellRow].Cells[cellCoumn].Value = isChecked;
        }
        public DataTable GetData()
        {
            DataTable dt = new DataTable();
            for (int i = 0; i < matrixValue; i++)
            {
                DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
                dataGridView1.Columns.Add(chk);
                DataRow row = dt.NewRow();
                dt.Rows.Add(row);
            }
            this.dataGridView1.Visible = true;
            return dt;
        }
    }
}

I have one project were i devloped matrix n*n with two form, one form having datagrid and another form have checkbox enabled after clicking on form one values, as toggled, now i want this into MVVM without using Xaml or wpf, is that possible to perfomr this operation??

Also i want to use Events and delegate as used Case in MVVM pattern along with Interfaces

Attached files of my project..

Regards,
Srikant S

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,865 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2020-09-21T06:41:41.25+00:00

    Hi Srikant S,
    You can use MVVM in Windows Forms, and you can link properties and ViewModel commands like in WPF.
    About implementing MVVM with Windows Forms, please refer to this thread.
    Hope it could be helpful to you.
    Best Regards,
    Daniel Zhang


    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