add TemplateColumn(dropdown) to DataGrid at runtime

Discussion in 'ASP .Net Datagrid Control' started by Kilic, Apr 27, 2004.

  1. Kilic

    Kilic Guest

    Hi,

    I am having trouble adding a dropdown to DataGrid at runtime.
    I create all boundColumns and the TemplateColumn for the dropdown at
    runtime.
    The dropdown has a different datasource then the DataGrid.

    1. If I try to bind the second datasource to the dropdown I get An unhandled
    exception of type 'System.StackOverflowException' occurred in mscorlib.dll.

    2. If I comment that line out it displays always the dropdown. I want it
    to display only in edit mode. And also since I add the dropdown in to the
    DataGrid I cannot
    switch to edit mode. None of my other columns are editable.


    this is what I have so far:
    ============================================================================
    ==
    public class dgview1 : System.Web.UI.Page
    {
    //....
    private void Page_Load(object sender, System.EventArgs e)
    {
    //....
    AddSelectedColumns();
    BindGrid(defaultOrderField);
    }

    private void AddSelectedColumns()
    {
    BoundColumn bCol;
    TemplateColumn tCol;
    int i;
    try
    {
    for (i=0; i < v_ColCode.Length; i++)
    {
    if (v_ColCode != "CompanyJobCode")
    {
    bCol = new BoundColumn();
    bCol.HeaderText = v_ColDesc;
    bCol.DataField = v_ColCode;
    bCol.SortExpression = v_ColCode;
    if (v_ColCode == "EmployeeID")
    bCol.ReadOnly = true;

    _DataGrid.Columns.Add(bCol);
    }
    else
    {
    tCol = new TemplateColumn();
    tCol.HeaderText = v_ColDesc;
    tCol.SortExpression = v_ColCode;
    tCol.ItemTemplate = new CompanyJobCode();
    _DataGrid.Columns.Add(tCol);
    }
    }
    }
    catch (Exception ex)
    {
    lbl_debug.Text = lbl_debug.Text + "Exeption in function[getTotalCol()]
    : " + (ex.ToString()) + "<BR>";
    }
    }

    private void BindGrid(string strOrderField)
    {
    SqlConnection conn = new SqlConnection(strDBConn);
    String strSqlQuery = "exec dbo.usp_GetEmployeeDataByAll @OrderField = '"
    + strOrderField + "'";
    SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, conn);
    DataSet ds = new DataSet();
    da.Fill(ds);

    _DataGrid.DataSource = ds.Tables[0];
    _DataGrid.DataBind();

    }
    }


    public class CompanyJobCode : ITemplate
    {
    public CompanyJobCode() { }

    public void InstantiateIn(Control container)
    {

    DropDownList myDropDownList = new DropDownList();
    myDropDownList.ID = "CompanyJobCode";
    myDropDownList.DataBinding += new
    EventHandler(this.BindCompanyJobCodeColumn);
    container.Controls.Add(myDropDownList);
    }

    public void BindCompanyJobCodeColumn(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection(strDBConn);
    String strSqlQuery = "select * from tbl_CompanyJobCode";
    SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, conn);

    string SelectedCompanyJobCode;
    DropDownList dropdownlist = (DropDownList)sender;

    try
    {
    DataSet ds = new DataSet();
    da.Fill(ds);
    dropdownlist.DataSource = ds.Tables[0];
    dropdownlist.DataTextField = "CompanyJobDesc";
    dropdownlist.DataValueField = "CompanyJobCode";
    // dropdownlist.DataBind();


    DataGridItem container = (DataGridItem)dropdownlist.NamingContainer;
    SelectedCompanyJobCode =
    Convert.ToString(((DataRowView)container.DataItem)["CompanyJobCode"]);

    if (dropdownlist.Items.FindByValue(SelectedCompanyJobCode) != null)

    dropdownlist.Items.FindByValue(SelectedCompanyJobCode.ToString()).Selected =
    true;

    }
    catch (Exception ex)
    {
    Console.WriteLine ("Error in BindCompanyJobCodeColumn" + ex.ToString());
    }
    finally
    {
    conn.Close();
    conn.Dispose();
    }
    }
    }

    ============================================================================
    =======

    So, I'm stuck.

    - Kilic
     
    Kilic, Apr 27, 2004
    #1
    1. Advertising

  2. Kilic

    Amar Guest

    Your code seems correct i don't see any problem i have done the same
    thing and i never had any problem...maybe somewhere else is the
    problem....

    "Kilic" <> wrote in message news:<>...
    > Hi,
    >
    > I am having trouble adding a dropdown to DataGrid at runtime.
    > I create all boundColumns and the TemplateColumn for the dropdown at
    > runtime.
    > The dropdown has a different datasource then the DataGrid.
    >
    > 1. If I try to bind the second datasource to the dropdown I get An unhandled
    > exception of type 'System.StackOverflowException' occurred in mscorlib.dll.
    >
    > 2. If I comment that line out it displays always the dropdown. I want it
    > to display only in edit mode. And also since I add the dropdown in to the
    > DataGrid I cannot
    > switch to edit mode. None of my other columns are editable.
    >
    >
    > this is what I have so far:
    > ============================================================================
    > ==
    > public class dgview1 : System.Web.UI.Page
    > {
    > //....
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > //....
    > AddSelectedColumns();
    > BindGrid(defaultOrderField);
    > }
    >
    > private void AddSelectedColumns()
    > {
    > BoundColumn bCol;
    > TemplateColumn tCol;
    > int i;
    > try
    > {
    > for (i=0; i < v_ColCode.Length; i++)
    > {
    > if (v_ColCode != "CompanyJobCode")
    > {
    > bCol = new BoundColumn();
    > bCol.HeaderText = v_ColDesc;
    > bCol.DataField = v_ColCode;
    > bCol.SortExpression = v_ColCode;
    > if (v_ColCode == "EmployeeID")
    > bCol.ReadOnly = true;
    >
    > _DataGrid.Columns.Add(bCol);
    > }
    > else
    > {
    > tCol = new TemplateColumn();
    > tCol.HeaderText = v_ColDesc;
    > tCol.SortExpression = v_ColCode;
    > tCol.ItemTemplate = new CompanyJobCode();
    > _DataGrid.Columns.Add(tCol);
    > }
    > }
    > }
    > catch (Exception ex)
    > {
    > lbl_debug.Text = lbl_debug.Text + "Exeption in function[getTotalCol()]
    > : " + (ex.ToString()) + "<BR>";
    > }
    > }
    >
    > private void BindGrid(string strOrderField)
    > {
    > SqlConnection conn = new SqlConnection(strDBConn);
    > String strSqlQuery = "exec dbo.usp_GetEmployeeDataByAll @OrderField = '"
    > + strOrderField + "'";
    > SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, conn);
    > DataSet ds = new DataSet();
    > da.Fill(ds);
    >
    > _DataGrid.DataSource = ds.Tables[0];
    > _DataGrid.DataBind();
    >
    > }
    > }
    >
    >
    > public class CompanyJobCode : ITemplate
    > {
    > public CompanyJobCode() { }
    >
    > public void InstantiateIn(Control container)
    > {
    >
    > DropDownList myDropDownList = new DropDownList();
    > myDropDownList.ID = "CompanyJobCode";
    > myDropDownList.DataBinding += new
    > EventHandler(this.BindCompanyJobCodeColumn);
    > container.Controls.Add(myDropDownList);
    > }
    >
    > public void BindCompanyJobCodeColumn(object sender, EventArgs e)
    > {
    > SqlConnection conn = new SqlConnection(strDBConn);
    > String strSqlQuery = "select * from tbl_CompanyJobCode";
    > SqlDataAdapter da = new SqlDataAdapter(strSqlQuery, conn);
    >
    > string SelectedCompanyJobCode;
    > DropDownList dropdownlist = (DropDownList)sender;
    >
    > try
    > {
    > DataSet ds = new DataSet();
    > da.Fill(ds);
    > dropdownlist.DataSource = ds.Tables[0];
    > dropdownlist.DataTextField = "CompanyJobDesc";
    > dropdownlist.DataValueField = "CompanyJobCode";
    > // dropdownlist.DataBind();
    >
    >
    > DataGridItem container = (DataGridItem)dropdownlist.NamingContainer;
    > SelectedCompanyJobCode =
    > Convert.ToString(((DataRowView)container.DataItem)["CompanyJobCode"]);
    >
    > if (dropdownlist.Items.FindByValue(SelectedCompanyJobCode) != null)
    >
    > dropdownlist.Items.FindByValue(SelectedCompanyJobCode.ToString()).Selected =
    > true;
    >
    > }
    > catch (Exception ex)
    > {
    > Console.WriteLine ("Error in BindCompanyJobCodeColumn" + ex.ToString());
    > }
    > finally
    > {
    > conn.Close();
    > conn.Dispose();
    > }
    > }
    > }
    >
    > ============================================================================
    > =======
    >
    > So, I'm stuck.
    >
    > - Kilic
     
    Amar, Apr 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob
    Replies:
    0
    Views:
    620
  2. Steve Pierce

    DataGrid Runtime DropDown - ViewState Issue

    Steve Pierce, Jun 11, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    3,381
    Steve Pierce
    Jun 13, 2005
  3. Giorgio
    Replies:
    1
    Views:
    3,077
    Eliyahu Goldin
    Jul 21, 2005
  4. vishnu
    Replies:
    1
    Views:
    1,234
    Patrick.O.Ige
    Mar 25, 2006
  5. Daniel Roth
    Replies:
    0
    Views:
    264
    Daniel Roth
    Apr 5, 2005
Loading...

Share This Page