DataGrid : dynamic manipulation of a template column using C#

Discussion in 'ASP .Net Datagrid Control' started by Richard, Jan 30, 2004.

  1. Richard

    Richard Guest

    I have created a DataGrid in the aspx page, with a template column, which
    has a label "lblTest2" in Item Template and a drop down list "ddlTest2" in
    the EditItemTemplate. Ultimately I want to be able to dynamically set
    DataSource etc for the ddlTest2 when I click the Edit link on the data row
    but at the moment I cannot even do a simple manipulation like
    ddlTest2.BackColor = whatever...

    Initially there was an error : "the type or namespace 'ddlTest2' could not
    be found" when attempting to build the solution.
    So I added "protected System.Web.UI.WebControls.DropDownList ddlTest2;" at
    the top of the page. Now the project will build, but when clicking Edit, now
    I get error : "Object reference not set to an instance of an object."
    occuring when it hits the line ddlTest2.BackColor = ...

    Any idea what I'm failing to do ?
     
    Richard, Jan 30, 2004
    #1
    1. Advertising

  2. Richard

    Earl Teigrob Guest

    I normally populate the dropdownlist in a edit template during the databind
    event so that when I click on edit, it is populated and the selected index
    is already set to the current value. Is this what you are looking for or do
    you really want to dynamically change the contents of the drop down list
    when you press edit?

    Earl



    "Richard" <> wrote in message
    news:%...
    > I have created a DataGrid in the aspx page, with a template column, which
    > has a label "lblTest2" in Item Template and a drop down list "ddlTest2" in
    > the EditItemTemplate. Ultimately I want to be able to dynamically set
    > DataSource etc for the ddlTest2 when I click the Edit link on the data row
    > but at the moment I cannot even do a simple manipulation like
    > ddlTest2.BackColor = whatever...
    >
    > Initially there was an error : "the type or namespace 'ddlTest2' could not
    > be found" when attempting to build the solution.
    > So I added "protected System.Web.UI.WebControls.DropDownList ddlTest2;"

    at
    > the top of the page. Now the project will build, but when clicking Edit,

    now
    > I get error : "Object reference not set to an instance of an object."
    > occuring when it hits the line ddlTest2.BackColor = ...
    >
    > Any idea what I'm failing to do ?
    >
    >
    >
    >
     
    Earl Teigrob, Jan 30, 2004
    #2
    1. Advertising

  3. Richard

    Richard Guest

    yes that's exactly what I'm looking for, although I don't see a databind
    event.
    I've got events for CancelCommand, EditCommand, SortCommand and
    UpdateCommand and there are databind commands in a few of these places. Also
    since last message I've discovered that my ddlTest2 code does not cause this
    error in CancelCommand and UpdateCommand - but does in EditCommand.

    (By the way I have no problem populating a normal DropDownList - it's only
    in the Template column that the problem arises)

    Here's a simplified version of the EditCommand - you say to populate the
    drop down menu in the the databind event - how would it fit into here ?

    private void dgMachinesItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)

    {



    strOrderBy = (string)ViewState["OrderBy"];



    this.dgMachines.EditItemIndex = e.Item.ItemIndex;

    this.dgMachines.DataSource =
    machine.GetMachines(strFilter, strOrderBy);







    this.dgMachines.DataBind();



    }



    Thanks


    "Earl Teigrob" <> wrote in message
    news:...
    > I normally populate the dropdownlist in a edit template during the

    databind
    > event so that when I click on edit, it is populated and the selected index
    > is already set to the current value. Is this what you are looking for or

    do
    > you really want to dynamically change the contents of the drop down list
    > when you press edit?
    >
    > Earl
    >
    >
    >
    > "Richard" <> wrote in message
    > news:%...
    > > I have created a DataGrid in the aspx page, with a template column,

    which
    > > has a label "lblTest2" in Item Template and a drop down list "ddlTest2"

    in
    > > the EditItemTemplate. Ultimately I want to be able to dynamically set
    > > DataSource etc for the ddlTest2 when I click the Edit link on the data

    row
    > > but at the moment I cannot even do a simple manipulation like
    > > ddlTest2.BackColor = whatever...
    > >
    > > Initially there was an error : "the type or namespace 'ddlTest2' could

    not
    > > be found" when attempting to build the solution.
    > > So I added "protected System.Web.UI.WebControls.DropDownList ddlTest2;"

    > at
    > > the top of the page. Now the project will build, but when clicking Edit,

    > now
    > > I get error : "Object reference not set to an instance of an object."
    > > occuring when it hits the line ddlTest2.BackColor = ...
    > >
    > > Any idea what I'm failing to do ?
    > >
    > >
    > >
    > >

    >
    >
     
    Richard, Jan 30, 2004
    #3
  4. Richard

    Earl Teigrob Guest

    You must create an event handler like this:

    this.DataGrid1.ItemDataBound += new
    System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBo
    und);

    for the binding event and then you can use code like this to populate your
    Drop Down. In this example, once I have ManagerId2 you can do whatever with
    it.

    Earl

    private void DataGrid1_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)

    {

    if(e.Item.ItemType == ListItemType.EditItem)

    {

    //GET THE DATAROW

    System.Data.Common.DbDataRecord DataRow =
    (System.Data.Common.DbDataRecord)e.Item.DataItem;


    //POPULATE AND SET SELECTED INDEX FOR MANAGER DROPDOWN LIST

    String pkManager = Common.MakeStringOr(DataRow["pkManager"], null);

    DropDownList ManagerId2 = (DropDownList) e.Item.FindControl("ManagerId2");

    CreateManagerDropdownList(ref ManagerId2);

    if (pkManager!=null)

    ManagerId2.Items.FindByValue(pkManager).Selected=true;

    //SET SELECTED VALUE FOR ISMANAGER

    CheckBox IsManager2 = (CheckBox) e.Item.FindControl("IsManager2");

    String IsManager = DataRow["IsManager"].ToString();

    IsManager2.Checked=Common.MakeBoolOr(IsManager,false);

    }

    }



    "Richard" <> wrote in message
    news:...
    > yes that's exactly what I'm looking for, although I don't see a databind
    > event.
    > I've got events for CancelCommand, EditCommand, SortCommand and
    > UpdateCommand and there are databind commands in a few of these places.

    Also
    > since last message I've discovered that my ddlTest2 code does not cause

    this
    > error in CancelCommand and UpdateCommand - but does in EditCommand.
    >
    > (By the way I have no problem populating a normal DropDownList - it's only
    > in the Template column that the problem arises)
    >
    > Here's a simplified version of the EditCommand - you say to populate the
    > drop down menu in the the databind event - how would it fit into here ?
    >
    > private void dgMachinesItemCommand(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    >
    > {
    >
    >
    >
    > strOrderBy = (string)ViewState["OrderBy"];
    >
    >
    >
    > this.dgMachines.EditItemIndex = e.Item.ItemIndex;
    >
    > this.dgMachines.DataSource =
    > machine.GetMachines(strFilter, strOrderBy);
    >
    >
    >
    >
    >
    >
    >
    > this.dgMachines.DataBind();
    >
    >
    >
    > }
    >
    >
    >
    > Thanks
    >
    >
    > "Earl Teigrob" <> wrote in message
    > news:...
    > > I normally populate the dropdownlist in a edit template during the

    > databind
    > > event so that when I click on edit, it is populated and the selected

    index
    > > is already set to the current value. Is this what you are looking for or

    > do
    > > you really want to dynamically change the contents of the drop down list
    > > when you press edit?
    > >
    > > Earl
    > >
    > >
    > >
    > > "Richard" <> wrote in message
    > > news:%...
    > > > I have created a DataGrid in the aspx page, with a template column,

    > which
    > > > has a label "lblTest2" in Item Template and a drop down list

    "ddlTest2"
    > in
    > > > the EditItemTemplate. Ultimately I want to be able to dynamically set
    > > > DataSource etc for the ddlTest2 when I click the Edit link on the data

    > row
    > > > but at the moment I cannot even do a simple manipulation like
    > > > ddlTest2.BackColor = whatever...
    > > >
    > > > Initially there was an error : "the type or namespace 'ddlTest2' could

    > not
    > > > be found" when attempting to build the solution.
    > > > So I added "protected System.Web.UI.WebControls.DropDownList

    ddlTest2;"
    > > at
    > > > the top of the page. Now the project will build, but when clicking

    Edit,
    > > now
    > > > I get error : "Object reference not set to an instance of an object."
    > > > occuring when it hits the line ddlTest2.BackColor = ...
    > > >
    > > > Any idea what I'm failing to do ?
    > > >
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Earl Teigrob, Jan 30, 2004
    #4
  5. Excelent!
    Earl please could you explain the part when you assign the correct index to
    the dropdownlist?

    thanks

    "Earl Teigrob" <> wrote in message
    news:...
    > You must create an event handler like this:
    >
    > this.DataGrid1.ItemDataBound += new
    >

    System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBo
    > und);
    >
    > for the binding event and then you can use code like this to populate your
    > Drop Down. In this example, once I have ManagerId2 you can do whatever

    with
    > it.
    >
    > Earl
    >
    > private void DataGrid1_ItemDataBound(object sender,
    > System.Web.UI.WebControls.DataGridItemEventArgs e)
    >
    > {
    >
    > if(e.Item.ItemType == ListItemType.EditItem)
    >
    > {
    >
    > //GET THE DATAROW
    >
    > System.Data.Common.DbDataRecord DataRow =
    > (System.Data.Common.DbDataRecord)e.Item.DataItem;
    >
    >
    > //POPULATE AND SET SELECTED INDEX FOR MANAGER DROPDOWN LIST
    >
    > String pkManager = Common.MakeStringOr(DataRow["pkManager"], null);
    >
    > DropDownList ManagerId2 = (DropDownList) e.Item.FindControl("ManagerId2");
    >
    > CreateManagerDropdownList(ref ManagerId2);
    >
    > if (pkManager!=null)
    >
    > ManagerId2.Items.FindByValue(pkManager).Selected=true;
    >
    > //SET SELECTED VALUE FOR ISMANAGER
    >
    > CheckBox IsManager2 = (CheckBox) e.Item.FindControl("IsManager2");
    >
    > String IsManager = DataRow["IsManager"].ToString();
    >
    > IsManager2.Checked=Common.MakeBoolOr(IsManager,false);
    >
    > }
    >
    > }
    >
    >
    >
    > "Richard" <> wrote in message
    > news:...
    > > yes that's exactly what I'm looking for, although I don't see a databind
    > > event.
    > > I've got events for CancelCommand, EditCommand, SortCommand and
    > > UpdateCommand and there are databind commands in a few of these places.

    > Also
    > > since last message I've discovered that my ddlTest2 code does not cause

    > this
    > > error in CancelCommand and UpdateCommand - but does in EditCommand.
    > >
    > > (By the way I have no problem populating a normal DropDownList - it's

    only
    > > in the Template column that the problem arises)
    > >
    > > Here's a simplified version of the EditCommand - you say to populate the
    > > drop down menu in the the databind event - how would it fit into here ?
    > >
    > > private void dgMachinesItemCommand(object source,
    > > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > >
    > > {
    > >
    > >
    > >
    > > strOrderBy = (string)ViewState["OrderBy"];
    > >
    > >
    > >
    > > this.dgMachines.EditItemIndex = e.Item.ItemIndex;
    > >
    > > this.dgMachines.DataSource =
    > > machine.GetMachines(strFilter, strOrderBy);
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > > this.dgMachines.DataBind();
    > >
    > >
    > >
    > > }
    > >
    > >
    > >
    > > Thanks
    > >
    > >
    > > "Earl Teigrob" <> wrote in message
    > > news:...
    > > > I normally populate the dropdownlist in a edit template during the

    > > databind
    > > > event so that when I click on edit, it is populated and the selected

    > index
    > > > is already set to the current value. Is this what you are looking for

    or
    > > do
    > > > you really want to dynamically change the contents of the drop down

    list
    > > > when you press edit?
    > > >
    > > > Earl
    > > >
    > > >
    > > >
    > > > "Richard" <> wrote in message
    > > > news:%...
    > > > > I have created a DataGrid in the aspx page, with a template column,

    > > which
    > > > > has a label "lblTest2" in Item Template and a drop down list

    > "ddlTest2"
    > > in
    > > > > the EditItemTemplate. Ultimately I want to be able to dynamically

    set
    > > > > DataSource etc for the ddlTest2 when I click the Edit link on the

    data
    > > row
    > > > > but at the moment I cannot even do a simple manipulation like
    > > > > ddlTest2.BackColor = whatever...
    > > > >
    > > > > Initially there was an error : "the type or namespace 'ddlTest2'

    could
    > > not
    > > > > be found" when attempting to build the solution.
    > > > > So I added "protected System.Web.UI.WebControls.DropDownList

    > ddlTest2;"
    > > > at
    > > > > the top of the page. Now the project will build, but when clicking

    > Edit,
    > > > now
    > > > > I get error : "Object reference not set to an instance of an

    object."
    > > > > occuring when it hits the line ddlTest2.BackColor = ...
    > > > >
    > > > > Any idea what I'm failing to do ?
    > > > >
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Ariel Gimenez, Feb 2, 2004
    #5
  6. Richard

    Richard Guest

    Thanks. This was fantastic - I can now access the DropDownList from within
    code.
    However still don't seem to be able to populate the List from a database
    source.
    I know this source works, because it is populating a DropDownList that is
    NOT in a DataGrid, but refuses to work for the one inside the data list.
    I can successfully populate the list manually but not from datasource.
    Is there some trick for databinding inside a databind event ?

    Examples
    1) works fine, 2) fails (empty drop down list)


    1) FINE
    private void dgMachines_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.EditItem)
    {DataSet dsMachineDDL = machine.GetMachineDataList();
    DropDownList ddlDom = (DropDownList) e.Item.FindControl("ddlDomain");

    ddlDom.Items.Add("hooja");
    ddlDom.Items.Add("wotsit");}
    }


    2) EMPTY DROPDOWNLIST
    private void dgMachines_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.EditItem)
    {DataSet dsMachineDDL = machine.GetMachineDataList();
    DropDownList ddlDom = (DropDownList) e.Item.FindControl("ddlDomain");

    ddlDom.DataSource = dsMachineDDL;
    ddlDom.DataMember = "Table1";
    ddlDom.DataTextField = "mc_domain";
    ddlDom.DataValueField = "mc_domain";
    ddlDom.DataBind();}
    }
    "Earl Teigrob" <> wrote in message
    news:...
    > You must create an event handler like this:
    >
    > this.DataGrid1.ItemDataBound += new
    >

    System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBo
    > und);
    >
    > for the binding event and then you can use code like this to populate your
    > Drop Down. In this example, once I have ManagerId2 you can do whatever

    with
    > it.
    >
    > Earl
    >
    > private void DataGrid1_ItemDataBound(object sender,
    > System.Web.UI.WebControls.DataGridItemEventArgs e)
    >
    > {
    >
    > if(e.Item.ItemType == ListItemType.EditItem)
    >
    > {
    >
    > //GET THE DATAROW
    >
    > System.Data.Common.DbDataRecord DataRow =
    > (System.Data.Common.DbDataRecord)e.Item.DataItem;
    >
    >
    > //POPULATE AND SET SELECTED INDEX FOR MANAGER DROPDOWN LIST
    >
    > String pkManager = Common.MakeStringOr(DataRow["pkManager"], null);
    >
    > DropDownList ManagerId2 = (DropDownList) e.Item.FindControl("ManagerId2");
    >
    > CreateManagerDropdownList(ref ManagerId2);
    >
    > if (pkManager!=null)
    >
    > ManagerId2.Items.FindByValue(pkManager).Selected=true;
    >
    > //SET SELECTED VALUE FOR ISMANAGER
    >
    > CheckBox IsManager2 = (CheckBox) e.Item.FindControl("IsManager2");
    >
    > String IsManager = DataRow["IsManager"].ToString();
    >
    > IsManager2.Checked=Common.MakeBoolOr(IsManager,false);
    >
    > }
    >
    > }
    >
    >
    >
    > "Richard" <> wrote in message
    > news:...
    > > yes that's exactly what I'm looking for, although I don't see a databind
    > > event.
    > > I've got events for CancelCommand, EditCommand, SortCommand and
    > > UpdateCommand and there are databind commands in a few of these places.

    > Also
    > > since last message I've discovered that my ddlTest2 code does not cause

    > this
    > > error in CancelCommand and UpdateCommand - but does in EditCommand.
    > >
    > > (By the way I have no problem populating a normal DropDownList - it's

    only
    > > in the Template column that the problem arises)
    > >
    > > Here's a simplified version of the EditCommand - you say to populate the
    > > drop down menu in the the databind event - how would it fit into here ?
    > >
    > > private void dgMachinesItemCommand(object source,
    > > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > >
    > > {
    > >
    > >
    > >
    > > strOrderBy = (string)ViewState["OrderBy"];
    > >
    > >
    > >
    > > this.dgMachines.EditItemIndex = e.Item.ItemIndex;
    > >
    > > this.dgMachines.DataSource =
    > > machine.GetMachines(strFilter, strOrderBy);
    > >
    > >
    > >
    > >
    > >
    > >
    > >
    > > this.dgMachines.DataBind();
    > >
    > >
    > >
    > > }
    > >
    > >
    > >
    > > Thanks
    > >
    > >
    > > "Earl Teigrob" <> wrote in message
    > > news:...
    > > > I normally populate the dropdownlist in a edit template during the

    > > databind
    > > > event so that when I click on edit, it is populated and the selected

    > index
    > > > is already set to the current value. Is this what you are looking for

    or
    > > do
    > > > you really want to dynamically change the contents of the drop down

    list
    > > > when you press edit?
    > > >
    > > > Earl
    > > >
    > > >
    > > >
    > > > "Richard" <> wrote in message
    > > > news:%...
    > > > > I have created a DataGrid in the aspx page, with a template column,

    > > which
    > > > > has a label "lblTest2" in Item Template and a drop down list

    > "ddlTest2"
    > > in
    > > > > the EditItemTemplate. Ultimately I want to be able to dynamically

    set
    > > > > DataSource etc for the ddlTest2 when I click the Edit link on the

    data
    > > row
    > > > > but at the moment I cannot even do a simple manipulation like
    > > > > ddlTest2.BackColor = whatever...
    > > > >
    > > > > Initially there was an error : "the type or namespace 'ddlTest2'

    could
    > > not
    > > > > be found" when attempting to build the solution.
    > > > > So I added "protected System.Web.UI.WebControls.DropDownList

    > ddlTest2;"
    > > > at
    > > > > the top of the page. Now the project will build, but when clicking

    > Edit,
    > > > now
    > > > > I get error : "Object reference not set to an instance of an

    object."
    > > > > occuring when it hits the line ddlTest2.BackColor = ...
    > > > >
    > > > > Any idea what I'm failing to do ?
    > > > >
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Richard, Feb 3, 2004
    #6
  7. Richard

    Earl Teigrob Guest

    At first glance, I don't see a problem in your code. In my code to populate
    the dropdown, I am retrieving

    a datareader (it really should be a dataset) but its almost the same code...



    private void CreateManagerDropdownList(ref DropDownList ManagerDropDown)

    {

    User u = new User((string) Application["RIGHTS_CS"], (int)
    Application["pkApplication"]);

    ManagerDropDown.DataTextField="UserId";

    ManagerDropDown.DataValueField="pkUser";

    ManagerDropDown.DataSource = u.SelectManagers();

    ManagerDropDown.DataBind();

    ManagerDropDown.Items.Insert(0,new ListItem("--Select--",""));

    }

    "Richard" <> wrote in message
    news:...
    > Thanks. This was fantastic - I can now access the DropDownList from within
    > code.
    > However still don't seem to be able to populate the List from a database
    > source.
    > I know this source works, because it is populating a DropDownList that is
    > NOT in a DataGrid, but refuses to work for the one inside the data list.
    > I can successfully populate the list manually but not from datasource.
    > Is there some trick for databinding inside a databind event ?
    >
    > Examples
    > 1) works fine, 2) fails (empty drop down list)
    >
    >
    > 1) FINE
    > private void dgMachines_ItemDataBound(object sender,
    > System.Web.UI.WebControls.DataGridItemEventArgs e)
    > {
    > if (e.Item.ItemType == ListItemType.EditItem)
    > {DataSet dsMachineDDL = machine.GetMachineDataList();
    > DropDownList ddlDom = (DropDownList) e.Item.FindControl("ddlDomain");
    >
    > ddlDom.Items.Add("hooja");
    > ddlDom.Items.Add("wotsit");}
    > }
    >
    >
    > 2) EMPTY DROPDOWNLIST
    > private void dgMachines_ItemDataBound(object sender,
    > System.Web.UI.WebControls.DataGridItemEventArgs e)
    > {
    > if (e.Item.ItemType == ListItemType.EditItem)
    > {DataSet dsMachineDDL = machine.GetMachineDataList();
    > DropDownList ddlDom = (DropDownList) e.Item.FindControl("ddlDomain");
    >
    > ddlDom.DataSource = dsMachineDDL;
    > ddlDom.DataMember = "Table1";
    > ddlDom.DataTextField = "mc_domain";
    > ddlDom.DataValueField = "mc_domain";
    > ddlDom.DataBind();}
    > }
    > "Earl Teigrob" <> wrote in message
    > news:...
    > > You must create an event handler like this:
    > >
    > > this.DataGrid1.ItemDataBound += new
    > >

    >

    System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBo
    > > und);
    > >
    > > for the binding event and then you can use code like this to populate

    your
    > > Drop Down. In this example, once I have ManagerId2 you can do whatever

    > with
    > > it.
    > >
    > > Earl
    > >
    > > private void DataGrid1_ItemDataBound(object sender,
    > > System.Web.UI.WebControls.DataGridItemEventArgs e)
    > >
    > > {
    > >
    > > if(e.Item.ItemType == ListItemType.EditItem)
    > >
    > > {
    > >
    > > //GET THE DATAROW
    > >
    > > System.Data.Common.DbDataRecord DataRow =
    > > (System.Data.Common.DbDataRecord)e.Item.DataItem;
    > >
    > >
    > > //POPULATE AND SET SELECTED INDEX FOR MANAGER DROPDOWN LIST
    > >
    > > String pkManager = Common.MakeStringOr(DataRow["pkManager"], null);
    > >
    > > DropDownList ManagerId2 = (DropDownList)

    e.Item.FindControl("ManagerId2");
    > >
    > > CreateManagerDropdownList(ref ManagerId2);
    > >
    > > if (pkManager!=null)
    > >
    > > ManagerId2.Items.FindByValue(pkManager).Selected=true;
    > >
    > > //SET SELECTED VALUE FOR ISMANAGER
    > >
    > > CheckBox IsManager2 = (CheckBox) e.Item.FindControl("IsManager2");
    > >
    > > String IsManager = DataRow["IsManager"].ToString();
    > >
    > > IsManager2.Checked=Common.MakeBoolOr(IsManager,false);
    > >
    > > }
    > >
    > > }
    > >
    > >
    > >
    > > "Richard" <> wrote in message
    > > news:...
    > > > yes that's exactly what I'm looking for, although I don't see a

    databind
    > > > event.
    > > > I've got events for CancelCommand, EditCommand, SortCommand and
    > > > UpdateCommand and there are databind commands in a few of these

    places.
    > > Also
    > > > since last message I've discovered that my ddlTest2 code does not

    cause
    > > this
    > > > error in CancelCommand and UpdateCommand - but does in EditCommand.
    > > >
    > > > (By the way I have no problem populating a normal DropDownList - it's

    > only
    > > > in the Template column that the problem arises)
    > > >
    > > > Here's a simplified version of the EditCommand - you say to populate

    the
    > > > drop down menu in the the databind event - how would it fit into here

    ?
    > > >
    > > > private void dgMachinesItemCommand(object source,
    > > > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > > >
    > > > {
    > > >
    > > >
    > > >
    > > > strOrderBy = (string)ViewState["OrderBy"];
    > > >
    > > >
    > > >
    > > > this.dgMachines.EditItemIndex = e.Item.ItemIndex;
    > > >
    > > > this.dgMachines.DataSource =
    > > > machine.GetMachines(strFilter, strOrderBy);
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > > this.dgMachines.DataBind();
    > > >
    > > >
    > > >
    > > > }
    > > >
    > > >
    > > >
    > > > Thanks
    > > >
    > > >
    > > > "Earl Teigrob" <> wrote in message
    > > > news:...
    > > > > I normally populate the dropdownlist in a edit template during the
    > > > databind
    > > > > event so that when I click on edit, it is populated and the selected

    > > index
    > > > > is already set to the current value. Is this what you are looking

    for
    > or
    > > > do
    > > > > you really want to dynamically change the contents of the drop down

    > list
    > > > > when you press edit?
    > > > >
    > > > > Earl
    > > > >
    > > > >
    > > > >
    > > > > "Richard" <> wrote in message
    > > > > news:%...
    > > > > > I have created a DataGrid in the aspx page, with a template

    column,
    > > > which
    > > > > > has a label "lblTest2" in Item Template and a drop down list

    > > "ddlTest2"
    > > > in
    > > > > > the EditItemTemplate. Ultimately I want to be able to dynamically

    > set
    > > > > > DataSource etc for the ddlTest2 when I click the Edit link on the

    > data
    > > > row
    > > > > > but at the moment I cannot even do a simple manipulation like
    > > > > > ddlTest2.BackColor = whatever...
    > > > > >
    > > > > > Initially there was an error : "the type or namespace 'ddlTest2'

    > could
    > > > not
    > > > > > be found" when attempting to build the solution.
    > > > > > So I added "protected System.Web.UI.WebControls.DropDownList

    > > ddlTest2;"
    > > > > at
    > > > > > the top of the page. Now the project will build, but when clicking

    > > Edit,
    > > > > now
    > > > > > I get error : "Object reference not set to an instance of an

    > object."
    > > > > > occuring when it hits the line ddlTest2.BackColor = ...
    > > > > >
    > > > > > Any idea what I'm failing to do ?
    > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Earl Teigrob, Feb 4, 2004
    #7
  8. Richard

    Richard Guest

    Thanks.
    Everything in working order now.
    There was one further thing I had wrong which was that the datasource,member
    etc were also declared in the HTML of the aspx page and were conflicting
    with the code behind.
    So finally got it !!
    Thanks for your help !

    "Earl Teigrob" <> wrote in message
    news:%...
    > At first glance, I don't see a problem in your code. In my code to

    populate
    > the dropdown, I am retrieving
    >
    > a datareader (it really should be a dataset) but its almost the same

    code...
    >
    >
    >
    > private void CreateManagerDropdownList(ref DropDownList ManagerDropDown)
    >
    > {
    >
    > User u = new User((string) Application["RIGHTS_CS"], (int)
    > Application["pkApplication"]);
    >
    > ManagerDropDown.DataTextField="UserId";
    >
    > ManagerDropDown.DataValueField="pkUser";
    >
    > ManagerDropDown.DataSource = u.SelectManagers();
    >
    > ManagerDropDown.DataBind();
    >
    > ManagerDropDown.Items.Insert(0,new ListItem("--Select--",""));
    >
    > }
    >
    > "Richard" <> wrote in message
    > news:...
    > > Thanks. This was fantastic - I can now access the DropDownList from

    within
    > > code.
    > > However still don't seem to be able to populate the List from a database
    > > source.
    > > I know this source works, because it is populating a DropDownList that

    is
    > > NOT in a DataGrid, but refuses to work for the one inside the data list.
    > > I can successfully populate the list manually but not from datasource.
    > > Is there some trick for databinding inside a databind event ?
    > >
    > > Examples
    > > 1) works fine, 2) fails (empty drop down list)
    > >
    > >
    > > 1) FINE
    > > private void dgMachines_ItemDataBound(object sender,
    > > System.Web.UI.WebControls.DataGridItemEventArgs e)
    > > {
    > > if (e.Item.ItemType == ListItemType.EditItem)
    > > {DataSet dsMachineDDL = machine.GetMachineDataList();
    > > DropDownList ddlDom = (DropDownList) e.Item.FindControl("ddlDomain");
    > >
    > > ddlDom.Items.Add("hooja");
    > > ddlDom.Items.Add("wotsit");}
    > > }
    > >
    > >
    > > 2) EMPTY DROPDOWNLIST
    > > private void dgMachines_ItemDataBound(object sender,
    > > System.Web.UI.WebControls.DataGridItemEventArgs e)
    > > {
    > > if (e.Item.ItemType == ListItemType.EditItem)
    > > {DataSet dsMachineDDL = machine.GetMachineDataList();
    > > DropDownList ddlDom = (DropDownList) e.Item.FindControl("ddlDomain");
    > >
    > > ddlDom.DataSource = dsMachineDDL;
    > > ddlDom.DataMember = "Table1";
    > > ddlDom.DataTextField = "mc_domain";
    > > ddlDom.DataValueField = "mc_domain";
    > > ddlDom.DataBind();}
    > > }
    > > "Earl Teigrob" <> wrote in message
    > > news:...
    > > > You must create an event handler like this:
    > > >
    > > > this.DataGrid1.ItemDataBound += new
    > > >

    > >

    >

    System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBo
    > > > und);
    > > >
    > > > for the binding event and then you can use code like this to populate

    > your
    > > > Drop Down. In this example, once I have ManagerId2 you can do

    whatever
    > > with
    > > > it.
    > > >
    > > > Earl
    > > >
    > > > private void DataGrid1_ItemDataBound(object sender,
    > > > System.Web.UI.WebControls.DataGridItemEventArgs e)
    > > >
    > > > {
    > > >
    > > > if(e.Item.ItemType == ListItemType.EditItem)
    > > >
    > > > {
    > > >
    > > > //GET THE DATAROW
    > > >
    > > > System.Data.Common.DbDataRecord DataRow =
    > > > (System.Data.Common.DbDataRecord)e.Item.DataItem;
    > > >
    > > >
    > > > //POPULATE AND SET SELECTED INDEX FOR MANAGER DROPDOWN LIST
    > > >
    > > > String pkManager = Common.MakeStringOr(DataRow["pkManager"], null);
    > > >
    > > > DropDownList ManagerId2 = (DropDownList)

    > e.Item.FindControl("ManagerId2");
    > > >
    > > > CreateManagerDropdownList(ref ManagerId2);
    > > >
    > > > if (pkManager!=null)
    > > >
    > > > ManagerId2.Items.FindByValue(pkManager).Selected=true;
    > > >
    > > > //SET SELECTED VALUE FOR ISMANAGER
    > > >
    > > > CheckBox IsManager2 = (CheckBox) e.Item.FindControl("IsManager2");
    > > >
    > > > String IsManager = DataRow["IsManager"].ToString();
    > > >
    > > > IsManager2.Checked=Common.MakeBoolOr(IsManager,false);
    > > >
    > > > }
    > > >
    > > > }
    > > >
    > > >
    > > >
    > > > "Richard" <> wrote in message
    > > > news:...
    > > > > yes that's exactly what I'm looking for, although I don't see a

    > databind
    > > > > event.
    > > > > I've got events for CancelCommand, EditCommand, SortCommand and
    > > > > UpdateCommand and there are databind commands in a few of these

    > places.
    > > > Also
    > > > > since last message I've discovered that my ddlTest2 code does not

    > cause
    > > > this
    > > > > error in CancelCommand and UpdateCommand - but does in EditCommand.
    > > > >
    > > > > (By the way I have no problem populating a normal DropDownList -

    it's
    > > only
    > > > > in the Template column that the problem arises)
    > > > >
    > > > > Here's a simplified version of the EditCommand - you say to populate

    > the
    > > > > drop down menu in the the databind event - how would it fit into

    here
    > ?
    > > > >
    > > > > private void dgMachinesItemCommand(object source,
    > > > > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > > > >
    > > > > {
    > > > >
    > > > >
    > > > >
    > > > > strOrderBy = (string)ViewState["OrderBy"];
    > > > >
    > > > >
    > > > >
    > > > > this.dgMachines.EditItemIndex = e.Item.ItemIndex;
    > > > >
    > > > > this.dgMachines.DataSource =
    > > > > machine.GetMachines(strFilter, strOrderBy);
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
    > > > > this.dgMachines.DataBind();
    > > > >
    > > > >
    > > > >
    > > > > }
    > > > >
    > > > >
    > > > >
    > > > > Thanks
    > > > >
    > > > >
    > > > > "Earl Teigrob" <> wrote in message
    > > > > news:...
    > > > > > I normally populate the dropdownlist in a edit template during the
    > > > > databind
    > > > > > event so that when I click on edit, it is populated and the

    selected
    > > > index
    > > > > > is already set to the current value. Is this what you are looking

    > for
    > > or
    > > > > do
    > > > > > you really want to dynamically change the contents of the drop

    down
    > > list
    > > > > > when you press edit?
    > > > > >
    > > > > > Earl
    > > > > >
    > > > > >
    > > > > >
    > > > > > "Richard" <> wrote in message
    > > > > > news:%...
    > > > > > > I have created a DataGrid in the aspx page, with a template

    > column,
    > > > > which
    > > > > > > has a label "lblTest2" in Item Template and a drop down list
    > > > "ddlTest2"
    > > > > in
    > > > > > > the EditItemTemplate. Ultimately I want to be able to

    dynamically
    > > set
    > > > > > > DataSource etc for the ddlTest2 when I click the Edit link on

    the
    > > data
    > > > > row
    > > > > > > but at the moment I cannot even do a simple manipulation like
    > > > > > > ddlTest2.BackColor = whatever...
    > > > > > >
    > > > > > > Initially there was an error : "the type or namespace 'ddlTest2'

    > > could
    > > > > not
    > > > > > > be found" when attempting to build the solution.
    > > > > > > So I added "protected System.Web.UI.WebControls.DropDownList
    > > > ddlTest2;"
    > > > > > at
    > > > > > > the top of the page. Now the project will build, but when

    clicking
    > > > Edit,
    > > > > > now
    > > > > > > I get error : "Object reference not set to an instance of an

    > > object."
    > > > > > > occuring when it hits the line ddlTest2.BackColor = ...
    > > > > > >
    > > > > > > Any idea what I'm failing to do ?
    > > > > > >
    > > > > > >
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Richard, Feb 5, 2004
    #8
    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:
    419
    Eliyahu Goldin
    Jan 18, 2005
  2. John E.
    Replies:
    3
    Views:
    4,471
    masoud bayan
    Mar 25, 2005
  3. Ken Varn
    Replies:
    1
    Views:
    978
    Ken Varn
    Aug 19, 2005
  4. Charleees
    Replies:
    2
    Views:
    8,214
    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:
    233
    Elton Wang
    Mar 28, 2005
Loading...

Share This Page