views:

52

answers:

1

Is it possible to get the column names directly from a datacontext? Below I'm just hard coding the DataTextField and DataValueField, but I'd like to be able to get them from the datacontext if possible. What is the recommended way of getting the column names in a strongly-typed way?

DataClassesDataContext db = new DataClassesDataContext(WebConfigurationManager.ConnectionStrings[connstring].ConnectionString);

        drdPriority.DataSource = db.Tasks;
        drdPriority.DataTextField = "TaskDescription";
        drdPriority.DataValueField = "TaskId";
        drdPriority.DataBind();
A: 

Try the following code:


      using (DataContext1.DataContext1 dc = new DataContext1.DataContext1()) { 
        var q = dc.Mapping.GetTables(); 
        foreach (System.Data.Linq.Mapping.MetaTable table in q) { 
          List names = new List(); 
          foreach (System.Data.Linq.Mapping.MetaDataMember member in table.RowType.DataMembers) { 
            names.Add(member.Name); 
          }
          //Process the names here 
        }
Devart
I think it's better to leave the ad in your profile and not put it in an answer.
Mike Two
Thank you for the opinion. We have removed the advertisement.
Devart