根据条件动态地将控件放在数据网格中

时间:2014-07-17 05:15:49

标签: c# windows datagrid

我是Windows应用程序的新手。我有一个表格,其中有两个字段,即标题和类型。 在类型字段中,我有两个值“O”& “T”。 现在我必须使用此表标题作为第一列和第二列填充数据网格,这取决于我必须放置控件的类型字段值,即当Type字段将具有“O”时,我必须在其中放置一个组合框列,当它是“T”时,我必须放置一个文本框

我已经尝试了很多并且google了很多但是同一列中的两个控件对我来说都很难作为初学者。 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 个答案:

答案 0 :(得分:1)

您正在寻找可能有多种方法来实现功能。 我在this link

上找到了一个样本

检查所描述的方法是否符合您当前的需求。