d3fcon 0 Newbie Poster

Hi All,

I'm new to web dev and c# so please bare with me :)

I am trying to create a dynamic gridview in a web form for users to to answer questions with (code below).

The dificulty im having is that i am nesting a dropdwonlist in the gridview and want to be able to dynamically set the list values based on parameters (of the row they are on). These values are in the main dataset for the gridview as each row represents a questionid and question text and then a ddl for the criteria...

I just don't know how to set the values for the dropdown all the code so far is below... just need to be able to populate the dropdowns with the relevant values.

I have created a stored proc to return the different criteria based on the questionid and questionGroupid which is the dataset that populates the other fields in the gridview: dbo.usp_QuestionCriteria @QuestionGroupId, @QuestionId

Please help

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
        con.Open();
        SqlCommand com = new SqlCommand("usp_QuestionGroupDS", con);
        com.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter ada = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        ada.Fill(ds);
        for (int i = 0; i < ds.Tables.Count; i++)
        {
            if (ds.Tables[i].Rows.Count > 0)
            {
                GridView gvDynamicQuestion = new GridView();
                gvDynamicQuestion.Width = Unit.Pixel(700);
                gvDynamicQuestion.BorderWidth = Unit.Pixel(0);
                gvDynamicQuestion.Caption = "<div id=\"nifty\" class=\"QuestionGroup\"> <b class=\"rtop\"><b class=\"r1\"></b><b class=\"r2\"></b><b class=\"r3\"></b><b class=\"r4\"></b></b>" + ds.Tables[i].Rows[0]["Category"].ToString() + " Questions<b class=\"rbottom\"><b class=\"r4\"></b><b class=\"r3\"></b><b class=\"r2\"></b><b class=\"r1\"></b></b></div>";
                gvDynamicQuestion.AutoGenerateColumns = false;
                gvDynamicQuestion.ShowFooter = true;
                TemplateField tf = null;

                tf = new TemplateField();
                tf.HeaderTemplate = new DynamicGridViewTextTemplate("QuestionId", DataControlRowType.Header);
                tf.ItemTemplate = new DynamicGridViewTextTemplate("QuestionId", DataControlRowType.DataRow);
                tf.FooterTemplate = new DynamicGridViewTextTemplate(DataControlRowType.Footer, ds.Tables[i].Rows.Count);                

                gvDynamicQuestion.Columns.Add(tf);

                tf = new TemplateField();
                tf.HeaderTemplate = new DynamicGridViewTextTemplate("Question", DataControlRowType.Header);
                tf.ItemTemplate = new DynamicGridViewTextTemplate("Question", DataControlRowType.DataRow);
                gvDynamicQuestion.Columns.Add(tf);

                 tf = new TemplateField();
                tf.HeaderText = "Criteria";
                tf.HeaderTemplate = new DynamicGridViewTextTemplate("Criteria", DataControlRowType.Header);
                tf.ItemTemplate = new DynamicGridViewDDLTemplate();
                gvDynamicQuestion.Columns.Add(tf);

                ////tf = new TemplateField();
                ////tf.HeaderText = "Criteria";
                ////tf.ItemTemplate = new DynamicGridViewDDLTemplate();
                ////gvDynamicQuestion.Columns.Add(tf);


                gvDynamicQuestion.DataSource = ds.Tables[i];
                gvDynamicQuestion.DataBind();
                phDynamicGridHolder.Controls.Add(gvDynamicQuestion);
            }
        }

    }
    protected void DynamicGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            //
        }
    }
}
public class DynamicGridViewTextTemplate : ITemplate
{
    string _ColName;
    DataControlRowType _rowType;
    int _Count;

    public DynamicGridViewTextTemplate(string ColName, DataControlRowType RowType)
    {
        _ColName = ColName;
        _rowType = RowType;
    }
    public DynamicGridViewTextTemplate(DataControlRowType RowType, int QuestionCount)
    {
        _rowType = RowType;
        _Count = QuestionCount;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        switch (_rowType)
        {
            case DataControlRowType.Header:
                Literal lc = new Literal();
                lc.Text = "<b>" + _ColName + "</b>";
                container.Controls.Add(lc);
                break;
            case DataControlRowType.DataRow:               
                 Label lbl = new Label();
                 lbl.DataBinding += new EventHandler(this.lbl_DataBind);
                 container.Controls.Add(lbl);               
                break;
            case DataControlRowType.Footer:
                Literal flc = new Literal();
                flc.Text = "<b>Total No of Questions:" + _Count + "</b>";
                container.Controls.Add(flc);
                break;
            default:
                break;
        }
    }


    private void lbl_DataBind(Object sender, EventArgs e)
    {
        Label lbl  = (Label)sender;
        GridViewRow row = (GridViewRow)lbl.NamingContainer;
        lbl.Text =DataBinder.Eval(row.DataItem, _ColName).ToString();
    }

}
public class DynamicGridViewDDLTemplate : ITemplate
{
    // Implementation of ITemplate
    public void InstantiateIn(System.Web.UI.Control container)
    {
        // Create a DDL
        DropDownList ddl = new DropDownList();
        //Attach method to delegate
        ddl.DataBinding += new System.EventHandler(this.ddl_DataBind);
        container.Controls.Add(ddl);
    }

    //Method that responds to the DataBinding event     
    private void ddl_DataBind(object sender, System.EventArgs e)
    {
        //DropDownList ddl = (DropDownList)sender;
        //DataGridItem container = (DataGridItem)ddl.NamingContainer;    
        //ddl.Data.Checked = [Data binding expression];
      }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.