日期:2014-05-18 浏览次数:21553 次
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dgv_1.RowHeadersVisible = false;
dgv_2.RowHeadersVisible = false;
dgv_3.RowHeadersVisible = false;
dgv_4.RowHeadersVisible = false;
}
private void btn_BindIList_Click(object sender , EventArgs e)
{
//将IList<int>绑定到dgv_1
IList<int> intList = new List<int>();
intList.Add(1);
intList.Add(99);
dgv_1.DataSource = intList;
//将Table绑定到dgv_2
DataTable dt = new DataTable();
dt.Columns.Add();
dt.Rows.Add(1);
dt.Rows.Add(99);
dgv_2.DataSource = dt;
//将IList<String>绑定到dgv_3
IList<String> strList = new List<String>();
strList.Add("1");
strList.Add("99");
dgv_3.DataSource = strList;
//将IList<MyString>绑定到dgv_4,MyString是一个包含一个string成员变量的类
IList<MyString> myStrList = new List<MyString>();
myStrList.Add(new MyString("1"));
myStrList.Add(new MyString("99"));
dgv_4.DataSource = myStrList;
//将IList<MyStringInt>绑定到dgv_5,MyStringInt是一个包含一个string和一个int成员变量的类
IList<MyStringInt> myStrIntList = new List<MyStringInt>();
myStrIntList.Add(new MyStringInt("1" , 1));
myStrIntList.Add(new MyStringInt("99" , 99));
dgv_5.DataSource = myStrIntList;
dgv_2.Columns[0].Width = dgv_2.Width - 3;
dgv_3.Columns[0].Width = dgv_3.Width - 3;
//dgv_4.Columns[0].Width = dgv_4.Width - 3;
//dgv_3.Columns[0].Width = dgv_3.Width - 3;
}
}
public class MyString
{
public string str;
public MyString(string str)
{
this.str = str;
}
}
public class MyStringInt
{
public string str;
public int i;
public MyStringInt(string str , int i)
{
this.str = str;
this.i = i;
}
}
//将IList <int>绑定到dgv_1
IList <int> intList = new List <int>();
intList.Add(1);
intList.Add(99);
dgv_1.DataSource = intList;
dgv_1.DataBind(); //here