Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.........

C#
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 = "";
           }
Posted

1 solution

Try this datagridview1.acceptchanges()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900