如何在C#中的datagridview中添加新记录 [英] how to add new record in datagridview in c#

查看:216
本文介绍了如何在C#中的datagridview中添加新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在创建采购账单
我想在gridview1中添加购买商品的记录...
我的问题是

当我第一次在datagridview1中添加项目时,它显示1row ..
再次,我第二次添加另一个项目,它两次添加..另一个记录
并第三次添加记录3次...
我希望记录只能添加一次...
我该怎么办...
请告诉我要正确运行程序应该进行哪些更改..

我的代码在下面给出
谢谢.........

hello ,
i m creating the purchase bill
i want to add the record of purchase item in gridview1...
my problem is

when i add the item 1st time in datagridview1 it show 1row..
again i m add the another item 2nd time it add it two times.. the another record
and for 3rd time it add the record 3 time...
i want the record should be add only one time...
how can i do that...
pls tell me what changes should i make to run the program correctly..

my code is given below
thanks.........

void dgv_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
       {
           textBox1.Text = dgv.Rows[e.RowIndex].Cells[0].Value.ToString();
           dgv.Hide();
           cn.Open();
           string query = "select part_no,part_name,mrp from part where part_no='" + textBox1.Text + "'";
           dt = new DataTable();
           ds = new DataSet();
           da = new SqlDataAdapter(query, cn);
           da.Fill(ds);

           //Close the Connection
           cn.Close();
           dataGridView1.Rows.Add(1);
           int r = dataGridView1.Rows.Count;

           for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
           {
               //dataGridView1.Rows.Add(1);
               dataGridView1.Rows[r].Cells["gv_part_no"].Value = ds.Tables[0].Rows[i]["part_no"].ToString();
               dataGridView1.Rows[r].Cells["gv_part_name"].Value = ds.Tables[0].Rows[i]["part_name"].ToString();
               dataGridView1.Rows[r].Cells["gv_pono"].Value = "";
               dataGridView1.Rows[r].Cells["gv_docqty"].Value = 1;
               dataGridView1.Rows[r].Cells["gv_rate"].Value = ds.Tables[0].Rows[i]["mrp"].ToString();
               dataGridView1.Rows[r].Cells["gv_amount"].Value = "";
           }

推荐答案

尝试使用此datagridview1.acceptchanges()
Try this datagridview1.acceptchanges()


这篇关于如何在C#中的datagridview中添加新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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