C#DataGridView中加ComboBox

来源:互联网 发布:如何做好外贸 知乎 编辑:程序博客网 时间:2024/04/30 04:09
        /// <summary>
        /// 给dataGridView绑定 ComboBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            DataGridViewComboBoxCell DgvCell = this.dataGridView1.Rows[e.RowIndex].Cells["Unit"] as DataGridViewComboBoxCell;//获得加入ComboBox的列


            DataTable dt_com = new DataTable();


            dt_com.Columns.Add("Unit", Type.GetType("System.String"));


            DataRow dr = dt_com.NewRow();
            dr["Unit"] = "个";
            dt_com.Rows.Add(dr);


            dr = dt_com.NewRow();
            dr["Unit"] = "台";
            dt_com.Rows.Add(dr);


            dr = dt_com.NewRow();
            dr["Unit"] = "套";
            dt_com.Rows.Add(dr);


            dr = dt_com.NewRow();
            dr["Unit"] = "包";
            dt_com.Rows.Add(dr);


            dr = dt_com.NewRow();
            dr["Unit"] = "箱";
            dt_com.Rows.Add(dr);


            DgvCell.DataSource = dt_com;
            DgvCell.DisplayMember = "Unit";
            DgvCell.ValueMember = "Unit";
        }
原创粉丝点击