绑定的问题
public class Form1 : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Data.DataSet ds = new System.Data.DataSet();
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
InitialDataAdapte();
}
private void InitialDataAdapte()
{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "BASE_MATERIAL", new System.Data.Common.DataColumnMapping[]
this.sqlSelectCommand1.CommandText = "SELECT WLDM, WLXH, WLMC FROM BASE_MATERIAL";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.sqlDataAdapter1.Fill(ds);
this.comboBox1.DataSource=ds;
this.comboBox1.DisplayMember ="BASE_MATERIAL.WLMC";
this.comboBox1.ValueMember="BASE_MATERIAL.WLDM";
this.BindingContext[this.comboBox1.DataSource,"BASE_MATERIAL"].SuspendBinding();
this.BindingContext[this.comboBox1.DataSource,"BASE_MATERIAL"].ResumeBinding();
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
MessageBox.Show(this.ds.Tables[0].Rows[this.BindingContext[this.comboBox1.DataSource,"BASE_MATERIAL.WLMC"].Position]["WLDM"].ToString().Trim());
}
我用设计器生成的数据集绑定,同样代码可以得到相应的Position,但自己代码生成一个dataset再填充绑定,就不能取到相应的position值