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