Northwest_worf
2017-07-20 08:55
采纳率: 0%
浏览 2.7k
已结题

c# datagrid动态添加下拉框

我想在表格中datagrid控件中动态添加下拉框,现有以下代码,但做出来的东西还是无法正确显示,应该怎么做:
namespace DataGridBackColor
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }
    private DataTable dtData;
    ComboBox comboBox = new ComboBox();

    private void Form1_Load(object sender, EventArgs e)
    {
        init_WareHouse();
       // comboBox.Visible = false;
        this.dgv_User.Controls.Add(comboBox);

    }



    private void initData() {
        dtData = new DataTable();
        dtData.Columns.Add("物料名称");
        dtData.Columns.Add("库房地址");
        dtData.Columns.Add("库位地址");
        dtData.Columns.Add("物料编码");
        dtData.Columns.Add("交货数量");
        dtData.Columns.Add("(单位)");

        DataRow drData;
        drData = dtData.NewRow();
        drData[0] ="啊啊啊啊";
        drData[1] = "bbbbbbb";
        drData[2] = "ccccccccc";
        drData[3] = "545455554";
        drData[4] = "1";
        drData[5] = "PC";
        dtData.Rows.Add(drData);
        this.dgv_User.DataSource = dtData;
    }



    private void init_WareHouse() {

         DataTable dtShelf = new DataTable();
        dtShelf.Columns.Add("Value");
        dtShelf.Columns.Add("Name");


            //String[] temp_ShelfInfo = ShelfInfo[i].Split(',');
            DataRow drShelf;
            drShelf = dtShelf.NewRow();
            drShelf[0] ="二级库";
            drShelf[1] ="4545454545";
            dtShelf.Rows.Add(drShelf);

            comboBox.ValueMember = "Value";
            comboBox.DisplayMember = "Name";
            comboBox.DataSource = dtShelf;
            comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
            this.dgv_User.Controls.Add(comboBox);
    }



    private void Serach_Click(object sender, EventArgs e)
    {

        initData();

    }

    private void dgv_User_CurrentCellChanged(object sender, EventArgs e)
    {
        try
        {
            if (this.dgv_User.CurrentCell.ColumnNumber==1)
            {
                MessageBox.Show("ddddd");
                Rectangle rect = new Rectangle(dgv_User.CurrentCell.ColumnNumber, dgv_User.CurrentCell.RowNumber,10,20);
                comboBox.Left = rect.Left;
                comboBox.Top = rect.Top;
                comboBox.Width = rect.Width;
                comboBox.Height = rect.Height;
                comboBox.Visible = true;
            }
        }
        catch
        {
        }

    }
}

}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题