DataGrid : dynamic manipulation of a template column using C#

R

Richard

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 ?
 
E

Earl Teigrob

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
 
R

Richard

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
 
E

Earl Teigrob

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 said:
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 said:
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
"ddlTest2"
in ddlTest2;"
at Edit,
now
 
A

Ariel Gimenez

Excelent!
Earl please could you explain the part when you assign the correct index to
the dropdownlist?

thanks

Earl Teigrob said:
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 said:
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 said:
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



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 ?
 
R

Richard

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 said:
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 said:
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 said:
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



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 ?
 
E

Earl Teigrob

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 said:
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 said:
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 said:
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


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
 
R

Richard

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 said:
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 said:
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();}
}
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);

}

}



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


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



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 ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top