Click here to Skip to main content
15,923,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a datagridview in a c# form contains datas. I want to select some of the rows (using select check boxes). And there is an sql table which has thousands of rows. I need to filter only the values which were selected on gridview and insert into a temp table so i can use it for reporting sections. how can i do this.
e.g.
dataGridview1 Column
cat
dog
horse
lion
fox
now i am selecting the checkboxes for cat and dog.
Sql data table view is like this.
name name code
cat cat001
dog dg001
horse hrs001
crow crw001
cat cat002
lion ln001
cat cat003
cat cat004
dog dog002
so I've selected cat and dog in the gridview. So i want a filtered temporary table with datas like this on a button click.
name name code
cat cat001
dog dg001
cat cat002
cat cat003
cat cat004
dog dog002
So i am trying to pass this using a sql query but failed. How can i do this.

What I have tried:

SqlConnection conn1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=acc;Integrated Security=True");
SqlCommand cmd1 = new SqlCommand(@"select db.date,db.type,db.refno,db.itmcod,db.qty,db.cuscod, db.cstcod,cus.cusnam INTO ##wec from fstktxn as db INNER JOIN fcustomer as cus on db.cuscod = cus.cuscod where itmcod = "dataGridView1.Rows[j].Cells["title"].Value"'", conn1);
conn1.Open();
cmd1.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn1);
bulkCopy.DestinationTableName = "##tmp1";
bulkCopy.WriteToServer(dt);
conn1.Close();
Posted
Updated 27-Jun-16 20:47pm
Comments
[no name] 27-Jun-16 19:50pm    
I think, you should use OR in your sql. Example: .....WHERE name='cat' OR name='dog'..
Mohamed Shakir 28-Jun-16 1:25am    
we can't surely assign cat or dog because a users selection will be different
Azziet 28-Jun-16 2:35am    
convert your values as comma separated then use IN clause
Arasappan 28-Jun-16 2:35am    
Please show ur checkbox design..

1 solution

C#
string name = string.Empty;
      for (int i = 0; i < dgvSupplier.Rows.Count; i++)
      {
        if (Convert.ToBoolean(dgvSupplier.Rows[i].Cells["CheckBoxColumn"].Value))
          name += "'" + Convert.ToString(dgvSupplier.Rows[i].Cells["Title"].Value) + "',";
      }


Now change where clause to "select db.date,db.type,db.refno,db.itmcod,db.qty,db.cuscod, db.cstcod,cus.cusnam INTO ##wec from fstktxn as db INNER JOIN fcustomer as cus on db.cuscod = cus.cuscod where itmcod IN (" + name + ")", conn1);
 
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