Click here to Skip to main content
15,896,201 members

Comments by xzmilan (Top 1 by date)

xzmilan 20-Feb-16 14:31pm View    
This worked for me. I looked for a solution to my datagridview to perform my SQL to only the selected items. Here is my code..

private void btn_groups_click(object sender, EventArgs e)
{
Bonus.Depts deptfrm = new Depts();
SqlConnection con1 = new SqlConnection();
con1.ConnectionString = @"Data Source=\\SQLEXPRESS;Initial Catalog=...;Integrated Security=True";


string insertcmd = (@" --Truncate Table dbo.deptsel_tbl
Declare @group_ID as int
set @group_ID = (select id from tbl)
INSERT INTO [dbo].[...]
(..,EmployeeDeptCode)
VALUES(.,@EmployeeDeptCode)");

SqlCommand cmd = new SqlCommand(insertcmd, con);


for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
if ((bool)dataGridView1.Rows[i].Cells[0].FormattedValue) //this is where i added this solution
{
cmd.Parameters.Add("@EmployeeDeptCode", dataGridView1.Rows[i].Cells[1].Value);
con.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
con.Close();
}
}
}