private void Form1_Load(object sender, EventArgs e)
//dataGridView1.DataSource = intarray;//使用字符串数组,结果显示字符串的第一个公共属性Length
//第二种做法
Item[] items = new Item[]
{
new Item("one"),
new Item("two"),
new Item("three")
}
class Item
{
{
get { return txt; }
}
public Item(string text)
{
this.txt = text;
}
}
//另附以下程序
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0) //当表头选中时
{
return;
}
if (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == "two") //当选中“two”时,checkBox1被选中
{
checkBox1.Checked = true;
}
else
{
checkBox1.Checked = false;
{
//第一种做法(不如人意,没有达到要求,只显示字符串的长度)
//string[] intarray =new string[] {"one","two","three"};//dataGridView1.DataSource = intarray;//使用字符串数组,结果显示字符串的第一个公共属性Length
//第二种做法
Item[] items = new Item[]
{
new Item("one"),
new Item("two"),
new Item("three")
};
this.dataGridView1.DataSource = items;}
class Item
{
private string txt;
public string Text{
get { return txt; }
}
public Item(string text)
{
this.txt = text;
}
}
//另附以下程序
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0) //当表头选中时
{
return;
}
if (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString() == "two") //当选中“two”时,checkBox1被选中
{
checkBox1.Checked = true;
}
else
{
checkBox1.Checked = false;
}
}
//第一种做法运行结果如下:
//第二种做法行结果如下:
//当选中“two”时,checkBox1被选中
DataGridView 控件使用方法可参考: