2013-04-13 53 views
1

我使用定時器刷新datagridview。請每隔10秒鐘幫助我 datagridview刷新。我無法找到代碼中刷新或索爾蒂摹後的DataGridView選定的單元將保持請幫助我..如何在刷新datagridview後保留在同一個單元格中#

private void timer1_Tick(object sender, EventArgs e) 
    { 
     if (radioButton1.Checked == true) 
     { 
      SqlCommand cmd = new SqlCommand("SELECT (Cust_No) as [Customer ID],(Cust_surname) as [Surname], (Cust_fname) as [First Name], (Cust_mi) as [Middle Initial], (Cust_address) as [Address], (Cust_ContactNO) as [Contact Number], (Cust_Faxno) as [Fax Number], (Cust_Tin)as [TIN] FROM Customer WHERE cust_status like ('Active')"); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = conn; 
      da = new SqlDataAdapter(cmd); 
      ds = new DataSet("ds"); 
      da.Fill(ds); 
      ds.Tables[0].TableName = "table_mirror"; 
      dataGridView2.DataSource = ds.Tables["table_mirror"]; 

     } 
     else 
     { 
      SqlCommand cmd1 = new SqlCommand("SELECT (comp_no) as [Company ID],(comp_name) as [Company Name], (comp_contactperson) as [Contact Person], (comp_address) as [Address], (comp_contactno) as [Contact No], (comp_fax) as [Fax], (comp_Tin) as [Tin] FROM Company WHERE comp_status like ('Active')"); 
      cmd1.CommandType = CommandType.Text; 
      cmd1.Connection = conn; 
      da = new SqlDataAdapter(cmd1); 
      ds = new DataSet("ds"); 
      da.Fill(ds); 
      ds.Tables[0].TableName = "table_mirror"; 
      dataGridView2.DataSource = ds.Tables["table_mirror"]; 
     } 
    } 

回答

0

我認爲這幫助ü。

 Point[] oldSelectedCells = new Point[dataGridView1.SelectedCells.Count]; 
     for (int i = 0; i < dataGridView1.SelectedCells.Count; i++) 
     { 
      oldSelectedCells[i].X = dataGridView1.SelectedCells[i].ColumnIndex; 
      oldSelectedCells[i].Y = dataGridView1.SelectedCells[i].RowIndex; 
     } 

     // refresh datagridview 

     dataGridView1.CurrentCell = null; 
     foreach (Point p in oldSelectedCells) 
      dataGridView1[p.X, p.Y].Selected = true; 
+0

謝謝:)) –

+0

非常感謝你:) btw我只是一個初學者。 –

相關問題