回 帖 发 新 帖 刷新版面

主题:怎样实现在datagrid中添加下拉菜单

怎样实现在datagrid中添加下拉菜单

回复列表 (共1个回复)

沙发

不清楚你说的下拉菜单是什么,,我用comboBox控件来实现...

        private void Form1_Load(object sender, System.EventArgs e)
        {
            comboBox1.Visible=false;
            comboBox1.Items.Add("业务部");
            comboBox1.Items.Add("销售部");
            comboBox1.Items.Add("生产部");
            comboBox1.Items.Add("信息部");
            SqlConnection conn=new SqlConnection("Server=purana;Database=demo;Integrated Security=SSPI");
            SqlDataAdapter ada=new SqlDataAdapter("Select * From 工作室",conn);
            DataSet ds=new DataSet();
            conn.Open();
            ada.Fill(ds,"工作室");
            dataGrid1.DataSource=ds.Tables["工作室"].DefaultView;
            conn.Close();
        }

        private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
        {
            int row=dataGrid1.CurrentCell.RowNumber;
            int col=dataGrid1.CurrentCell.ColumnNumber;
            if(col==4)         //第五列
            {
                comboBox1.Left=dataGrid1.GetCurrentCellBounds().Left+8;
                comboBox1.Top=dataGrid1.GetCurrentCellBounds().Top+8;
                comboBox1.Width=dataGrid1.GetCurrentCellBounds().Width;
                comboBox1.Text=dataGrid1[row,col].ToString();
                comboBox1.Visible=true;
            }
            else
                 comboBox1.Visible=false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            int col=dataGrid1.CurrentCell.ColumnNumber;
            int row=dataGrid1.CurrentCell.RowNumber;
            if(col==4)
            {
                dataGrid1[row,col]=comboBox1.SelectedItem.ToString();
                comboBox1.Visible=false;
            }
        }

我来回复

您尚未登录,请登录后再回复。点此登录或注册