Professional Web Applications Themes

add TemplateColumn(dropdown) to DataGrid at runtime - ASP.NET Data Grid Control

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 ...

  1. #1

    Default add TemplateColumn(dropdown) to DataGrid at runtime

    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[i] != "CompanyJobCode")
    {
    bCol = new BoundColumn();
    bCol.HeaderText = v_ColDesc[i];
    bCol.DataField = v_ColCode[i];
    bCol.SortExpression = v_ColCode[i];
    if (v_ColCode[i] == "EmployeeID")
    bCol.ReadOnly = true;

    _DataGrid.Columns.Add(bCol);
    }
    else
    {
    tCol = new TemplateColumn();
    tCol.HeaderText = v_ColDesc[i];
    tCol.SortExpression = v_ColCode[i];
    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(SelectedCompanyJob Code) != null)

    dropdownlist.Items.FindByValue(SelectedCompanyJobC ode.ToString()).Selected =
    true;

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

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

    So, I'm stuck.

    - Kilic


    Kilic Guest

  2. #2

    Default Re: add TemplateColumn(dropdown) to DataGrid at runtime

    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" <kbegsalary.com> wrote in message news:<e6qDEWKLEHA.3428TK2MSFTNGP09.phx.gbl>...
    > 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[i] != "CompanyJobCode")
    > {
    > bCol = new BoundColumn();
    > bCol.HeaderText = v_ColDesc[i];
    > bCol.DataField = v_ColCode[i];
    > bCol.SortExpression = v_ColCode[i];
    > if (v_ColCode[i] == "EmployeeID")
    > bCol.ReadOnly = true;
    >
    > _DataGrid.Columns.Add(bCol);
    > }
    > else
    > {
    > tCol = new TemplateColumn();
    > tCol.HeaderText = v_ColDesc[i];
    > tCol.SortExpression = v_ColCode[i];
    > 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(SelectedCompanyJob Code) != null)
    >
    > dropdownlist.Items.FindByValue(SelectedCompanyJobC ode.ToString()).Selected =
    > true;
    >
    > }
    > catch (Exception ex)
    > {
    > Console.WriteLine ("Error in BindCompanyJobCodeColumn" + ex.ToString());
    > }
    > finally
    > {
    > conn.Close();
    > conn.Dispose();
    > }
    > }
    > }
    >
    > ================================================== ==========================
    > =======
    >
    > So, I'm stuck.
    >
    > - Kilic
    Amar Guest

Similar Threads

  1. Replies: 0
    Last Post: April 5th, 03:58 AM
  2. DataGrid/ TemplateColumn/ HeaderTemplate / Preserve Value in Postb
    By codputer in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: December 31st, 07:27 PM
  3. Added TemplateColumn is not showing in DataGrid after PostBack
    By Jade Seguin in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: July 14th, 03:50 AM
  4. TemplateColumn to display Checkbox in DataGrid
    By Paul in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: April 21st, 04:13 AM
  5. DataGrid TemplateColumn and Events
    By DAC in forum ASP.NET Data Grid Control
    Replies: 4
    Last Post: August 27th, 04:09 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139