我正尝试从以下代码访问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;
}
但似乎无法调用该方法(我得到Argument错误)。我对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 :(得分:1)
我认为这是一个事件处理程序。如果您使用的是使用标准基类但使用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);