0

我是c#的新手,絕對是windows窗體應用程序的新手。我正在實施一個Sudoku 求解器我使用C++編碼,因爲我需要一個GUI。使用DataGridView,需要數據綁定的幫助,而不需要數據庫。發佈我的代碼,請幫助。需要想法在datagridview中需要數據綁定的幫助

1.如何讓用戶在datagridview和「inital」數組中輸入數據。

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; } 
    } 
} 

回答

-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(); 
} 
+0

的的WinForms DataGridView中沒有一個DataBind方法不同,它是ASP.Net同行。 – stuartd