简体   繁体   中英

System.IndexOutOfRangeException: Cannot find column 3. with dynamic Datatable Column Creation in ASP.net

I have got a work where i need to show static and dynamic columns in same datatable ..Here is the description for the Datatable column in Gridview...

First column should be checkbox column and it is static.. Second column is Username column and it is also static..

Rest of the column needs to be merged dynamically..from the code..

Here is the code..for the gridview in aspx..

<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>

And here is my code behind file which is using Membership datatable of mssql to show the records in checked or unchecked mode in rows of the 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();
}

but it is giving the above mentioned error in the title of the problem

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM