2

northwinddataset = (NORTHWNDDataSet)bsEmployees.DataSource; 下面代码中的实际含义是什么?它是northwinddataset 实例的数据源?

 public partial class FormEdit : Form
 {
     BindingSource bsEmployees;
     NORTHWNDDataSet northwinddataset;                    
     EmployeesTableAdapter taEmployees;

     public FormEdit(BindingSource bsEmployees)            
     {
         InitializeComponent();

         this.bsEmployees = bsEmployees;
         taEmployees = new EmployeesTableAdapter();
         /***** This line *****/
         northwinddataset = (NORTHWNDDataSet)bsEmployees.DataSource;
         /*********************/

        }

        private void FormEdit_Load(object sender, EventArgs e)               
        {
            try
            {
                tbLastName.DataBindings.Add("Text", bsEmployees, northwinddataset.Employees.LastNameColumn.ColumnName);
                tbFirstName.DataBindings.Add("Text", bsEmployees, northwinddataset.Employees.FirstNameColumn.ColumnName);
                tbTitle.DataBindings.Add("Text", bsEmployees, 
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Greska: {0}", ex.Message));
                Close();
            }
        }

        private void btnSnimi_Click(object sender, EventArgs e)                   
        {
            bsEmployees.EndEdit();   
            if (taEmployees.Update(northwinddataset.Employees)> 0)
             {
                 MessageBox.Show("Recorded");
                 Close();
             }
4

1 回答 1

0

这一行:

northwinddataset = (NORTHWNDDataSet)bsEmployees.DataSource;

它基本上是将其转换bsEmployees.DataSource为一种NORTHWNDDataSet类型。这样你就可以使用/调用所有可用的函数/成员NORTHWNDDataSet。上可能不可用的函数和成员bsEmployees.DataSource

这种操作称为Casting (C# Programming Guide)

于 2012-09-05T15:29:17.750 回答