2014-02-12 75 views
0

我有兩個表:請求和項目。 此MouseUp事件適用於WinForm DatagridRow。 如何將此事件用於WPF DataGrid?在wpf DataGrid中使用MouseUp事件

private void gridRequests_MouseUp(object sender, MouseEventArgs e) 
     { 
      DataTable dt = new DataTable(); 
      DataBase db = new DataBase(); 
      try 
      { 
       s = gridRequests[0, gridRequests.CurrentRow.Index].Value.ToString(); 
       dt = db.MySelect("select * from Items where RequestID='" + s + "'"); 
       gridItems.DataSource = dt; 
       gridItems.Columns[0].HeaderText = "No."; 
       gridItems.Columns[1].HeaderText = "Item Code"; 
       gridItems.Columns[2].HeaderText = "Item Name"; 

      } 
      catch { } 
     } 

我在WPF使用:

private void requestsDataGrid_MouseUp(object sender, MouseButtonEventArgs e) 
     { 
      DataTable dt = new DataTable(); 
      DataBase db = new DataBase(); 
      s = requestsDataGrid[0, requestsDataGrid.CurrentRow.Index].Value.ToString(); 
      dt = db.MySelect("select * from Items where RequestID='" + s + "'"); 
     } 

CurrentRow或CurrentCell是我的錯誤點。

+1

OMG,UI中的sql查詢和空的catch塊,是在生產中嗎? ahhh和連接sql查詢...你聽說過關於sql注入嗎? –

+0

我正在使用sql查詢。 – mhshojaee

+0

你可以使用相同的MouseUp事件,有什麼問題? –

回答

0
+0

我用過:s = requestsDataGrid.SelectedIndex.ToString(); dt = db.MySelect(「select * from Items where RequestID ='」+ s +「'」); requestsDataGrid.DataContext = dt;當我點擊請求行時,所有行都會隱藏! – mhshojaee

+0

嘗試SelectedItem –

0

謝謝阿爾森, 我用這個:

private void requestsDataGrid_MouseUp(object sender, MouseButtonEventArgs e) 
     { 
      DataTable dt = new DataTable(); 
      DataBase db = new DataBase(); 

       string s = requestsDataGrid.SelectedItem.ToString(); 

       dt = db.MySelect("select * from Items where RequestID='" + s + "'"); 
       gridItems.DataContext = dt; 
       gridItems.Columns[0].HeaderStringFormat = "No"; 
       gridItems.Columns[1].HeaderStringFormat = "Item Code; 
       gridItems.Columns[2].HeaderStringFormat = "Item Name"; 

     } 

這種常見的錯誤:轉換爲varchar值 'System.Data.DataRowView' 當轉換失敗到數據類型int。