0

我是 C# 新手,对 Windows 窗体应用程序完全陌生。我正在实现一个我用 C++ 编写的数独求解器,因为我需要一个 GUI。在没有数据库的情况下使用DataGridView和需要帮助进行数据绑定。发布我的代码,请帮助。需要想法

1.如何让用户在datagridview和“初始”数组中输入数据。

2.如何读取该数据并复制到“复制”数组中。

3.如何将数据绑定到DataGridView。

namespace WindowsFormsApplication3
{


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            bindGrid();
        }

        private void bindGrid()
        {
            List<TestCode> list = new List<TestCode>();
            TestCode tt = new TestCode();
            list.Add(tt);
            list.Add(tt);
            list.Add(tt);
            list.Add(tt);
            list.Add(tt);
            list.Add(tt);
            list.Add(tt);
            list.Add(tt);
            list.Add(tt);

            this.dataGridView1.DataSource = list;
        }

        public int i;
        public int[,] initial;
        public int[,] copied;

        int inputvalue(int x, int y, int value)
        {

            initial = new int[9, 9];
            copied = new int[9, 9];

            for (int i = 0; i < 9; i++)
            {
                if (value == copied[x, i] || value == copied[i, y])
                    return 0;
            }

            for (i = (x / 3) * 3; i <= ((x / 3) * 3) + 2; i++)
                for (int j = (y / 3) * 3; j <= ((y / 3) * 3) + 2; j++)
                    if (copied[i, j] == value)
                        return 0;
            return value;
        }


        bool solve(int x, int y)
        {
            int i;
            int temp;
            if (copied[x, y] == 0)
            {
                for (i = 1; i < 10; i++)
                {
                    temp = inputvalue(x, y, i);
                    if (temp > 0)
                    {
                        copied[x, y] = temp;
                        if (x == 8 && y == 8)
                            return true;
                        else if (x == 8)
                        {
                            if (solve(0, y + 1))
                                return true;
                        }
                        else
                        {
                            if (solve(x + 1, y))
                                return true;
                        }
                    }
                }
                if (i == 10)
                {
                    if (copied[x, y] != initial[x, y])
                        copied[x, y] = 0;
                    return false;
                }
            }
            if (x == 8 && y == 8)
                return true;
            else if (x == 8)
            {
                if (solve(0, y + 1))
                    return true;

            }
            else
            {
                if (solve(x + 1, y))
                    return true;
            }
            return true;
        }

        private void fillDatagrid()
        {


        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            //this.dataGridView1.Rows.Add(new object[] { "Column1", "Column2", "Column3", "Column4","Column5","Column5","Column6","Column7","Column8","Column9", true });
            //this.dataGridView1.DataSource = copied;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i, j;
            Form1 P = new Form1();

            for (i = 0; i < 9; i++)
                for (j = 0; j < 9; j++)
                {
                    //dataGridView1[i, j];
                   // Console.SetCursorPosition(i + 1, j + 1);
                    //P.initial[i,j] = Console.ReadLine();
                }
            for (i = 0; i < 9; i++)
                for (j = 0; j < 9; j++)
                    P.copied = P.initial;
            if (P.solve(0, 0))
            {
                for (i = 0; i < 9; i++)
                    for (j = 0; j < 9; j++)
                    {
                        Console.SetCursorPosition(i + 1, j + 1);
                        Console.Write("   ", +P.copied[i, j]);

                    }
            }
            else
                Console.Write(" NO Solution");

        }
    }
    public class TestCode
    {
        public int Value1 { get; set; }
        public int Value2 { get; set; }
        public int Value3 { get; set; }
        public int Value4 { get; set; }
        public int Value5 { get; set; }
        public int Value6 { get; set; }
        public int Value7 { get; set; }
        public int Value8 { get; set; }
        public int Value9 { get; set; }
    }
}
4

1 回答 1

-1
private void bindGrid()
{
    List <TestCode> list = new List<TestCode>();
    TestCode tt = new TestCode();
    list.Add(tt);
    list.Add(tt);
    list.Add(tt);
    list.Add(tt);
    list.Add(tt);
    list.Add(tt);
    list.Add(tt);
    list.Add(tt);
    list.Add(tt);
    this.dataGridView1.DataSource = list;
    this.dataGridView1.DataBind();
}
于 2013-07-30T13:07:04.870 回答