我有2個GridView
控件。我需要將選中的行從一個GridView添加到第二個GridView。 (我通過點擊直接到GridView從GridView中選擇一行。)從一個gridview添加選定的行到另一個
這是我的代碼,但它將所有數據從gridview1複製到gridview2。我只需要選定的行。
private void button6_Click(object sender, EventArgs e)
{
DataGridViewColumn newCol = null;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
newCol = new DataGridViewColumn(col.CellTemplate);
newCol.HeaderText = col.HeaderText;
newCol.Name = col.Name;
dataGridView2.Columns.Add(newCol);
}
dataGridView2.RowCount = dataGridView1.RowCount;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
dataGridView2.Rows[row.Index].Cells[col.Name].Value = row.Cells[col.Name].Value;
}
}
我填充dataGridView1
與一個DataTable:
dataGridView1.DataSource
後
SqlConnection con = new SqlConnection(@"Data Source=AFZAL\SQLEXPRESS;Initial Catalog=GIMS_LabInfo;Integrated Security=True");
con.Open();
SqlCommand sc = new SqlCommand("SELECT PROFCODE,PROFNAME FROM PROFNAMES$ WHERE (PROFNAME LIKE '" + textBox1.Text + "%') AND PROFCODE NOT IN (SELECT PROFCODE FROM MAP) ORDER BY Profname desc ", con);
sc.ExecuteNonQuery();
SqlDataAdapter sda = new SqlDataAdapter(sc);
DataTable data = new DataTable();
sda.Fill(data);
dataGridView1.DataSource = data;
你能否提供一些你現有的代碼? – 2014-09-01 15:23:56
請參閱上面我的代碼。 – 2014-09-01 15:31:32
它將網格視圖1中的所有數據複製到網格視圖2,但我只需要選定的行。 – 2014-09-01 15:32:36