I have a ListBox and 2 DataGridViews.
I have an Object which has
[Serializable]
public class Process
{
...
public string ProcessName { get; set; }
public List<Step> Steps { get; set; }
}
[Serializable]
public class Step
{
...
public List<StepEvent> StepEvents { get; set; }
}
[Serializable]
public class StepEvent
{
...
public int EventId { get; set; }
}
I can connect the listbox and the first grid, but I can not hook up the second grid. I don't think I can use a DataRelation because its an object not a DataTable
Code So Far
List<Process> processes = new XMLHelper().CreateObject<List<Process>>(GetXmlProcess());
listProcess.DataSource = processes;
listProcess.DisplayMember = "ProcessName";
listProcess.ValueMember = "ProcessName";
dgSteps.DataSource = processes;
dgSteps.DataMember = "Steps";
//List<Step> steps = (from p in processes
// from st in p.Steps
// select st).ToList();
dgStepEvents.DataSource = processes;
dgStepEvents.DataMember = "Steps.StepEvents";
No matter what I make dgStepEvents.DataMember it gives me an error. As you can see I tried LINQ but then [steps] is a new object and doesn't link.
2nd related question: I have been reading a lot of BindableSource but I 开发者_如何学编程figured I didn't need something like that since the listview and the datagrid are already in sync.
You could do it the old fashion way of hooking up the SelectIndexChange Event
private void listProcess_SelectedIndexChanged(object sender, EventArgs e)
{
Process p = listProcess.SelectedItem as Process;
dgSteps.DataSource = p.Steps;
}
private void dgSteps_SelectionChanged(object sender, EventArgs e)
{
List<Step> steps = dgSteps.DataSource as List<Step>;
dgStepEvents.DataSource = steps[dgSteps.CurrentRow.Index].StepEvents;
}
精彩评论