2011-10-20 33 views
1

我有一個包含多個複選框的datagridview。當Finished複選框被選中時,我需要執行linq代碼來更新特定的表。如何找出特定複選框是否髒,以及我在哪裏編寫代碼以傳遞需要傳遞給表的值。請注意,它不是datagridview基於的表。如何在選定某個複選框後執行代碼

謝謝。

編輯:

private void propertyInformationDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)propertyInformationDataGridView.Rows[e.RowIndex].Cells[3]; 

     DataGridViewRow row = propertyInformationDataGridView.Rows[e.RowIndex] as DataGridViewRow; 

     System.Data.DataRowView SelectedRowView; 
     newCityCollectionDataSet.PropertyInformationRow SelectedRow; 

     SelectedRowView = (System.Data.DataRowView)propertyInformationBindingSource.Current; 
     SelectedRow = (newCityCollectionDataSet.PropertyInformationRow)SelectedRowView.Row; 

     if (Convert.ToBoolean(checkCell.EditedFormattedValue) == true) 
     { 
      DataClasses1DataContext dc = new DataClasses1DataContext(); 

      var matchedCaseNumber = (from c in dc.GetTable<PropertyInformation>() 
            where c.CaseNumberKey == SelectedRow.CaseNumberKey 
            select c).SingleOrDefault(); 

      reportsSent newReport = new reportsSent(); 
      newReport.CaseNumberKey = SelectedRow.CaseNumberKey; 
      dc.reportsSents.InsertOnSubmit(newReport); 
      dc.SubmitChanges(); 

     } 
    } 

我需要在某些時候EndEdit中是這個問題?

+2

因此對每一行正確 「已完成」 複選框? – KreepN

+0

讓我知道如果你還需要更多的代碼幫助,我會一整天。 – KreepN

回答

1

這是我的一些代碼,所有你需要做的是爲您的datagridview一個「CellContentClick」事件。

最簡單的方法是選擇Datagridview,轉到屬性並單擊閃電。向下滾動到「CellContentClick」並雙擊空白框。這將自動生成您需要將以下代碼粘貼到的方法。

確保您將我的「CustomersDataGridView」實例重命名爲任何您所命名的名稱,intellisense應該突出顯示您需要替換的紅色無效代碼。

此外,您在checkCell聲明中看到的「9」需要更改爲您的「完成」複選框的索引。如果它位於左側的第3個單元格中,則將其設置爲2而不是9,因爲索引是基於0的。

EDITTED修正意見:

private void CustomersDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 

    if (e.ColumnIndex.ToString() == "9") 
    { 
     DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)CustomersDataGridView.Rows[e.RowIndex].Cells[9]; 
     DataGridViewRow row = CustomersDataGridView.Rows[e.RowIndex] as DataGridViewRow; 

     if (Convert.ToBoolean(checkCell.EditedFormattedValue) && CustomersDataGridView.IsCurrentCellDirty) 
     { 
      //Do Work here. 
      var z = row.Cells[0].Value; // Fill in the brackets with the column you want to fetch values from 
      //z in this case would be the value of whatever was in the first cell in the row of the checkbox I clicked 
     } 
    } 
} 
+0

非常感謝@KreepN。我猜測,我可以使用if(checkcell = -1)然後我需要的代碼等等。因爲我沒有定義那些布爾值,而是INT,我用-1爲真,0爲假? – korrowan

+0

老實說,由於Convert.ToBoolean,我認爲**你應該可以毫無問題地使用我的代碼,因爲它檢查複選框是否被選中(不是與它相關的值)並傳入if條件如果它被檢查。如果它不起作用,只需使用你選擇的正確的if(checkcell = -1)或if(checkcell.toString()== -1)方法,你應該可以。 – KreepN

+0

我會使用你的代碼,然後作爲它的一點點清潔。最後一個問題只是爲了確定。這隻會在該複選框CHANGES時執行。我只是想確保它將一個值放入一個向客戶端發送報告的表中,因此重要的是他們只能得到一次報告=)。 – korrowan

1

你可以做到這一點在的CheckedChanged-事件

private void checkBox1_CheckedChanged(object sender, EventArgs e) 
{ 
    // Do what you have to do... 
} 
+2

您可能想要包括如何將事件連接到方法的示例。我懷疑他會問他是否知道如何做到這一點。 – JSWork

+1

@JsWOrk這正是我需要理解的=)。 – korrowan

+0

有兩種方法將事件連接到複選框。 1)您可以在設計器中的Visual Studio中通過單擊複選框,將屬性窗口切換到事件選項卡,搜索CheckedChanged條目並輸入事件處理程序的名稱。按下Enter後,Visual Studio將切換到顯示事件方法的代碼頁。 2)拿到複選框的窗口的構造函數。在「InitializeComponents();」之後插入下面的行:「checkBox.CheckedChanged + = checkBox1_CheckedChanged;」在構造函數之後添加上面提到的代碼片段。 – Fischermaen

相關問題