Populate a UserControl Gridview with a List of Objects
I have a List of an object called "Reasons" that contain开发者_Python百科s 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();
}
You're correct in your assumption that you set the DataSource to the List. You also need to remember to call the GridView.DataBind() command once you've set the DataSource to your list.
i.e.:
List<Reasons> lReasons = Assign List Here....
gvReasons.DataSource = lReasons;
gvReasons.DataBind();
Also, If you want to setup your two properties as the columns in your GridView, assign them like so (assuming you're working with WebForms here and not WinForms):
<asp:GridView ID="gvReasons" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Code" />
<asp:BoundField DataField="Text" />
</Columns>
</asp:GridView>
Or you can specify AutoGenerateColumns="true" and let the framework generate the columns itself.
I don't really understand the sentence, "I want to use this to fill a UserControl of a Gridview."? However, if the question you are asking is how to bind a GridView to a list of your Reasons objects then this should work:
.aspx Page
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="true">
</asp:GridView>
</div>
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
List<Reasons> reasonsList = new List<Reasons>()
{
new Reasons() { Code = "Code 1", Text = "Text 1" },
new Reasons() { Code = "Code 2", Text = "Text 2" },
new Reasons() { Code = "Code 3", Text = "Text 3" },
};
GridView1.DataSource = reasonsList;
GridView1.DataBind();
}
public class Reasons
{
public string Text { get; set; }
public string Code { get; set; }
}
While binding to datasource as suggested in other answers will work for a basic grid, I don't like that approach b/c it makes paging and sorting more difficult. You'll have fewer headaches in the long run if you bind an ObjectDataSource to a business tier as suggested here:
populate gridview via code-behind or markup DataSource?
精彩评论