Add New Row To DataGrid Using the Footer With a DropDownList Control

I am trying to discover the best method for adding a new row of data to a datagrid. My datagrid is bound to an SQL Server table. I was reading the article "Adding a New Record to the Datagrid", by John Sanborn (4GuysFromRolla.com) wherein he suggests a method of using the datagrid's footer for adding new records.

The concept works if each column of data is to be keyed in by the user, but I have a requirement to provide a drop-down list of valid values for a specific column. I've been tinkering with using a drop-down list in the footer of the grid, but I am unable get it to display a list of values.
I can find the drop-down control in the footer, populate the control with a list of values (I can verify the dropdownlist's contents in the command window while in break-mode), but the control doesn't display any data when the drop-down arrow is clicked.
Is it possible to populate a drop-down list that is in the footer of a datagrid, or am I way off-base? If so, would somebody be able to provide me with an example of how this might be accomplished?
If not, would somebody be able to suggest a good method for adding a new record when a datagrid is being used to display data? A few people have suggested populating the web form with a series of text box controls (directly below the datagrid), but I'm not too thrilled with this approach. I would think the datagrid should be able to provide this functionality.

Thanks!
0
klowery
2/28/2004 5:21:48 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
1030 Views

Similar Articles

[PageSpeed] 6

Yes, it is very possible to use a dropdownlist in the footer the way you want to do. Can you post the code you are using to populate it? Specificially, where in your page code are you doing the population?
Jim


MS MVP ASP.NET [VC++/MFC emeritus]


Old Dog Learns New Tricks

Preferred programming language: cuneiform on clay tablets
0
JimRoss
2/29/2004 6:24:23 PM
Thanks for your response!

I would first like to clarify a few things.
1) The article that I'm referencing is:
http://aspnet.4guysfromrolla.com/articles/021203-1.aspx
2) I interpret the article's process for adding a new record as this:
a)The new row is visible in the footer (the footer is hidden/shown when the user clicks edit/cancel...)
b)An "Add" button is in the footer row, in the templated "Delete" button column
c)A user keys data into each text box in the grid's footer
d)The user clicks the "Add" button
e)myDataGrid_ItemCommand event is fired and the record is written to the database.
f)The "Add" is trapped in the myDataGrid_ItemCommand event using:
Private Sub myDataGrid_ItemCommand (byVal source..., byVal e...)
If e.CommandName = "Insert" then
... code to add the new record to the database
End If
End sub
This process will work for me if I can get my drop-down lists populated.
3) I'm not certain when or where I can populate the footer drop-down lists (I have two that I need to populate in this form). The drop-down list that's in my data columns is populated when the "Edit" link is clicked from a Dataset object (oRateDataSet) in the MyDataGrid_ItemDataBound event, using the following logic:
Private Sub myDataGrid_ItemDataBound(byVal sender..., byVal e...)
If e.Item.ItemType = ListItemType.EditItem Then
Dim dropBusUnit As DropDownList
dropBusUnit = e.Item.FindControl("dropBusUnit")
dropBusUnit.DataSource = oRateDataSet.Tables("BusUnits")
dropBusUnit.DataMember = "BusUnits"
dropBusUnit.DataTextField = "Bus_Unit"
dropBusUnit.DataValueField = "Bu_Id"
dropBusUnit.DataBind()
End if
End Sub
4) I created a templated "delete" button column in my datagrid, as prescribed in the article. I then created a footer template within the templated delete column. The html looks like this:
<ASP:DATAGRID>...
****html for the templated Delete column
<asp:TemplateColumn HeaderText="DELETE">
<ItemTemplate>
<asp:LinkButton runat="server" Text="Delete" CommandName="Delete" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
***this is the "add" button that is displayed in the footer row of the delete column****
<asp:Button commandname="Insert" text="add" id="btnAdd" runat="server" />
</FooterTemplate>
</asp:TemplateColumn>

****html for my Business Unit templated column in myDataGrid****
<asp:TemplateColumn HeaderText="BUS UNIT">
<HeaderStyle Wrap="False"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Bus_Unit") %>'>
</asp:Label>
</ItemTemplate>
<FooterStyle Wrap="False"></FooterStyle>
****I added this for the drop-down list control in the footer******
<FooterTemplate>
<asp:dropdownlist id="dropNewBusUnit" Runat="Server" Width="96px" />
</FooterTemplate>
****This is the drop-down list that is used when the grid is in "Edit" mode********
<EditItemTemplate>
<asp:DropDownList id="dropBusUnit" runat="server" Width="96px"> </asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
....</ASP:DATAGRID>

5) I'm currently trying to populate my footer drop-down list in the myDataGrid_ItemCommand event, although the drop-down list in the footer must be populated prior to this, as this event fires after a new record has been constructed and the "Add" button is clicked. I'm just tying to see if I can populate the drop-down lists at this moment in time. This is what I have so far, but it doesn't populate the footer drop-down list:
Private Sub myDataGrid_ItemCommand(byVal source..., byVal e...)
If e.CommandName = "Insert" Then
Dim dropNewBusUnit As DropDownList
'Need to instantiate oRateDataSet
BindGrid()

dropNewBusUnit = e.Item.Cells(4).Controls(1)
'Show the list of Business Units we're working with
'oRateDataSet is a dataset object that contains the table "BusUnits"
dropNewBusUnit.DataSource = oRateDataSet.Tables("BusUnits")
dropNewBusUnit.DataMember = "BusUnits"
dropNewBusUnit.DataTextField = "Bus_Unit"
dropNewBusUnit.DataValueField = "Bu_Id"
dropNewBusUnit.DataBind()
dropNewBusUnit.SelectedItem.Value = 0
End If
End Sub
So, in a nutshell:
a) Can I use a drop-down list in the footer of a datagrid, for the purpose of adding a new record to the database?
b) When should I populate the footer drop-down list?
c) I am open for any suggestions you may have.
Thanks,
Kevin
0
klowery
2/29/2004 8:24:26 PM
Keven, thanks for pointing that article out.  Here is how I populated a dropdownlist after doing everything in the article:
Sub Grid_ItemDataBound(Sender As Object, E As DataGridItemEventArgs)

If e.Item.ItemType = ListItemType.footer Then
Dim strConn2 as string = (ConfigurationSettings.AppSettings("ConnectionString"))
Dim MySQL2 as string = "Select userID, companyname from users where usertype='advertiser'"
Dim MyConn2 as New SQLConnection(strConn2)
Dim Cmd2 as New SQLCommand(MySQL2, MyConn2)
MyConn2.Open()
Dim MyDLL As dropdownlist
MyDLL = CType(e.Item.FindControl("add_UserID"), DropDownList)
MyDLL.DataSource = Cmd2.ExecuteReader(system.data.CommandBehavior.CloseConnection)
MyDLL.DataBind()
MyDLL.Items.Insert(0, New ListItem("", "")) '<---to add a blank
End If
End Sub
And here is dropdownlist in the footertemplate:
<FooterTemplate>

<asp:dropdownlist ID="add_UserID" datatextfield=companyname datavaluefield=userid Runat="Server" /><br>
<asp:RequiredFieldValidator ControlToValidate="add_UserID" Display="dynamic" Font-Name="verdana" Font-Size="9pt" ErrorMessage="Please don't leave blank." runat="server"></asp:RequiredFieldValidator>
</FooterTemplate>
A couple of things I did here that were not a must were add a seperate datatextfield and datavaluefield to the dropdownlist, adding a blank space at the top of the dropdownlist, and added a requiredfielfvalidator to the dropdownlist.
Hope this helps!
Greg
0
gregfnor
3/15/2004 10:51:29 AM
Greg:

Thanks for the reply!
I had discovered the following article a couple of weeks ago regarding this subject: http://www.dotnetjunkies.com/Tutorial/24D5D73F-F941-4A70-B064-3195B8C039E7.dcik.
This article, along with the aforementioned article, brought everything together and allowed me to use the datagrid footer as a mechanism for adding new records.
You have enlightened me on two additional points that I had not yet attacked; 1) adding a blank row to the dropdown list and 2) using a requiredfieldvalidator with a datagrid. I will now be able to incorporate each of these into my datagird.
Thanks again!
0
klowery
3/15/2004 2:59:16 PM
Reply:

