I have a List of an object called "Reasons" that contains two properties "Code" & "Text". I want to use this to fill a UserControl of a Gridview. However, I don't understand how to link the gridview to the List of Reasons and actually set which data to use from the object.
I would assume the approach would be to set the datasource to the List, however, that is not working as it does seem populate the gridview with any rows. Is there a better approach to this problem?
I am assuming you're doing this in winform C#. It should be fairly similar in C# codebehind for asp.net. Here's some sample code you can easily customize to your obj type:
/// <summary>
/// The test class for our example.
/// </summary>
class TestObject
{
public string Code { get; set; }
public string Text { get; set; }
}
void PopulateGrid()
{
TestObject test1 = new TestObject()
{
Code = "code 1",
Text = "text 1"
};
TestObject test2 = new TestObject()
{
Code = "code 2",
Text = "text 2"
};
List<TestObject> list = new List<TestObject>();
list.Add(test1);
list.Add(test2);
dataGridView1.DataSource = list;
dataGridView1.DataBind();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With