I create a DataGridView with a CheckBox column so I can check one or the other row.
As data source of the DataGridView I pass a list of strings. When passing the list, I expected to get a datagridview that would show me as the first column, a checkbox column and the second column the strings of the list, but no, instead of the strings it shows me its length:
In the Length column I should (or would like) to display the strings passed through the list and not their length.
The way to generate the "Selection" column is as follows:
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.HeaderText = "Seleccion";
column.Name = "Seleccion";
column.TrueValue = true;
column.FalseValue = false;
column.IndeterminateValue = false;
column.ThreeState = false;
column.ValueType = typeof(Boolean);
dgRemotas.Columns.Add(column);
and to add the string list I do the following:
dgRemotas.DataSource = list;
list is defined as:
public List<string> list;
How can I display strings instead of their length?
By default it
DataGridView
has the propertyAutoGenerateColumns
set to true. Having that property turned on, itDataGridView
tries to get the properties of the objectsDataSource
you're passing to it. But, it turns out that itstring
only has one property, and that isLength
. That's why it shows you that column.To fix it, the first step is to disable
AutoGenerateColumns
:Then, you must add the second column that must show those
string
of your list:Trust him
DataPropertyName
. We must passDataSource
a list of objects that have a property calledValor
, so we do the following:In this way, we generate a list of objects with a property called
Valor
that contains the text to display.By default
DataGridView
it will look at the properties of the container objects in the list. Forstring
there is only one property -length
. So, you need a wrapper for a string, like this:Next, you bind the object
List<StringValue>
to theDataGridView
.NOTE: Answer obtained from this link .