private void ShowIterationSelection()
{
IterationForm iterationForm = new IterationForm(lblProjectID.Text);
iterationForm.ShowDialog();
}
当我显示对话框时,它会使用ProjectID显示与项目相关的迭代列表:
public partial class IterationForm : Form
{
public IterationForm(string projectID)
{
InitializeComponent();
LoadIterationsForProject(projectID);
}
private void LoadIterationsForProject(string projectID)
{
IterationRepository iterationRepo = new IterationRepository();
Int64 ID = Convert.ToInt64(projectID);
dgvIterations.DataSource = iterationRepo.FindAllIterations().Where(i => i.IDProject == ID).Select(i => new { Codigo = i.ID, Descripcion = i.Description, Inicio = i.StartDate, Fin = i.EndDate });
}
}
我的问题是,如何从datagrid视图中捕获所选值并将其传递给我的调用表单以使用select IterationID打开另一个表单?
感谢您的帮助。
所以可能是这样的:
private void ShowIterationSelection()
{
IterationForm iterationForm = new IterationForm(lblProjectID.Text);
var result = iterationForm.ShowDialog();
showTheThing(result); //this uses the iterationID
}
答案 0 :(得分:3)
ShowDialog()的结果是DialogResult类型的枚举。 通常你会测试所显示的表格是否有“Ok”的DialogResult。 您可以在表单中设置一个属性,用于保存结果,例如在属性“ResultProperty”中,如果您想在表单关闭后阅读它,请使用以下内容:
IterationForm iterationForm = new IterationForm(lblProjectID.Text);
if ( iterationForm.ShowDialog() == DialogResult.Ok )
{
// read result
var result = iterationForm.ResultProperty
}
else
{
// user has not clicked ok - do some other stuff here
}
在您显示的表单中,您可以在用户应该单击的按钮上放置一个事件处理程序,以便接受并添加如下代码:
private void Accept_Click(object sender, EventArgs e)
{
this.ResultProperty = someResult;
this.DialogResult = DialogResult.Ok;
this.Close();
}
如果用户点击了取消按钮,您可以将属性设置为:
private void Decline_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
答案 1 :(得分:1)
只需在迭代表单上创建一个可以从主表单访问的公共属性。
public partial class IterationForm : Form
{
// set this to the selected object (change to w/e type you need)
public object SelectedObject{get; private set;}
public IterationForm(string projectID)
{
InitializeComponent();
LoadIterationsForProject(projectID);
}
private void LoadIterationsForProject(string projectID)
{
IterationRepository iterationRepo = new IterationRepository();
Int64 ID = Convert.ToInt64(projectID);
dgvIterations.DataSource = iterationRepo.FindAllIterations().Where(i => i.IDProject == ID).Select(i => new { Codigo = i.ID, Descripcion = i.Description, Inicio = i.StartDate, Fin = i.EndDate });
}
}
然后你可以这样做:
private void ShowIterationSelection()
{
IterationForm iterationForm = new IterationForm(lblProjectID.Text);
var result = iterationForm.ShowDialog();
showTheThing(iterationForm.SelectedObject); //this uses the iterationID
}
答案 2 :(得分:0)
首先为您的IterationForm提供所选ID的属性:
public int SelectedID { get; set; }
当您的用户从数据网格中选择一行时,您需要设置selectedID属性。我会把这一点留给你。
现在,为IterationForm的Closed
事件添加处理程序:
private void ShowIterationSelection()
{
IterationForm iterationForm = new IterationForm(lblProjectID.Text);
iterationForm.Closed += new EventHandler(iterationForm_Closed)
iterationForm.ShowDialog();
}
void iterationForm_Closed (object sender, EventArgs e)
{
IterationForm form = (IterationForm)sender;
showTheThing(form.SelectedID);
}
答案 3 :(得分:0)
ShowDialog()
仅返回DialogResult。
但是,您可以做的是在收到成功的DialogResult时将iterationID作为IterationForm上的公共属性进行查询。类似的东西:
var iterationForm = new IterationForm(lblProjectID.Text);
if (iterationForm.ShowDialog() == DialogResult.OK)
{
// do something with iterationForm.SelectedIterationId
}