1

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

第一列应该是复选框列,它是静态的。第二列是用户名列,它也是静态的。

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

这是代码..用于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 的成员资格数据表在 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();
}

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

4

1 回答 1

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));
于 2013-09-20T11:11:53.027 回答