Similar Artilces:

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Should I use DataGrid(add dynamic control) or just dynamic control?
Hi, I am debating between using just dynamic control's or actually adding the control's dynamically into a datagrid.  I have tried with just adding dynamically, but I am having so much trouble because the selectedindexchanged even it firing for no reason.  I have asked a lot of people, but nobody knows the answer for that and I searched in the web and I am not getting anything either. Now someone suggested to use DataGrid and add the control's dynamically.  In each row, I have a dynamic column, which could be 2 and above.  It will depends on a number that I get fromt he ...

Problem in Using DropDownList Control to select a selected row of SqlDataSource that is under GridView Control
Hi all,In my SQL Server Management Studio Express, I have a database "ChemDatabase" that has a table "LabData"  formatted in the following way: SampleID int PRIMARY KEY, SampleName nvarchar(25), ChemicalName, nvarchar(25), Concentration decimal(6.2) with 5 rows of records in this .dbo table. In my Visual Web Developer Express, I created a website "RPD-TCE" for extracting a row of record from the LabData table by using a DropDownList, a GridView and a SqlDataSource.  I want to click DropDownList to list one number of S...

How to add a new row into gridview using Objectdatasource control
Hi, I am binding data into the gridview using objectdatasource control. I am able to perform the update and the delete functionality. But I need to insert a new row into the gridview and then somehow wire up the objectdatasource control to do the insert routine for me. I am using business logic layer plus the dataaccess layer along with stored procedures to achieve the above. Can some one tell me how to perform the insert routine? Hi guyind, It is no matter which datasource control you use, ObjectDataSource or SqlDataSource. You can set InsertMethod into ObjectDataSource to insert a new r...

