2014-07-17 29 views
0

我是windows應用程序的新手。我有一個表格,其中有兩個字段,即標題和類型。 在類型字段中我有兩個值「O」&「T」。 現在我必須填充這個表標題作爲第一列和第二列的數據網格取決於類型字段值我必須放置一個控制即當類型字段將有「O」在那個時候我必須放置一個組合框在該列,當它是「T」我必須放置一個文本框根據條件動態地在數據網格中放置控件

我已經嘗試了很多,並搜索了很多,但同一列中的兩個控件對我來說是相當困難的作爲一個初學者。 Plz幫助我解決這個問題。

這是我的代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using ERP_Ship.App_Code.Fleet_Person; 
using ERP_Ship.App_Code.Appraisal; 
using ERP_Ship.Forms.Master; 
using ERP_Ship.App_Code.Common; 
using ERP_Ship.App_Code.Vessel_Hardening_Measures; 

namespace ERP_Ship.Forms.Reports 
{ 
    public partial class Vessel_Hardening_Measures : Form 
    { 

     // ? // 
     private DataSet ds = new DataSet("myDs"); 
     private DataTable dt = new DataTable("Apprdtl"); 

     //Define controls must be add to data grid. 
     private Label lblControl = new Label(); 
     private TextBox txtControl = new TextBox(); 
     private ComboBox cboControl = new ComboBox(); 

     //Capture the clicked cell 
     private DataGrid.HitTestInfo hitTestGrid; 

     //Control definishion to add to DataGrid 
     DataGridTableStyle dataGridStyle = new DataGridTableStyle(); 
     DataGridTextBoxColumn dataGridLableTitle = new DataGridTextBoxColumn(); 
     DataGridTextBoxColumn dataGridLableTitle1 = new DataGridTextBoxColumn(); 
     DataGridTextBoxColumn dataGridLableTitle2 = new DataGridTextBoxColumn(); 
     //DataGridTextBoxColumn dataGridLable = new DataGridTextBoxColumn(); 
     DataGridTextBoxColumn dataGridTextBox = new DataGridTextBoxColumn(); 
     DataGridTextBoxColumn dataGridComboBox = new DataGridTextBoxColumn(); 

     private System.Windows.Forms.DataGrid gv_Appraisal; 



     #region Form Level Variables 

     string _strName; 
     string _strCDCNo; 
     //DataTable dt = new DataTable(); 
     // OracleConnection con1 = new OracleConnection(ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString.ToString()); 
     I_Common objCommon = new I_Common(); 
     DataTable dt1 = new DataTable("dt1"); 
     #endregion 

     #region Public Properties 

     public string SearchName 
     { 
      set 
      { 
       _strName = value; 
      } 
      get 
      { 
       return _strName; 
      } 
     } 


     public string SearchCDCNo 
     { 
      set 
      { 
       _strCDCNo = value; 
      } 
      get 
      { 
       return _strCDCNo; 
      } 
     } 


     #endregion 



     public Vessel_Hardening_Measures() 
     { 
      InitializeComponent(); 
      InitializeControls(); 
      Load_Year(); 
      //for (int index = 0; index <= gv_Appraisal.Columns.Count - 1; index++) 
      //{ 
      // gv_Appraisal.Columns[index].DataPropertyName = gv_Appraisal.Columns[index].Name; 
      //} 
      //gv_Appraisal.AutoGenerateColumns = false; 


     } 

     private void InitializeControls() 
     { 
      //label property 
      lblControl.Cursor = Cursors.Hand; 
      lblControl.ForeColor = Color.Red; 
      lblControl.Font = new Font("Arial", 12, FontStyle.Bold | FontStyle.Italic); 

      //textbox property 
      txtControl.Cursor = Cursors.Hand; 
      txtControl.BackColor = Color.WhiteSmoke; 
      txtControl.ForeColor = Color.DarkSlateBlue; 
      txtControl.Font = new Font("Arial", 8, FontStyle.Bold); 
      //textbox events. 
      txtControl.TextChanged += new EventHandler(txtTextChanged); 
      string[] dropdownitems = { "Yes", "No", "Not Applicable" }; 
      //Define and add ComboBox rows, will be added to data grid. 

      for (int i = 0; i < dropdownitems.Count(); i++) 
       cboControl.Items.Add(dropdownitems[i]); 

      //combobox property 
      cboControl.Cursor = Cursors.Hand; 
      cboControl.DropDownStyle = ComboBoxStyle.DropDownList; 
      //combobox events. 
      cboControl.SelectedIndexChanged += new EventHandler(cboSelectedIndexChanged); 
     } 

