1

我正在尝试从列表中设置网格标题值得到错误

你调用的对象是空的

我先检查它,但我将列表值转换为字符串,在调试时我可以在列表中找到值

代码如下,

        List<string> rows = new List<string>(
new string[] { "Item", "Quantity", "Price" });

                GdItemList.HeaderRow.Cells[0].Text = Convert.ToString(rows[0]);
                GdItemList.HeaderRow.Cells[1].Text = rows[1].ToString();
                GdItemList.HeaderRow.Cells[2].Text = rows[2].ToString();

GdItemList 网格视图就像,

<asp:GridView ID="GdItemList" runat="server" ShowHeaderWhenEmpty="True" CellPadding="4"
                    EmptyDataText="No Record Found" ForeColor="#333333" GridLines="None">
                    <AlternatingRowStyle BackColor="White" />
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>
4

3 回答 3

2
List<string> rows = new List<string>(
    new string[] { "Item", "Quantity", "Price" });

GdItemList.Columns[0].HeaderText = Convert.ToString(rows[0]);
GdItemList.Columns[1].HeaderText = rows[1].ToString();
GdItemList.Columns[2].HeaderText = rows[2].ToString();
于 2017-11-04T08:28:10.303 回答
1

问题不在于您的字符串列表,而在于您的 GdItemList。您向我们展示的代码中没有错误:https ://repl.it/Njj3/0

于 2017-11-04T08:22:50.743 回答
0

您是否尝试使用 ElementAt 功能?https://msdn.microsoft.com/en-us/library/bb299233(v=vs.110).aspx

List<string> rows = new List<string>(new string[] { "Item", "Quantity","Price"});

GdItemList.HeaderRow.Cells[0].Text = rows.ElementAt(0);
于 2017-11-04T08:23:26.080 回答