2014-02-05 92 views
1

我想從下面的代碼訪問的數據存儲在ResultID:訪問數據

public class ResultPanelEventArgs : EventArgs 
{ 
    private string stringResultId = string.Empty; 
    private int intRowIndex = -1; 
    private string stringAnalysisName = string.Empty; 
    private DateTime dateTimeAnalysisDateTime; 

    /// <summary> 
    /// Gets or sets the row index of selected result. 
    /// </summary> 
    public int RowIndex 
    { 
     get { return intRowIndex; } 
     set { intRowIndex = value; } 
    } 

    /// <summary> 
    /// Gets or sets the result id of selected result. 
    /// </summary> 
    public string ResultId 
    { 
     get { return stringResultId; } 
     set { stringResultId = value; } 
    } 

    /// <summary> 
    /// Gets or sets the date and time of the selected result. 
    /// </summary> 
    public DateTime AnalysisDateTime 
    { 
     get { return dateTimeAnalysisDateTime; } 
     set { dateTimeAnalysisDateTime = value; } 
    } 

    /// <summary> 
    /// Gets or sets the name of the sample as Analysis name. 
    /// </summary> 
    public string AnalysisName 
    { 
     get { return stringAnalysisName; } 
     set { stringAnalysisName = value; } 
    } 
}; 

我要拉信息的方法:

private string GetResultID(object sender, ResultPanelEventArgs e) 
    {   


     string resultID = string.Empty; 
     resultID = e.ResultId.ToString(); 
     return resultID;   

    } 

,但似乎無法調用該方法(我得到參數錯誤)。我對c#有點新,並且從來沒有用過EventArgs,所以我甚至不知道這是否可行。有關如何訪問此處存儲的數據的任何建議?

每請求,這裏是出現填充ResultId三種方法:

private ResultPanelEventArgs GetDataForTheGivenRowIndex(int intRowIndex) 
    { 
     ResultPanelEventArgs resultPanelEventArgs = new ResultPanelEventArgs(); 
     resultPanelEventArgs.RowIndex = intRowIndex; 
     try 
     { 
      if (intRowIndex >= 0 && intRowIndex < dataGridViewResultView.Rows.Count) 
      { 
       object objectResultId = null; 
       if (dataGridViewResultView.Columns.Contains("ColumnResultId") == true) 
       { 
        objectResultId = dataGridViewResultView.Rows[intRowIndex].Cells["ColumnResultId"].Value; 
       } 
       else 
       { 
        objectResultId = dataGridViewResultView.Rows[intRowIndex].Tag; 
       } 
       if (objectResultId != null) 
       { 
        resultPanelEventArgs.ResultId = objectResultId.ToString(); 
       } 

       object objectAnalysisName = null; 
       if (dataGridViewResultView.Columns.Contains("ColumnAnalysis") == true) 
       { 
        objectAnalysisName = dataGridViewResultView.Rows[intRowIndex].Cells["ColumnAnalysis"].Value; 
       } 
       else 
       { 
        objectAnalysisName = dataGridViewResultView.Rows[intRowIndex].Tag; 
       } 
       if (objectAnalysisName != null) 
       { 
        resultPanelEventArgs.AnalysisName = objectAnalysisName.ToString(); 
       } 

       object objectAnalysisDateTime = null; 
       if (dataGridViewResultView.Columns.Contains("ColumnDate") == true) 
       { 
        objectAnalysisDateTime = dataGridViewResultView.Rows[intRowIndex].Cells["ColumnDate"].Value; 
       } 
       else 
       { 
        objectAnalysisDateTime = dataGridViewResultView.Rows[intRowIndex].Tag; 
       } 
       if (objectAnalysisDateTime != null) 
       { 
        resultPanelEventArgs.AnalysisDateTime = 
          Utilities.ConvertStringToDateTime(objectAnalysisDateTime.ToString()); 
       } 
      } 
     } 
     catch 
     { 
      resultPanelEventArgs = null; 
      //Nothing to do 
     } 
     return resultPanelEventArgs; 
    } 

private ResultPanelEventArgs GetDataForTheGivenResultID(string stringResultId) 
    { 
     ResultPanelEventArgs resultPanelEventArgs = null; 
     try 
     { 
      foreach (DataGridViewRow dataGridViewRow in dataGridViewResultView.Rows) 
      { 
       if (dataGridViewRow != null) 
       { 
        if (dataGridViewRow.Index >= 0) 
        { 
         if (dataGridViewRow.Cells["ColumnResultId"] != null) 
         { 
          if (dataGridViewRow.Cells["ColumnResultId"].Value.ToString() == stringResultId) 
          { 
           //Create the ResultPanelEventArgs 
           object objectResultId = dataGridViewRow.Cells["ColumnResultId"].Value; 
           object objectAnalysisName = dataGridViewRow.Cells["ColumnAnalysis"].Value; 
           object objectAnalysisDateTime = dataGridViewRow.Cells["ColumnDate"].Tag; 
           resultPanelEventArgs = new ResultPanelEventArgs(); 

           if (objectResultId != null) 
           { 
            resultPanelEventArgs.RowIndex = dataGridViewRow.Index; 
            resultPanelEventArgs.ResultId = objectResultId.ToString(); 
            resultPanelEventArgs.AnalysisName = objectAnalysisName.ToString(); 
            resultPanelEventArgs.AnalysisDateTime = (DateTime)objectAnalysisDateTime; 
           } 

           dataGridViewRow.Selected = true; 
           break; 
          } 
         } 
        } 

       } 

      } 
     } 
     catch 
     { 
      resultPanelEventArgs = null; 
      //Nothing to do 
     } 
     return resultPanelEventArgs; 
    } 

private void dataGridViewResultList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) 
    { 
     try 
     { 
      if (e.RowIndex >= 0) 
      { 
       this.result = this.GetResult(e.RowIndex); 

       ResultPanelEventArgs resultPanelEventArgs = new ResultPanelEventArgs(); 
       resultPanelEventArgs.ResultId = this.result.Id.ToString(); 
       resultPanelEventArgs.RowIndex = this.dataGridViewResultList.SelectedRows[0].Index; 

       if (this.DoubleClicked != null) 
       { 
        this.DoubleClicked(sender, resultPanelEventArgs); 
       } 

       this.DialogResult = DialogResult.OK; 
      } 
     } 
     catch (Exception ex) 
     { 
      UICommon.LogError(ex); 
     } 
    } 
+0

你能同時提供你在哪裏填充事件參數的ResultID財產的例子嗎? – neontapir

+1

neontapir,我添加了你要求的方法。 – willkk

回答

0

我想這是一個事件處理程序。如果您使用的是使用標準基類的事件,但使用ResultPanelEventArgs觸發它,則需要使用基類EventArgs處理它,然後將它們轉換爲正確的類型。

private void HandleResultID(object sender, EventArgs args) 
{   
    var e = (ResultPanelEventArgs) args; 
    var resultID = e.ResultId.ToString(); 


    // Now do something with the ID. You cannot return it, because this is handling the click event. 

} 

更新:要訂閱事件(添加事件處理程序的事件):

this.DoubleClicked += new EventHandler(HandleResultID); 
+0

但是我需要什麼參數來調用該方法? this.GetResultId()不接受0個參數。 – willkk

+0

@alex是的,我忽略了這一點。 willkk我想這不是一個事件處理程序,如果你想調用它。 –

+0

@willkk我看到這是一個事件處理程序。返回類型必須是無效的。 –