Results 1 to 4 of 4

Thread: [RESOLVED] Accessing dynamically generated column names

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Resolved [RESOLVED] Accessing dynamically generated column names

    I have a gridview which is populated from a dataset returned from Sql Server. The stored procedure that returns the data (which is a cross tab query) generates the columns dynamically (as the data on screen will look like a report).

    The data is about users and, depending on the criterion passed to the stored procedure, the columns might be called [Bill],[Jim],[Mick] etc. or, with a different parameter passed to the stored procedure .. [Linda],[John],[Jane],[Fred]

    The data is a list of users as columns and the rows contain a list of skills.

    So the data might look like:

    Skill______Bill______Jim______Mick
    Word_____1|27____2|27_____3|27
    Excel_____1|34____2|34_____3|34

    The data like '1|27' is the UserID and SkillID concatenated - which is returned in the dataset.

    On screen I want it to like like the data above but with a checkbox at the intersections of the user and skill - and the value of the checkbox needs to be set to be the UserID|SkillID pair (as it were).

    Normally, I'd do something like this when populating the GridView:

    Code:
     gvUserSkills.DataSource = ds.Tables[2];
            gvUserSkills.DataKeyNames = new string[] { "UserID", "SkillID", "UserName" };
            gvUserSkills.DataBind();
    because, normally, I know what the column names are (because I've said: 'SELECT UserID, UserName, SkillID FROM etc.' to get the data.

    But here the columns are dynamically generated.

    So, my question is - how can I loop through the columns in my dataset assigning the Column Names (in this case [Bill],[Jim],[Mick] etc.) to the DataKeyNames collection?

    Thanks for any help.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Accessing dynamically generated column names

    Sorry, please ignore the above - got it.

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Accessing dynamically generated column names

    Hello,

    For the sake of the other people on this forum, you might want to think about explaining what you did.

    Gary

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: [RESOLVED] Accessing dynamically generated column names

    I looped through the columns in the datatable being used as the datasource and added the ColumnName to a string[]

    Code:
    gvUsers.DataSource = dt;
    
            string[] dkNames = new string[dt.Columns.Count];
    
            foreach (DataColumn dc in dt.Columns)
            {
                dkNames[numColumns] = dc.ColumnName.ToString();
                numColumns += 1;
            }
            gvUsers.DataKeyNames = dkNames;
    
            gvUsers.DataBind();

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width