combobox綁定數(shù)據(jù)庫與datagridview綁定數(shù)據(jù)庫
1.combobox綁定數(shù)據(jù)庫
在面頁加載事件中綁定:
SqlConnection connection = new SqlConnection(connectionString)
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
this.combobox1.DataSource = ds.Tables[0];
this.combobox1.ValueMember = "字段名";//下拉框綁定的值,一般與displaymember一一對應(yīng);
this.combobox1.DisplayMember = "字段名"; //下拉框中顯示的值。
this.combobox1.SelectedValue.ToString();//這是下拉框中的值被選定后,獲得的被選項。
combobox1.DropDownStyle = ComboBoxStyle.DropDownList;//限制combobox的內(nèi)容不能被用戶編輯,只能從下拉表中選
2.datagridview綁定數(shù)據(jù)庫
SqlConnection connection = new SqlConnection(connectionString)
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
this.dataGridView1.DataSource = ds;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataMember = ds.Tables[0].TableName;
dataGridView1.ReadOnly = true;
然后在datagridview的編輯中添加顯示的列名,并將每列的datapropertyname屬性與數(shù)據(jù)庫中的字段一一對應(yīng)(綁定)。
3.當(dāng)下拉框中的內(nèi)容發(fā)生改變時引的的事件一般是selectionchangecommitted事件。
在面頁加載事件中綁定:
SqlConnection connection = new SqlConnection(connectionString)
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
this.combobox1.DataSource = ds.Tables[0];
this.combobox1.ValueMember = "字段名";//下拉框綁定的值,一般與displaymember一一對應(yīng);
this.combobox1.DisplayMember = "字段名"; //下拉框中顯示的值。
this.combobox1.SelectedValue.ToString();//這是下拉框中的值被選定后,獲得的被選項。
combobox1.DropDownStyle = ComboBoxStyle.DropDownList;//限制combobox的內(nèi)容不能被用戶編輯,只能從下拉表中選
2.datagridview綁定數(shù)據(jù)庫
SqlConnection connection = new SqlConnection(connectionString)
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
this.dataGridView1.DataSource = ds;
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataMember = ds.Tables[0].TableName;
dataGridView1.ReadOnly = true;
然后在datagridview的編輯中添加顯示的列名,并將每列的datapropertyname屬性與數(shù)據(jù)庫中的字段一一對應(yīng)(綁定)。
3.當(dāng)下拉框中的內(nèi)容發(fā)生改變時引的的事件一般是selectionchangecommitted事件。
posted on 2012-04-17 09:54 SkyDream 閱讀(1262) 評論(0) 編輯 收藏 所屬分類: C# WinForm