Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binding dropdown within datagrid

Status
Not open for further replies.

ludmann

Programmer
Apr 14, 2004
49
GB
I have a datagrid. The datagrid has a footer, and one of the columns in this footer is a dropdown list.

<asp:TemplateColumn>
... <FooterTemplate>
<asp:DropDownList ID="add_attribute"Runat="Server">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateColumn>

I am trying to bind this dropdown list to a column in the database

add_attribute.DataSource = ds.Tables[0];

add_attribute.DataTextField = ds.Tables[0].Columns["ss_attribute_name_c"].ColumnName.ToString();

add_attribute.DataValueField = ds.Tables[0].Columns["o_id"].ColumnName.ToString();

add_attribute.DataBind();

BUT I get the error that the add_attribute could not be found. I guess it is because it is a part of the datagrid.

How can I bind the dropdown within the datagrid with the column in the database?

Any suggestions?

Thank you,

Marika
 
Try this (it assumes your DataGrid has the ID datagrid1)
Code:
DropDownList add_attribute = (DropDownList) datagrid1.FindControl("add_attribute");
add_attribute.DataSource = ds.Tables[0];
//etc etc

To find the DropDownList you need to look inside the DataGrid's Controls collection. The FindControl method allwos you to search the collection for a control with the given Id and creates a reference to it.

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
I tried what you said, and it seem to except it, but I know get the error message:

System.NullReferenceException: Object reference not set to an instance of an object.

Would you know why this is? My table does exist and is not empty.
 
Hmm. What line is the object error thrown on? If its after the FindControl call then this probably means I'm wrong and its not finding your control in the datagrid. This doesn't mean the approach is wrong just the execution! My guess is the TextBox control isn't within the Controls collection of the DataGrid but in some child collection. Unfortunately I don't have time to put a mock up in place now to test but i will try later if you still don't have an answer. If you have VS.NET then the debugger can help you examine the control hierachy to find your textbox....

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
If you do have time later and could try it be great.

Thanks for the help either way,

Marika
 
Ok, the DropDownList is in the Controls collection of the footer DataGridItem. You can access it through the ItemDataBound event of the grid like this.
Code:
private void datagrid1_ItemDataBound(object sender, DataGridItemEventArgs e) {
			if(e.Item.ItemType == ListItemType.Footer){
				DropDownList lst = (DropDownList) e.Item.FindControl("add_attribute");
				lst.Items.Add(new ListItem("text", "value"));
			}
		}

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top