我想再dataGridView里加一列,这列里包括一个textbox和button,主要是我想点按钮时,弹出对话框来选商品,然后再将所选的商品名称填入textbox里面。

解决方案 »

  1.   

    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void Form2_Load(object sender, EventArgs e)
            {
                dataGridView1.Rows.Add();
                dataGridView1.Rows.Add();
                dataGridView1.Rows.Add();
                dataGridView1.Rows.Add();
                dataGridView1.Rows.Add();
            }        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                //这里Column2是按钮类型,Column1是文本类型
                if (e.ColumnIndex == Column2.Index)
                {
                    form f = new form();
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        dataGridView1[Column1.Index, e.RowIndex].Value = f.GetValue;
                    }
                }
            }
        }    public class form : Form
        {
            TextBox tb1 = new TextBox();
            TextBox tb2 = new TextBox();
            Button b1 = new Button();
            Button b2 = new Button();        public form()
            {
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Size = new Size(300, 300);
                this.Controls.Add(tb1);
                this.Controls.Add(tb2);
                this.Controls.Add(b1);
                this.Controls.Add(b2);
                tb1.Size = new Size(100, 21);
                tb1.Location = new Point(0, 0);
                tb1.Text = "文本1";            tb2.Size = new Size(100, 21);
                tb2.Location = new Point(0, 40);
                tb2.Text = "文本2";            b1.Size = new Size(50, 21);
                b1.Location = new Point(200, 0);
                b1.Text = "取文本1";            b2.Size = new Size(50, 21);
                b2.Location = new Point(200, 40);
                b2.Text = "取文本2";            b2.Click += new EventHandler(b2_Click);
                b1.Click += new EventHandler(b1_Click);
            }        public string GetValue { get; set; }        void b1_Click(object sender, EventArgs e)
            {
                GetValue = tb1.Text;
                this.DialogResult = DialogResult.OK;
            }        void b2_Click(object sender, EventArgs e)
            {
                GetValue = tb2.Text;
                this.DialogResult = DialogResult.OK;
            }
        }