I have a List that I am binding to a GridView, therefore my GridView will have only one column of these string values. I want to have a proper header-text for this column. Please help me. See what I have attempted to do:
<asp:GridView ID="GridView1" runat="server" Width="95%">
<Columns>
<asp:BoundField HeaderText="My List" />
</Columns>
</asp:GridView>
And in code behind:
List<string> myList = new List<string>();
:
:
// code to populate myList
:
:
GridView1.DataSource = myList;
GridView1.DataBind();
When I run this code, I get two columns in the GridView. First column having header-text as “My List” and having blank rows, whereas the second column having header-text as “Item” and having rows with myList values. I want to have only one column in my GridView having header-text as “My List” and rows with the values of the myList object.
Thanks
Or you can do it like this:
Aspx:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="test" HeaderText="Text" />
</Columns>
</asp:GridView>
Code:
var ls=new List<string>();
ls.Add("Test");
ls.Add("Test2");
gv.DataSource=ls.Select (l =>new{test=l});
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