从表单中的文本框添加到另一种形式的数据视图C# [英] Adding from textbox in form to datagridview in another form C#

查看:59
本文介绍了从表单中的文本框添加到另一种形式的数据视图C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个文本框和1个按钮的表单1,我有包含dataGridView的表单2,我想从另一个表单向datagridview添加新行。



但是它给了我这个错误:

I Have form 1 with 4 textboxes and 1 button, and I have form 2 which contains dataGridView, I want to add new row to the datagridview from the other form.

but it give me this error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index









这是我在Button Click上的代码。







This is my code on the Button Click.

Main frm = new Main();

frm.dataGridView1.Rows[0].Cells[0].Value = textBox1.Text;
frm.dataGridView1.Rows[0].Cells[1].Value = textBox2.Text;
frm.dataGridView1.Rows[0].Cells[2].Value = textBox4.Text;
frm.dataGridView1.Rows[0].Cells[3].Value = dateTimePicker1.Value;
frm.dataGridView1.Rows.Add();
this.Hide();
frm.Show();

推荐答案

您好,在您的datagridview中添加列,因为没有定义列您无法将数据分配给datagridview,所以在datagridview中创建列,并使用以下行向datagridview添加行。



Hi, Add Columns in your datagridview, because without defining columns you can't assign data to datagridview, so create columns in datagridview, and use the following line to add rows to datagridview.

frm.dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox4.Text, dateTimePicker1.Value);


这篇关于从表单中的文本框添加到另一种形式的数据视图C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