C#窗体 - 从数据库填充树视图

时间:2014-08-15 16:19:51

标签: c# sql windows treeview

我用Google搜索,并在树视图上研究了问题。但我不清楚如何从数据库填充树视图!有些使用parentId查询,有些使用Datarow!为何选择datarow?好吧,

我有一个名为DressType的表,其中包含以下字段

  1. 编号
  2. DressTypeName
  3. TailoringRate
  4. SticherRate
  5. CutterRate
  6. 我想用上面的DressTypeName填充treeview父元素,但是通过从文本字段中获取输入来选择父节点时将创建子节点。

    子节点的表称为DressMeasurement,它将在从文本字段获取输入时创建。假设一个用户选择一种服装类型,比如裤子,那么他将输入长度并将其添加到裤子下的子节点。

    树视图将是这样的

    -Pants

    -----长度

    -----腰围

    现在我面临两个问题。

    1. 如何从DressType表中填充treeview-parent节点?
    2. 如何通过选择父节点来填充DressMeasurement Table中的treeview-child节点?
    3. 这是我的DressType UI类

       public partial class DressTypeUI : Form
       {
          public DressTypeUI()
          {
              InitializeComponent();
          }
      
          private void addButton_Click(object sender, EventArgs e)
          {
              DressType aDressType = new DressType();
              aDressType.Name = nameTextBox.Text;
              aDressType.TailoringRate = Convert.ToDouble(tailoringRateTextBox.Text);
              aDressType.SticherRate = Convert.ToDouble(sticherRateTextBox.Text);
              aDressType.CutterRate = Convert.ToDouble(cutterRateTextBox.Text);
      
              DressTypeManager aDressTypeManager = new DressTypeManager();
      
      
              if (aDressTypeManager.SaveAll(aDressType))
              {
                  MessageBox.Show("Dress Type Added Successfully");
              }
              else
              {
                  MessageBox.Show("Error Adding Dress Type");
              }
          }
      }
      

      这是我的DressType Model Class

      public class DressType
      {
          public int DressTypelID { get; set; }
          public string Name { get; set; }
          public double TailoringRate { get; set; }
          public double SticherRate { get; set; }
          public double CutterRate { get; set; }
      }
      

      这是我的DressType Manager Class

      public class DressTypeManager
      {
          private DressTypeGateway aDressTypeGateway;
          public bool SaveAll(Model.DressType aDressType)
          {
              aDressTypeGateway = new DressTypeGateway();
      
              if (aDressTypeGateway.SaveAll(aDressType))
              {
                  return true;
              }
              else
              {
                  return false;
              }
      
          }
      
          public List<DressType> GetAll()
          {
              aDressTypeGateway = new DressTypeGateway();
              return aDressTypeGateway.GetAll();
          }
      }
      

      这是我的DressType网关类

          public class DressTypeGateway
      {
          private SqlConnection connection;
          private SqlCommand command;
      
          public DressTypeGateway()
          {
              connection = new SqlConnection();
              connection.ConnectionString = ConfigurationManager.ConnectionStrings["TailorShopDB"].ConnectionString;
      
          }
      
          public bool SaveAll(Model.DressType aDressType)
          {
              connection.Open();
              string query = string.Format("INSERT INTO DressTypeTable values ('{0}','{1}','{2}','{3}')", 
                  aDressType.Name, aDressType.TailoringRate, aDressType.SticherRate, aDressType.CutterRate
              );
      
              command = new SqlCommand(query, connection);
              int affectedRows = command.ExecuteNonQuery();
              connection.Close();
              if (affectedRows > 0)
              {
                  return true;
              }
              else
              {
                  return false;
              }
          }
      
          public List<Model.DressType> GetAll()
          {
              string query = "SELECT * FROM DressTypeTable";
              SqlConnection sqlConnection = new SqlConnection(connection.ConnectionString);
              sqlConnection.Open();
      
              SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
              SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
              List<DressType> dressTypes = new List<DressType>();
              while (sqlDataReader.Read())
              {
                  DressType aDressType = new DressType();
                  aDressType.Name = sqlDataReader[1].ToString();
                  dressTypes.Add(aDressType);
              }
              sqlConnection.Close();
              return dressTypes;
          }
      }
      

0 个答案:

没有答案