     private void DesignTableStyle() 
     { 
      dataGridStyle.PreferredRowHeight = 24; 
      dataGridStyle.MappingName = "Apprdtl"; 
      gv_Appraisal.TableStyles.Add(dataGridStyle); 

      dataGridStyle.GridColumnStyles.Add(dataGridLableTitle); 
      dataGridStyle.GridColumnStyles.Add(dataGridLableTitle1); 
      dataGridStyle.GridColumnStyles.Add(dataGridLableTitle2); 
      dataGridStyle.GridColumnStyles.Add(dataGridTextBox); 
      //dataGridStyle.GridColumnStyles.Add(dataGridComboBox); 

      dataGridLableTitle.HeaderText = "vhm_id"; 
      dataGridLableTitle.MappingName = "vhm_id"; 
      dataGridLableTitle.Width = 1; 


      //dataGridLableTitle.Width = 40; 

      dataGridLableTitle1.HeaderText = "Title"; 
      dataGridLableTitle1.MappingName = "Title"; 
      dataGridLableTitle1.Width = 150; 

      dataGridLableTitle2.HeaderText = "Type"; 
      dataGridLableTitle2.MappingName = "Type"; 
      dataGridLableTitle2.Width = 1; 
      //dataGridLableTitle2.Width = 40; 


      dataGridTextBox.HeaderText = "TEXTBOX_COL"; 
      dataGridTextBox.MappingName = "TextBox_Col"; 
      dataGridTextBox.Width = 130; 

      //dataGridComboBox.HeaderText = "COMBOBOX_COL"; 
      //dataGridComboBox.MappingName = "ComboBox_col"; 
      //dataGridComboBox.Width = 130; 
     } 



