How to Bind a List<String> to a Datagridview Control

How to bind a List string to a DataGridView control?

Thats because DataGridView looks for properties of containing objects. For string there is just one property - length. So, you need a wrapper for a string like this

public class StringValue
{
public StringValue(string s)
{
_value = s;
}
public string Value { get { return _value; } set { _value = value; } }
string _value;
}

Then bind List<StringValue> object to your grid. It works

How to bind a List string[] to a DataGridView control?

Try this:

dataGridView.DataSource = userNphotoValues
.Select(arr => new { UserName = arr[0], PhotoPath = arr[1] })
.ToArray();

c# - Add list of strings to the DataGridView when the DataSource is already bonded to DataGridView

Finally I figured out the best solution by myself.

In User form, declared a global List<string> variable

// Global Declaration    
private static List<string> AssignedRoleList { get; set; }

Then assigned the existed user roles to this global variable AssignedRoleList and bounded the user roles data to DataGridView. Below is the code.

AssignedRoleList = new List<string>();
AssignedRoleList = _userBll.ReadUserRoles(userId);
if (AssignedRoleList.Count > 0)
{
var source = new BindingSource {DataSource = AssignedRoleList.Select(x => new {Roles = x})};
dgvAssignedRoles.DataSource = source;
}

Then after I added the new SelectedRoleList to AssignedRoleList and bounded it to DataGridView.

NewRoleList = new List<string>();
// Assigned Old user roles to the new one.
if (AssignedRoleList != null) NewRoleList = AssignedRoleList;

foreach (var v in dataviewform.SeletedDataList)
{
bool status = true;
// Check if user already have the roles
if (dgvAssignedRoles.Rows.Cast<DataGridViewRow>().Any(row => Equals(row.Cells[0].Value, v)))
{
MessageBox.Show("Role already exist.");
status = false;
}
// Adding the new selected roles to the existence user role if not present
if (status)
NewRoleList.Add(v);
}
// Finally attaching the both new and old user roles to datagridview
var source = new BindingSource {DataSource = NewRoleList.Select(x => new {Roles = x})};
dgvAssignedRoles.DataSource = source;
}

How to bind list to dataGridView?

Use a BindingList and set the DataPropertyName-Property of the column.

Try the following:

...
private void BindGrid()
{
gvFilesOnServer.AutoGenerateColumns = false;

//create the column programatically
DataGridViewCell cell = new DataGridViewTextBoxCell();
DataGridViewTextBoxColumn colFileName = new DataGridViewTextBoxColumn()
{
CellTemplate = cell,
Name = "Value",
HeaderText = "File Name",
DataPropertyName = "Value" // Tell the column which property of FileName it should use
};

gvFilesOnServer.Columns.Add(colFileName);

var filelist = GetFileListOnWebServer().ToList();
var filenamesList = new BindingList<FileName>(filelist); // <-- BindingList

//Bind BindingList directly to the DataGrid, no need of BindingSource
gvFilesOnServer.DataSource = filenamesList
}

Is it possible to bind a List Object which contains List String to DataGrid

here you go

    <DataGrid ItemsSource="{Binding Students}"
AutoGenerateColumns="False"
CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"
Header="Name" />
<DataGridTextColumn Binding="{Binding Marks[0]}"
Header="Mark1" />
<DataGridTextColumn Binding="{Binding Marks[1]}"
Header="Mark2" />
<DataGridTextColumn Binding="{Binding Marks[2]}"
Header="Mark3" />
</DataGrid.Columns>
</DataGrid>

also change class as

    class Student
{
public String Name { get; set; }
public List<String> Marks { get; set; }
}

note I made public properties for your variables

result

sample

Variable Columns

you can have variable number of columns but not for each row

method1 hard-code maximum columns in xaml, so if a column does not have a value for that row it will remain empty

eg

grid sample 2

I have added another column to demonstrate clearly

<DataGridTextColumn Binding="{Binding Marks[3]}"
Header="Mark4" />

other approach involve to generate columns at run-time via code behind or via help of attached properties

c# WinForm DataGridView bind List in List

You can use CellFormatting event of DataGridView to provide a friendly representation of categories property:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
//Use zero based index of the cell that contains categories
var cell= 3;

if (e.ColumnIndex == cell && e.RowIndex >= 0)
{
var categories = (List<object>)dataGridView1.Rows[e.RowIndex].Cells[cell].Value;
e.Value = string.Join(", ", categories.Select(x => x.ToString()));
}
}

In this case, categories contains List of product categories names (string). So selecting ToString() is OK. But for a case of List<SomeClass>, you either should override ToString of SomeClass or select one of it's properties.

As another option you can shape the result of query using a new ViewModel or using anonymous type, but in the current situation which Product model doesn't belong to you and it has lot's of properties, previous solution is the most simple option.

Binding List T to DataGridView in WinForm

List does not implement IBindingList so the grid does not know about your new items.

Bind your DataGridView to a BindingList<T> instead.

var list = new BindingList<Person>(persons);
myGrid.DataSource = list;

But I would even go further and bind your grid to a BindingSource

var list = new List<Person>()
{
new Person { Name = "Joe", },
new Person { Name = "Misha", },
};
var bindingList = new BindingList<Person>(list);
var source = new BindingSource(bindingList, null);
grid.DataSource = source;


Related Topics



Leave a reply



Submit