Click here to Skip to main content
15,884,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need help in DataGrid.

I have a DataGrid in Enabled editing mode. I have AutoComplete in first cell and on this basis data is retrived from database in other cells of corresponding row.
This works on dataGridView1_CellEndEdit Event. But on second row edit my first row data is refreshing.
Plz Please suggest me.


C#
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  {
      int i;
      i = dataGridView1.SelectedCells[0].RowIndex;
      int g = dataGridView1.Rows.Count;
    
          if (dataGridView1.CurrentCell.ColumnIndex == 0)
          {
              if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "")
              {

                  for (int i4 = 0; i4 < dataGridView1.Rows.Count; i4++)
                  {
                      if (i4 == dataGridView1.Rows.Count - 1 && dataGridView1.Rows[i].Cells[0].Value.ToString() != "")
                      {
                          string item = dataGridView1.Rows[i4-1].Cells[0].Value.ToString();
                          string sql = "select MRP,Units from list where item ='" + item + "' ";
                          DataTable dt;
                          dt = obj.ReturnDT(sql);

                          if (dt.Rows.Count > 0)
                          {

                              dataGridView1.Rows[i4-1].Cells[1].Value = "1";
                              string unit = dt.Rows[0]["Units"].ToString();
                              dataGridView1.Rows[i4-1].Cells[2].Value = unit;
                              string mrp1 = dt.Rows[0]["MRP"].ToString();
                              dataGridView1.Rows[i4-1].Cells[3].Value = mrp1;
                              dataGridView1.Rows[i4-1].Cells[4].Value = "0";
                              dataGridView1.Rows[i4-1].Cells[5].Value = mrp1;
                              dataGridView1.Rows[i4-1].Cells[6].Value = mrp1;
                              count = 1;

                          }
                      }
         
    
                  }




              }
    
      }
          if (dataGridView1.CurrentCell.ColumnIndex != 0 && dataGridView1.CurrentCell.ColumnIndex != 6)
          {

              string quantity = dataGridView1.Rows[i].Cells[1].Value.ToString();
              string mrp = dataGridView1.Rows[i].Cells[3].Value.ToString();

              string dis = dataGridView1.Rows[i].Cells[4].Value.ToString();
              string rate = dataGridView1.Rows[i].Cells[5].Value.ToString();
              string amt = dataGridView1.Rows[i].Cells[6].Value.ToString();

              float a = float.Parse(mrp);
              float b = float.Parse(dis);

              float rate2 = a - b;
              dataGridView1.Rows[i].Cells[5].Value = rate2;

              float a1 = float.Parse(quantity);
              //float b1 = float.Parse(rate2);

              float amount = a1 * rate2;
              float total = 0;
              dataGridView1.Rows[i].Cells[6].Value = amount;
              for (int i4 = 1; i4 < dataGridView1.Rows.Count; i4++)
              {

                  float amt1 = float.Parse(dataGridView1.Rows[i4 - 1].Cells[6].Value.ToString());
                  total = total + amt1;

              }
              
              label1.Text = total.ToString();


        

          }

  }
Posted
Updated 18-Apr-12 15:33pm
v4

1 solution

You should not edit another cell using this event handler method. If cell2 edits cell1 then cell1's end_edit will be called which will call cell2's end_edit -- indefinitely. However, it will not hang because editing a cell to the same value will not trigger end_edit.
 
Share this answer
 
Comments
ankitaverma 20-Apr-12 8:26am    
So what should i do?

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