0

我将如何将我在 NewActivity 的文本框中输入的文本插入到 form1 的 datagridview 的第一列中?

这是我到目前为止的编码。

表格1

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.IsMdiContainer = true;
        }

        private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
        {
        }

        private void newActivityToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewActivity NewAc = new NewActivity();
            NewAc.MdiParent = this;
            NewAc.Show();
        }

        private void deleteActivityToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
    }
}

新活动

 public partial class NewActivity : Form
    {
        public string activityName;

        public NewActivity()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            activityName = "";
            this.Close(); 
        }

        private void btnAddActivity_Click(object sender, EventArgs e)
        {
            activityName = txtActivityName.Text;            
            this.Close();           
        }             
    }
}
4

2 回答 2

0

您可以将其插入您的事件点击

private void btnAddActivity_Click(object sender, EventArgs e)
    {
        activityName = txtActivityName.Text;    
        int index = yourDataGridView.Rows.Add();
       DataGridViewRow row = yourDataGridView.Rows[index];
       row.Cells[0].Value =   activityName ;      
        this.Close();           
    }   
于 2012-08-15T19:47:22.117 回答
0

下面是如何将文本框控件中的数据绑定到 DataGrid 的示例

// create new row
DataGridViewRow row = new DataGridViewRow();

// create cells
row.CreateCells(this.dataGridView1, textBox1.Text, textBox2.Text, textBox3.Text);

// add to data grid view
this.dataGridView1.Rows.Add(row);

---------------下面是你在你的情况下如何使用它--------

private void btnAddActivity_Click(object sender, EventArgs e)
{
   activityName = txtActivityName.Text;    
   int index = dgvActivityList .Rows.Add();
   DataGridViewRow row = dgvActivityList .Rows[index];
   row.Cells[0].Value = activityName;      
   this.Close();           
}   
于 2012-08-15T19:36:12.907 回答