如何使用文本框在gridview中插入行并增加1 [英] How to insert a row in gridview using textboxes and increase by one

查看:109
本文介绍了如何使用文本框在gridview中插入行并增加1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用文本框在gridview中插入一行时,它应该提交该值,下次当我们提交值时,它应该转到下一行。但是我的代码没有发生它,它取代了当前行



我的尝试:



when we insert a row in gridview by using textboxes it should submit that values and next time when we submit values it should goes to next line.but its not happening with my code,it replaces the current row

What I have tried:

form1.dataGridView1.Rows[1].Cells[0].Value = textBox1.Text;
             form1.dataGridView1.Rows[1].Cells[1].Value = textBox2.Text;
             form1.dataGridView1.Rows[1].Cells[2].Value = textBox3.Text;
             form1.dataGridView1.Rows[1].Cells[3].Value = textBox4.Text;
             form1.dataGridView1.Rows[1].Cells[4].Value = textBox5.Text;
             form1.dataGridView1.Rows[1].Cells[5].Value = textBox6.Text;
             form1.dataGridView1.Rows[1].Cells[6].Value = textBox7.Text;
             form1.dataGridView1.Rows[1].Cells[7].Value = textBox8.Text;
             form1.dataGridView1.Rows[1].Cells[8].Value = textBox9.Text;
             form1.dataGridView1.Rows[1].Cells[9].Value = textBox10.Text;
            MessageBox.Show("Saved successfully");

推荐答案

正如G3Coder所指出的那样,你不断覆盖行[1] ]。您需要每次向dataGridView添加一行,例如(未经测试)

As G3Coder has pointed out you are continuously overwriting Rows[1]. You need to add a row to the dataGridView each time e.g. (untested)
string[] rowData = new string[] { textBox1.Text, textBox2.Text,textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text, textBox10.Text };
dataGridView1.Rows.Add(row);

OR(也未经测试)

OR (also untested)

int i = dataGridView1.Rows.Count + 1;
dataGridView1.Rows.Add();
form1.dataGridView1.Rows[i].Cells[0].Value = textBox1.Text;
form1.dataGridView1.Rows[i].Cells[1].Value = textBox2.Text;
form1.dataGridView1.Rows[i].Cells[2].Value = textBox3.Text;
... etc


这篇关于如何使用文本框在gridview中插入行并增加1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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