datagrid dynamic column template

Discussion in 'ASP .Net Datagrid Control' started by Jacob, Feb 4, 2005.

  1. Jacob

    Jacob Guest

    hi everyone,

    i've built a datagrid template that displays different cell controls depending
    on the data bound to it. eg: in the first row, the control may be a textbox,
    and in the next row, the control may be a checkbox. currently i go about
    doing this by adding the control when databind is called.

    public class MyTemplate : System.Web.UI.ITemplate
    {
    private void DataBindItem(object sender, EventArgs e)
    {
    PlaceHolder placeHolder = (PlaceHolder)sender;
    DataGridItem dataGridItem = (DataGridItem)placeHolder.NamingContainer;

    string rowType = Convert.ToString(DataBinder.Eval(dataGridItem.DataItem,
    "RowType"));

    if(rowType == "TextBox") {
    TextBox myTextBox = new TextBox();
    myTextBox.ID = "myTextBox";

    Label rowTypeLabel = new Label();
    rowTypeLabel.ID = "rowTypeLabel";
    rowTypeLabel.Visible = false;


    placeHolder.Controls.Add(myTextBox);
    placeHolder.Controls.Add(rowTypeLabel);
    return;
    }

    if(rowType == "CheckBox") {
    CheckBox myCheckBox = new CheckBox();
    myCheckBox.ID = "myCheckBox";

    Label rowTypeLabel = new Label();
    rowTypeLabel.ID = "rowTypeLabel";
    rowTypeLabel.Visible = false;

    placeHolder.Controls.Add(myTextBox);
    placeHolder.Controls.Add(rowTypeLabel);
    return;
    }

    }

    public void InstantiateIn(Control container)
    {
    PlaceHolder placeHolder = new PlaceHolder();
    placeHolder.DataBinding += new EventHandler(DataBindItem);

    container.Controls.Add(placeHolder);
    }

    }

    the datagrid is displayed correctly with the textboxes and checkboxes. however,
    when i iterate throught the datagrid rows, i can't reference the textbox
    or checkbox.

    private void OnClickUpdateButton(object sender, System.Web.UI.ImageClickEventArgs
    e)
    {
     
    Jacob, Feb 4, 2005
    #1
    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. Dexter
    Replies:
    1
    Views:
    432
    Eliyahu Goldin
    Jan 18, 2005
  2. John E.
    Replies:
    3
    Views:
    4,494
    masoud bayan
    Mar 25, 2005
  3. Ken Varn
    Replies:
    1
    Views:
    987
    Ken Varn
    Aug 19, 2005
  4. Charleees
    Replies:
    2
    Views:
    8,264
    Eliyahu Goldin
    Jun 20, 2006
  5. John E.

    Move bound column to right of dynamic column in datagrid?

    John E., Mar 24, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    250
    Elton Wang
    Mar 28, 2005
Loading...

Share This Page