0

so I have managed to save a whole datagridview into my database via WCF, but now i only want to save the selected rows which is declared by a CHeck box. Can anyone help?

Here is my code snippet of saving the whole datagridview, but now i need to save only the selected items:

   for (  int i = 0; i < dataGridView2.Rows.Count; i++)
            {
                ServiceReference6.ProcessDetails det2 = new ServiceReference6.ProcessDetails();

                det2.Item1 = dataGridView2.Rows[i].Cells["Item1"].Value.ToString();
                det2.Item2 = dataGridView2.Rows[i].Cells["Item2"].Value.ToString();
                obj6.InsertProcesses(det2);

            }

And here is my WCF code:

public string InsertProcesses(ProcessDetails proDetails)
    {
        string Message;
        SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=WCFTest;Integrated Security=True;Pooling=False");
        con.Open();

        SqlCommand cmd = new SqlCommand("insert into SaveProcesses(Item1, Item2)values(@Item1, @Item2)", con);
        cmd.Parameters.AddWithValue("@Item1", proDetails.Item1);
        cmd.Parameters.AddWithValue("@Item2", proDetails.Item2);
        int result = cmd.ExecuteNonQuery();

        if (result == 1)
        {
            Message = proDetails.Item1+ "Details accepted";
        }
        else
        {
            Message = proDetails.Item1+ "Details not accepted";
        }
        con.Close();
        return Message;
    }
Andrew
  • 9
  • 4
  • 10
  • Possible duplicate http://stackoverflow.com/questions/5280655/get-the-selected-rows-from-a-datagridview – Pawan Jun 17 '13 at 10:40

1 Answers1

0
  for (  int i = 0; i < dataGridView2.Rows.Count; i++) 
      { 
        CheckBox chk = new CheckBox();

        chk = (CheckBox)(dataGridView2.Rows[iCount].FindControl("checkbox1"));

        if (chk.Checked)
        {
                 "do your code here"
        }
      }

Please change the name of controls as per your req.

Utkarsh
  • 127
  • 6
  • HI there, thanks for the help, however the code gives me an error of the name "iCount" does not exist in the current context" Am i meant to replace it with something else? Thanks – Andrew Jun 17 '13 at 10:50
  • Ah ok thanks, no wonder why the error chucked up. Any other ideas? – Andrew Jun 17 '13 at 10:51
  • Refer this link . http://stackoverflow.com/questions/13338837/check-uncheck-a-checkbox-on-datagridview – Utkarsh Jun 17 '13 at 10:53