[基础]datagridview绑定数据源的几种常见方式
private void button1_Click(object sender, EventArgs e)
{
//using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString()))
//{
// SqlDataAdapter sda = new SqlDataAdapter("Select * From T_Class Where F_Type='Product' order by F_RootID,F_Orders", conn);
// DataSet Ds = new DataSet();
// sda.Fill(Ds, "T_Class");
// //使用DataSet绑定时,必须同时指明DateMember
// //this.dataGridView1.DataSource = Ds;
// //this.dataGridView1.DataMember = "T_Class";
// //也可以直接用DataTable来绑定
// this.dataGridView1.DataSource = Ds.Tables["T_Class"];
//}
//甚至直接直接绑定数组
ArrayList AL = new ArrayList();
AL.Add(new myItem("text1", "value1"));
AL.Add(new myItem("text2", "value2"));
AL.Add(new myItem("text3", "value3"));
this.dataGridView1.DataSource = AL;
}
class myItem
{
private string _Text;
private string _Value;
public myItem(string text, string value)
{
this._Text = text;
this._Value = value;
}
public string Text {
set { _Text = value; }
get { return _Text; }
}
public string Value
{
set { _Value = value; }
get { return _Value; }
}
}
{
//using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ToString()))
//{
// SqlDataAdapter sda = new SqlDataAdapter("Select * From T_Class Where F_Type='Product' order by F_RootID,F_Orders", conn);
// DataSet Ds = new DataSet();
// sda.Fill(Ds, "T_Class");
// //使用DataSet绑定时,必须同时指明DateMember
// //this.dataGridView1.DataSource = Ds;
// //this.dataGridView1.DataMember = "T_Class";
// //也可以直接用DataTable来绑定
// this.dataGridView1.DataSource = Ds.Tables["T_Class"];
//}
//甚至直接直接绑定数组
ArrayList AL = new ArrayList();
AL.Add(new myItem("text1", "value1"));
AL.Add(new myItem("text2", "value2"));
AL.Add(new myItem("text3", "value3"));
this.dataGridView1.DataSource = AL;
}
class myItem
{
private string _Text;
private string _Value;
public myItem(string text, string value)
{
this._Text = text;
this._Value = value;
}
public string Text {
set { _Text = value; }
get { return _Text; }
}
public string Value
{
set { _Value = value; }
get { return _Value; }
}
}
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。