如何将所有查询结果附加到datagrigridView?

时间:2017-06-07 16:47:02

标签: c#

所以基本上我试图将foreach循环查询的所有结果附加到datagrid2,但我只是不明白。谁能指出我正确的方向?

foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            if (row != null)
            {
                string col1 = row.Cells["F2"].Value.ToString();
                string col2 = row.Cells["F3"].Value.ToString();
                string col3 = row.Cells["F9"].Value.ToString();
                string like = "'" + "%" + col3 + col1 + col2 + "%" + "'";

                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();

                    string query = string.Format("query" + like);
                    using (SqlCommand cmd = connection.CreateCommand())
                    {
                        cmd.CommandText = query;
                        cmd.CommandType = CommandType.Text;
                        SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);

                        try
                        {
                            DataTable dt = new DataTable();
                            dAdapter.Fill(dt);
                            dataGridView2.DataSource = dt;
                        }
                        catch (Exception exceptionObject)
                        {
                            MessageBox.Show(exceptionObject.ToString());
                        }
                    }
                }
            }
        }

0 个答案:

没有答案