Reputation: 168
I have a List ComparedItem which I want to display in datagridview dataGridViewCompare. Some properties of ComparedItem are Lists too, and dataGridViewCompare will not display these columns.
Actually I don't want to display the list in a cell, but I want to show the content of this cell in a textbox when the user select a specific row.
public class ComparedItem
{
public ElementItem SourceElement { get; private set; }
public ElementItem TargetElement { get; private set; }
public bool HasErrors { get; set; }
public List<ErrorType> ErrorTypes { get; set; }
public string FriendlyErrorNames { get; set; }
public List<string> DetailsInconsistencyError { get; set; }
public string DetailsSpecialCharsError { get; set; }
public string DetailsTextError { get; set; }
public ComparedItem(ElementItem source, ElementItem target)
{
SourceElement = source;
TargetElement = target;
DetailsInconsistencyError = new List<string>();
DetailsInconsistencyError.Add("Test"); // <- Temp
ErrorTypes = new List<ErrorType>();
FriendlyErrorNames = String.Empty;
}
}
In my main form:
List<ComparedItem> = new List<ComparedItem> comparedItems;
// fill list in some other code...
dataGridViewCompare.DataSource = comparedItems;
dataGridViewCompare shows all the lines I expect, but only the columns
are displayed.
are lost.
Is it not possible to hold Lists in datagridview cells?
Upvotes: 2
Views: 180
Reputation: 54433
I don't think you can display a List<T>
in a DGV cell.
It is in the DataSource
however, so you can pull out the values and display them in the TextBoxes
as you want to.
Here is how to access the item in your list the (first selected) row is bound to:
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count <= 0) return;
ComparedItem ci = (dataGridView1.SelectedRows[0].DataBoundItem as ComparedItem);
if (ci != null)
{
textBox1.Text = someStringRepresentation(ci.ErrorTypes);
textBox2.Text = someStringRepresentation(ci.DetailsInconsistencyError);
}
}
Depending on the SelectionMode
you may want to add similar code to the CurrentCellChanged
event etc..
Upvotes: 2