dynamic column from listbox by Linq to sql RRS feed

  • Question

  • Hi

    i have gone through many post but did not find any help. My problem is, I want to show selected column from database using a listbox by linq to sql.

    First the page shows the column to a listbox. When I select some column and click show button then the gridview will show the selected column. here is my code

    protected void Page_Load(object sender, EventArgs e)
            ROSCDataContext roscdcontxt = new ROSCDataContext();
            var datamodel = roscdcontxt.Mapping;
            foreach (var r in datamodel.GetTables())
                if (r.TableName.Equals("dbo.vw_web_RMIS_LC_lst", StringComparison.InvariantCultureIgnoreCase))
                    foreach (var r1 in r.RowType.DataMembers)
                    {     ListBoxColumnName.Items.Add(r1.MappedName);
     protected void btnShow_LCList_Click(object sender, EventArgs e)
            var filter = ListBoxColumnName.Items;
            ROSCDataContext roscdcontxt = new ROSCDataContext();
            var query = from lc in roscdcontxt.vw_web_RMIS_LC_lsts
                        where (lc.LCYr == Convert.ToInt32(ddlcohort.Text))
                        orderby lc.DistrictID, lc.UpazilaID
                        select new { filter };
            GridView1.DataSource = query;

    what is wrong and what should I do by this problem.

    Thanks in advance.

    • Moved by Kristin Xie Friday, October 31, 2014 8:02 AM move to appropriate forum
    • Moved by Carl Cai Friday, October 31, 2014 9:43 AM more related to suggested forum
    Wednesday, October 29, 2014 7:40 AM

All replies

  • Might have been an idea to explain what happens when you do what with your code.

    As it is, I can't run it so have to guess at what problem you're seeing.


    You can use a dynamic query.

    In order for that to work you want a string in a select.

    Something like


    where somestringvariable is something like:

    "new(chosencolumnName as blaa)"

    And you can then bind to blaa since any column you choose is aliased as blaa.


    Wednesday, October 29, 2014 11:26 AM
  • Thanks but not working. It can be done without Linq easily. i think but I need to do it.
    Thursday, October 30, 2014 2:30 AM
  • Hi Soulidentities,

    From your code, I found it is a web project, Here is a forum specilized for web project, I suggest you posting it in it for better supports.


    BTW, what is the ROSCDataContext class in your code? Is it customized?  what does your filter string look like? You could share more details in your next post.

    Best regards,

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 31, 2014 9:34 AM