0

I have Web Form in ASP.NET in which I would like to show a table with all users in my app. There should be user name, user email, last activity date and user roles.

Currently I has this:

<asp:DataGrid id="UserGrid" runat="server"
            CellPadding="2" CellSpacing="1"
            Gridlines="Both" AutoGenerateColumns="false">
  <Columns>
    <asp:BoundColumn DataField="UserName" ReadOnly="False" HeaderText="Name" />
    <asp:BoundColumn DataField="Email" ReadOnly="True" HeaderText="Email" />
    <asp:BoundColumn DataField="LastActivityDate" ReadOnly="True" HeaderText="Last activity"/>
</Columns>
<HeaderStyle BackColor="darkblue" ForeColor="white" />

</asp:DataGrid>

_

UserGrid.DataSource = Membership.GetAllUsers();
UserGrid.DataBind();

I would like to add column roles to this DataGrid. How can I do that?

In the next step I want to add column with buttons to edit users info, manage his roles etc.

holocen
  • 227
  • 1
  • 3
  • 10
  • do u have user roles in your database table? – shreesha Aug 25 '15 at 15:13
  • Yes. I can do for all users: string[] roles = Roles.GetRolesForUser(username); but I do not know how add this data to new column in DataGrid or maybe add this data to DataSource? – holocen Aug 26 '15 at 07:10
  • your `Membership.GetAllUsers();` is fetching **UserRoles** also right?or is it in different query? – shreesha Aug 26 '15 at 07:28

1 Answers1

1

Add the columns your GridView as shown below

 <asp:GridView id="UserGrid" runat="server"
                CellPadding="2" CellSpacing="1"
                Gridlines="Both" AutoGenerateColumns="false">
      <Columns>
        <asp:BoundColumn DataField="UserName" ReadOnly="False" HeaderText="Name" />
        <asp:BoundColumn DataField="Email" ReadOnly="True" HeaderText="Email" />
        <asp:BoundColumn DataField="LastActivityDate" ReadOnly="True" HeaderText="Last activity"/>
        <asp:TemplateField HeaderText="User Roles" ItemStyle-Width="30%">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblrole" runat="server" %>'></asp:Label>
                                                            </ItemTemplate>
                                                        </asp:TemplateField>
    </Columns>
    <HeaderStyle BackColor="darkblue" ForeColor="white" />

    </asp:GridView>

Declare a global variable for holding your user roles.

string[] roles;

In page load get your data

protected void page_load(object sender,EventArgs e)
{
if(!IsPostBack)
{
string[] roles=GetUserRoles();
}
}

In your RowDataBound event add the data to your gridview

    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       if(e.Row.RowType= DataControlRowType.DataRow)
      {
        e.Row.FindControl("lblrole").Text=roles[e.Row.RowIndex];
       }
    }

If data order is not matching then sort both the collections.Here DataField is your database column name or the alias name you are using in your query

shreesha
  • 1,811
  • 2
  • 21
  • 30
  • How should I make DataSource for this grid? Now I have: UserGrid.DataSource = Membership.GetAllUsers(); UserGrid.DataBind(); and I do not know how add to this data information about user roles, how can I add to MembershipUserCollection field with roles. My Membership.GetAllUsers(); does not contain UserRoles. – holocen Aug 26 '15 at 07:47
  • Membership.GetAllUsers() method must be executing some query on database right?check if that query is returning user roles column – shreesha Aug 26 '15 at 07:50
  • Membership.GetAllUsers() is a method from System.Web.Security (https://msdn.microsoft.com/en-us/library/dy8swhya%28v=vs.110%29.aspx) and I do not have access to its database query. It returns MembershipUserCollection (https://msdn.microsoft.com/pl-pl/library/system.web.security.membershipusercollection%28v=vs.110%29.aspx) and there is no field about roles. I probably should call for every user: Roles.GetRolesForUser(username) and somehow add this data to my DataGrid, but I do not know how. – holocen Aug 26 '15 at 07:57
  • check the edit.i used gridview change it with datagrid event – shreesha Aug 26 '15 at 09:18
  • I have DataGrid not GridView. DataGrid does not have RowDataBound event. I would like to add different roles for different users with the use of Roles.GetRolesForUser(username) - in your solution there is a string - the same roles for all rows. – holocen Aug 26 '15 at 09:45
  • check my edit.there are many advantages for gridview over datagrid.check the [link](http://www.codeproject.com/Questions/389336/What-is-the-diff-between-gridview-and-datagrid).if you still want to use datagrid then do some research and find equivalent code for datagrid – shreesha Aug 27 '15 at 06:41
  • shreesha thank you for your help I did something similar to your suggestion: protected void UserGrid_ItemDataBound(object sender, DataGridItemEventArgs e) { if (e.Item.DataItem != null) { e.Item.Cells[4].Text = string.Join(",", Roles.GetRolesForUser((e.Item.DataItem as MembershipUser).UserName)); } } – holocen Aug 27 '15 at 07:52