Reputation: 9314
I am trying to bind a grid to the following data source. Why do i get an extra row in there when there is not data in it? The count of the rows is also correct , i can see that when i debug .
Object[] users = new Object[100];
int i = 0;
while (reader.Read())
{
users[i++] = new {
userid= reader.GetString(0),
group = reader.GetString(1),
subgroup = reader.GetString(2),
};
}
gridview_UserAccess.DataSource = users;
gridview_UserAccess.DataBind();
<asp:GridView ID="gridview_UserAccess" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkbox_Select" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<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" />
</asp:GridView>
Upvotes: 0
Views: 1757
Reputation: 465
It is because you have a blank Line in your file. Because of this reader.Read() is true. Userid, group and subgroup are empty strings. So an User Object is created with empty values leading it to the empty row.
Upvotes: 3
Reputation: 1710
Possibly because your are incrementing i on the first read thus leaving users[0] blank? Try to change your code to the following:
int i = -1;
while (reader.Read())
{
users[i++] = new {
userid= reader.GetString(0),
group = reader.GetString(1),
subgroup = reader.GetString(2),
};
}
Upvotes: 0