无法从列表中获取值以设置网格标头值c#

无法从列表中获取值以设置网格标头值c#,c#,asp.net,gridview,converter,gridviewheader,C#,Asp.net,Gridview,Converter,Gridviewheader,我正在尝试从列表获取错误设置网格头值 对象引用未设置为对象的实例 我首先检查它,但我正在将列表值转换为字符串,在调试时,我可以在列表中找到值 代码如下所示: List<string> rows = new List<string>( new string[] { "Item", "Quantity", "Price" }); GdItemList.HeaderRow.Cells[0].Text = Convert.ToSt

我正在尝试从列表获取错误设置网格头值

对象引用未设置为对象的实例

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

代码如下所示:

        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.HeaderRow.Cells[0]。Text=Convert.ToString(行[0]);
GdItemList.HeaderRow.Cells[1]。Text=行[1]。ToString();
GdItemList.HeaderRow.Cells[2]。Text=行[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>

问题不在于字符串列表,而在于GdItemList。
您向我们显示的代码中没有错误:

您是否尝试使用ElementAt函数

List rows=新列表(新字符串[]{“项目”、“数量”、“价格”});
GdItemList.HeaderRow.Cells[0].Text=rows.ElementAt(0);
列表行=新列表(
新字符串[]{“项目”、“数量”、“价格”});
GdItemList.Columns[0].HeaderText=Convert.ToString(行[0]);
GdItemList.Columns[1]。HeaderText=行[1]。ToString();
GdItemList.Columns[2]。HeaderText=行[2]。ToString();

我猜标题行甚至整个“GdItemList”都没有初始化?这可能就是问题所在。GridView中的某些内容未初始化。我们需要看更多的代码。我认为这里的答案是正确的。我尝试过它给一个对象的实例提供相同的错误对象引用。
List<string> rows = new List<string>(new string[] { "Item", "Quantity","Price"});

GdItemList.HeaderRow.Cells[0].Text = rows.ElementAt(0);
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();