     private void Load_DNF() // DATA NOT FOUND OF CDC NO 
     { 
      try 
      { 
       using (BL_Vessel_Hardening_Measures obj_vsl_hardening_measures = new BL_Vessel_Hardening_Measures()) 
       { 
        DataTable dataTable = new DataTable(); 

        ds = obj_vsl_hardening_measures.Get_Vessel_Hardening_Measures(); 

        dt1 = ds.Tables[0]; 
        DataColumn dc = new DataColumn("vhm_id"); 
        //Add created column to datatable object. 
        dt.Columns.Add(dc); 
        //Create a new column for datatable. 

        dc = new DataColumn("Title"); 
        //Add created column to datatable object. 
        dt.Columns.Add(dc); 
        //Create a new column for datatable. 
        dc = new DataColumn("Type"); 
        //Add created column to datatable object. 
        dt.Columns.Add(dc); 
        //Create a new column for datatable. 

        //Create a new column for datatable. 
        dc = new DataColumn("TextBox_Col", System.Type.GetType("System.String")); 
        //Add created column to datatable object. 
        dt.Columns.Add(dc); 



        DataRow dr; 
        for (int i = 0; i < dt1.Rows.Count; i++) 
        { 
         dr = dt.NewRow(); 
         dr["vhm_id"] = dt1.Rows[i]["vhm_id"]; 
         dr["Title"] = dt1.Rows[i]["Title"]; 
         dr["Type"] = dt1.Rows[i]["Type"]; 
         dr["TextBox_Col"] = ""; 
         //dr["ComboBox_Col"] = ""; 
         dt.Rows.Add(dr); 
        } 



        // gv_Appraisal.DataSource = ds.Tables[0]; 
        gv_Appraisal.DataSource = dt; 
        ds.Tables.Remove("Apprdtl"); 
        ds.Tables.Add(dt); 

       } 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 






     private void btn_Close_Click(object sender, EventArgs e) 
     { 
      cls_Utility.Close_Pending_Form(); 
      Home objHome = new Home(); 
      I_Common.CloseForm(this.MdiParent, this, objHome); 
     } 



     private void Reset_Controls() 
     { 
      //txt_CDC_NO.Text = ""; 
      //cmb_Name.SelectedIndex = -1; 
     } 

     ~Vessel_Hardening_Measures() 
     { 
      this.Close(); 
     } 

     private void Vessel_Hardening_Measures_FormClosed(object sender, FormClosedEventArgs e) 
     { 
      cls_Utility.Close_Pending_Form(); 
     } 

     private void Load_Year() 
     { 
      try 
      { 
       //ddl_Year.DisplayMember = "Year"; 
       //ddl_Year.ValueMember = "Year"; 

       objCommon.Load_Years(ddl_Year, 1); 

       //string str_year = ""; 
       //str_year = DateTime.Now.AddYears(-1).Year.ToString(); 
       //ddl_Year.Items.Add(str_year); 

       //str_year = DateTime.Now.Year.ToString(); 
       //ddl_Year.Items.Add(str_year); 

       // ddl_Year.SelectedIndex = 1; 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); 
      } 
     } 


     private void Vessel_Hardening_Measures_Load(object sender, EventArgs e) 
     { 

      DesignTableStyle(); 
      Load_DNF(); 

      ERP_Ship.App_Code.Common.I_Common.MdiFormTopPanel("Vessel Hardening Measures", "Vessel Hardening Measures", this.MdiParent, true); 
      //ERP_Ship.App_Code.Common.I_Common.StatusInfoLabel("Double Click the Search Record to View or Edit", "I", this.MdiParent, true); 




     } 

     private void gv_Appraisal_MouseUp(object sender, MouseEventArgs e) 
     { 
      hitTestGrid = gv_Appraisal.HitTest(e.X, e.Y); 
      if (hitTestGrid != null) 
      { 
       //Which column of datagrid has been clicked. 
       //switch (hitTestGrid.Column) 
       //{ 

       if (hitTestGrid.Column == 3) 
       { 


        if (gv_Appraisal[gv_Appraisal.CurrentRowIndex, 2].ToString() == "T") 
        { 



         dataGridStyle.GridColumnStyles.Clear(); 
         dataGridStyle.GridColumnStyles.Add(dataGridTextBox); 
         dataGridTextBox.HeaderText = "TEXTBOX_COL"; 
         dataGridTextBox.MappingName = "TextBox_Col"; 
         dataGridTextBox.Width = 130; 
         //Add texbox control to datagrid. 
         dataGridTextBox.TextBox.Controls.Add(txtControl); 
         // txtControl.Text = gv_Appraisal[gv_Appraisal.CurrentRowIndex, 3].ToString(); 
         txtControl.Focus(); 


        } 
        else if (gv_Appraisal[gv_Appraisal.CurrentRowIndex, 2].ToString() == "O") 
        { 

         dataGridStyle.GridColumnStyles.Clear(); 
         dataGridStyle.GridColumnStyles.Add(dataGridComboBox); 
         dataGridComboBox.HeaderText = "COMBOBOX_COL"; 
         dataGridComboBox.MappingName = "ComboBox_col"; 
         dataGridComboBox.Width = 130; 
         //Add combobox control to datagrid. 
         dataGridComboBox.TextBox.Controls.Add(cboControl); 
         cboControl.Focus(); 
         //for (int i = 0; i < cboControl.Items.Count; i++) 
         //{ 
         // if (cboControl.Items[i].ToString() == gv_Appraisal[gv_Appraisal.CurrentRowIndex, 3].ToString()) 
         //  cboControl.SelectedIndex = i; 
         //} 

        } 
       } 





      } 
     } 






     private void txtTextChanged(object sender, System.EventArgs e) 
     { 
      ds.Tables["Apprdtl"].Rows[gv_Appraisal.CurrentRowIndex]["TextBox_Col"] = txtControl.Text; 
     } 

     //Combobox selected index changed event. 
     private void cboSelectedIndexChanged(object sender, System.EventArgs e) 
     { 
      ds.Tables["Apprdtl"].Rows[gv_Appraisal.CurrentRowIndex]["TextBox_Col"] = cboControl.Text; 

     } 



    } 

} 

回答

1

有可能實現的功能多種方法,你正在尋找。 我在this link上發現了一個樣本

檢查那裏描述的方法是否適合您當前的需求需求。

+0

沒有找到解決方案link.page未找到即將到來 – Anuj

+0

鏈接已更新。你現在可以檢查嗎? – Ujjwal

+0

其中datagridtextBox聲明? – Anuj