how to add numbers in a any data control (Datagrid,DataList,Repeater) having textbox controls using Javascript?
hi,       i have a data control either(DataList,Grid, Repeater) having 3 textboxes row wise and column wise. I need to add the numbers inside the textbox using javascript in onclient Change or blur event .Anybody know how to do this ? pls give sample code.  javascript:var mygrid = document.getElementById('<%= MyGrid.ClientID %>'); var arrinputs = mygrid.getElementsByTagName("input"); for(var i = 0; i < arrinputs.length; i++) { if(arrinputs[i].type == "text") { // you have a text box now enter in value ...

What control do I use (or do I not use a control at all...)
Okay so here's the story so far. I am making a blog to be incorporated into a website. Well, I'm doing the blog from scratch. I'm using MySQL to source all the data via MySQL .NET/Connector. Now while developing the script, I used DataReader and Response.Write to show the blog topics. Now that it has come to put it under a design (I'm using a Master Page for that), I've hit the problem. Response.Write shows all the topics before any of the design elements could be incorporated. So that (more or less) is out of question. So next I looked into the data presentation controls. I looked up GridVi...

this.Controls.Add does not add control to child collection (this.Controls.Count == 0)
Ok, as silly as it may sound, I have a situation where I am creating a CompositeControl in ASP.NET 2.0, C#. I have the following code in the CreateChildControls() method that build the control's child control collection: 1    for (int i = 0; i < _menus.Count; i++)2    this.Controls.Add(_menus[i]);3    4    foreach (FloatingMenu menu in _menus)5    {6    Label lbl = new Label();7    lbl.ID = string.Concat("GroupQ", menu.ID);8    lbl.CssClass = "menuIt...

Error When Using a DropDownList Control in a ListView Control
VWD 2008 Express.  Visual Basic. I have placed a ListView Control on my page that is bound to a SQLDataSource.  I have placed a dropdownlist control in one of the fields (columns) in the edit, update, and insert templates.  This dropdownlist control is bound to another SQLDataSource.  The dropdownlist control works fine in the edit and update templates.  However, when I place the same dropdownlist in the insert template, I receive the following error (while in the VWD debug mode).  Why would it work in the edit and update templates and not in the insert templat...

Add .Net Control to Web Custom Control
 I want to add .NET Control (like: TextBox, Button...) to Web Custom Control.How i can do it? Alphavn That's called a composite control -- "Mark As Answer" if my reply helped you -- Hello Guys What gunteman said, that is one approach and in most cases the right one.But its also possible to add child controls to a Control thats inherit from WebControl itself. Be sure to implement INamingContainer to that inherited Control too, thats the important point of that approach.--Microsoft MVP - Visual Developer ASP / ASP.NET- http://www.aspnetzone.de/blogs/peterbuch...

How to place a DataGrid Control with in a DataGrid Control
Hi, I want to place a DataGrid control in a DataGrid control can any one plz tell me the procedure to place the control Thanks in Advance Krisamigo <!---<asp:DataGrid ID=dg_List runat=server><Columns><asp:TemplateColumn><ItemTemplate><asp:DataGrid ID=dg_List2 Runat=server></asp:DataGrid> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> ---> Hardik Joshi...

Add row in datagrid from other controls
Hello all, I am using the VS.2003.I would like to add a row in a datagrid, the data will come from other controls. I am having 2 textboxes (name, surname), 1 dropdownlist (sex) and a datagrid. I want to enter some values into these controls and when i am pressing a button (add) to insert them into datagrid row (the columns are also: name, surname and sex).The other issue is that i should add the ability of editing the values in the datagrid by pressing "Edit" button (it exists in each datagrid row). And the datagrid columns will have to be also 2 textboxes and 1 dropdownlist (jus...

Controls.Add in Composite Control that inherits another control
Hi! I created a composite control that inherits from the WebControls.Button. I added the following in the Load event:         Dim mHidden As New WebControls.HiddenField        Me.Controls.Add(mHidden) but the control is not created. Why?Dimitris PapadimitriouSoftware Development Professional Hi,Child controls on Button do not make much sense (how would you expect them to be rendered, doesn't button allow only text into value attribute?). Button allows child controls to be added, but they won't be rendered b...

Add a new parent control to an existing user control...?
Hi there. Sorry if this has been covered before but I couldn't find any reference to it.I have a user control which is based on an asp.net image control. One of the custom properties allows you to make the image 'clickable', which I would like to do by surrounding the custom image tag with a hyperlink (although also have an option to make it a linkbutton eventually).I've tried a couple of ways, and neither has quite worked:* Overriding the render method, and rendering the begin and end tags of a new hyperlink control right before and just after rendering the base control. Thi...

Unable to use Custom Control within a datagrid control
I have a datagrid and a template field to look like this <asp:TemplateField> <ItemTemplate> <customControl:Display runat="server" Description='<%#DataBinder.Eval(Container.DataItem,"Description")%>' /> <asp:Label ID="mylabel" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"ReportID")%>' /> </ItemTemplate> </asp:TemplateField> My custom control has a property called "Description" which basically is nothing but the value for a label in that control. Now, in the above code, the asp label control correctly displays the text,...

Web resources about - Add New Row To DataGrid Using the Footer With a DropDownList Control - asp.net.presentation-controls

Tag Synonyms - Stack Overflow
Q&A for professional and enthusiast programmers

Accidental entities - what about the UI?
This post is a follow-up to my previous blog post " Accidental entities - you don't need that identity ". In that post, we followed a consultant ...

Flash Builder Plug-in for SAP Netweaver Gateway - Adobe Labs
Search Navigation Labs Home Wiki Home Technologies Alchemy Blueprint Camera Profiles and DNG Profile Editor Cirrus ColdFusion 10 DNG Codec Fireworks ...

Air Astana - Join Now
Thank you for considering membership of Nomad Club – Kazakhstan’s first Frequent Flyer Program. You could soon be on your way to earning free ...

Languages -> C# Articles, Tutorials, Examples: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Blog - Avisra
Avisra Web Development Home Blog Services Contact The Blog Latest Module Builder: How can we make it better? Sitefinity by Chris on July 2, 2013 ...

Online Booking Tool Support
This Online Booking Tool Support page might help you as you navigate through the process of booking travel through your OBT provided by CWT. ...

@Html and beyond
... ); } return string .Format( “{0}{1}” , me.Member.Name, id); } The Html building methods are quite simple. Building a dropdownlist for a ...

Top Ranked Articles
Top Ranked Articles - Free source code and tutorials for Software developers and Architects.; Updated: 23 Feb 2013

Unit Testing DateTime.Now Without Isolation
public virtual void SeedWithYearsSince(DropDownList list, int year){ for (int index = year; index

Resources last updated: 1/23/2016 8:48:17 AM