C# 需要datagridview中数据绑定方面的帮助吗
我是c#新手,对windows窗体应用程序绝对是新手。我正在实现一个数独游戏 解算器I编码在C++中,因为我需要一个GUI。使用C# 需要datagridview中数据绑定方面的帮助吗,c#,data-binding,user-interface,datagridview,windows-forms-designer,C#,Data Binding,User Interface,Datagridview,Windows Forms Designer,我是c#新手,对windows窗体应用程序绝对是新手。我正在实现一个数独游戏 解算器I编码在C++中,因为我需要一个GUI。使用DataGridView,需要无数据库数据绑定方面的帮助。张贴我的代码,请帮助。需要了解 1。如何允许用户在datagridview和“inital”数组中输入数据。 namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form
DataGridView
,需要无数据库数据绑定方面的帮助。张贴我的代码,请帮助。需要了解
1。如何允许用户在datagridview和“inital”数组中输入数据。
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; }
}
}
2。如何读取该数据并在“复制”数组中复制。
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; }
}
}
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; }
}
}
命名空间窗口窗体应用程序3
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
bindGrid();
}
私有void bindGrid()
{
列表=新列表();
TestCode tt=新的TestCode();
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
this.dataGridView1.DataSource=list;
}
公共国际一级;
公共整数[,]首字母;
公共整数[,]已复制;
int输入值(int x,int y,int值)
{
初始值=新整数[9,9];
复制=新整数[9,9];
对于(int i=0;i<9;i++)
{
如果(值==复制的[x,i]| |值==复制的[i,y])
返回0;
}
对于(i=(x/3)*3;iprivate void bindGrid()
{
列表=新列表();
TestCode tt=新的TestCode();
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
列表。添加(tt);
this.dataGridView1.DataSource=list;
this.dataGridView1.DataBind();
}
Winforms DataGridView没有数据绑定方法,这与ASP.Net对应的方法不同。