Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Bind LINQ result variable into Gridview in asp.net

I have written a simple LINQ which is retrieving username with roles from membership .Now as per my need i have to bind and display it into gridview but its not happening ...here is my code...

This is my gridview code...

<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
</div>
</form>

And here is my server side code with LINQ query..

protected void Page_Load(object sender, EventArgs e)
{
    BindGridviewData();      

}



protected void BindGridviewData()
{
    var roles = from MembershipUser u in Membership.GetAllUsers()
                select new
                {
                    user = u,
                    roles = Roles.GetRolesForUser(u.UserName)
                };

    GridView1.DataSource = roles;
    GridView1.DataBind();

}

But its not happening ....Any help will be appreciated...

like image 408
vikas Avatar asked Nov 26 '25 06:11

vikas


1 Answers

Here example shows binding (employee details) with the condition employee age >=20

Check out this link to get more ideas

This works fine

            var result = from c in objDataContext.Employees
                         where c.EmployeeAge >= 20
                         select c;                
            List<Employee> lstEmployee = result.ToList();
            gridviewEmp.DataSource = lstEmployee;
            gridviewEmp.DataBind();

Getting started with LINQ to SQL

like image 83
Arun Kumar Avatar answered Nov 28 '25 23:11

Arun Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!