System.IndexOutOfRangeException:在ASP.net中找不到动态数据列创建的第3列

时间:2013-09-20 11:10:28

标签: c# asp.net gridview datatable

我有一项工作需要在同一个数据表中显示静态和动态列。这是Gridview中Datatable列的描述...

第一列应该是复选框列,它是静态的.. 第二列是Username列,它也是静态的..

需要从代码中动态合并列的其余部分..

以下是aspx中gridview的代码..

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"
                    AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan"
                    BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" 
                    Width="477px">
                    <AlternatingRowStyle BackColor="PaleGoldenrod" />
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <asp:CheckBox ID="chkhdr" runat="server" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkChild" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Username">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("col0") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Role(Admin)">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkAdmin" runat="server" Checked='<%# Eval("col1") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Role(DPAO User )">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkUser" runat="server" Checked='<%# Eval("col2") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Role(GeneralUser)">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkgen" runat="server" Checked='<%# Eval("col3") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

这是我的代码隐藏文件,它使用mssql的Membership数据库在gridview的行中以已检查或未检查的模式显示记录

 protected void BindGridviewData()
{

    var role = from MembershipUser u in Membership.GetAllUsers()
               select new
               {
                   User = u.UserName,
                   Role = string.Join(",", Roles.GetRolesForUser(u.UserName))
               };


        DataTable dTable = new DataTable();

        string[] rolesarr = Roles.GetAllRoles();
        //int length = rolesarr.Count();

        for (int i = 0; i < rolesarr.Length; i++)
        {
            string colname = rolesarr[i];
            if (i == 0)
            {
                dTable.Columns.Add(colname, typeof(string));
            }
            else
            {
                dTable.Columns.Add(colname, typeof(bool));
            }
        }


    foreach (MembershipUser u in Membership.GetAllUsers())
    {
        DataRow dRow = dTable.NewRow();
        dRow[0] = u.UserName;

        string[] roles = Roles.GetRolesForUser(u.UserName);
        dRow[1] = roles.Contains("Admin") ? true : false;
        dRow[2] = roles.Contains("DPAO User") ? true : false;
        dRow[3] = roles.Contains("GeneralUser") ? true : false;
        dTable.Rows.Add(dRow);
    }
    GridView1.DataSource = dTable;
    GridView1.DataBind();
}

但它在问题标题中给出了上述错误

1 个答案:

答案 0 :(得分:0)

DataTable dTable = new DataTable();
string[] rolesarr = Roles.GetAllRoles();
// add column for user name 
dTable.Columns.Add("User Name", typeof(string));
// then add all the roles as columns 
Array.ForEach(rolesarr, r => dTable.Columns.Add(r));