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!

How to add new record using GridView 1

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi All,

I am using numerous gridviews to allow the application admimistrator to manage the values of lookup tables (code tables, etc - whatever you want to call them).

I can't see how to include and "Add" feature to the gridviews - so new records can be added. All I see is the Edit, Update, and Delete options. How do you manipulate gridviews to be used to enter new records. I can write the code to do an insert - that's not the problem. But typically you would see an extra row at the button for entering a new record. How to make that happen?

Thanks!
KB
 
You can use a FooterTemplate and just put whatever controls in it that you need.

Here's a good article which should explain the basics:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Thanks ca8msm.

As long as I am on the subject, another issue I have is getting the cells in the grid view to display the data from the record.

Let me be more exact....

I have bound gridviews (whether bound to a SQLSource control or a dataset does not seem to matter). I wish to give users the choice to edit. That's OK using the command field.

I have the gridview bound to the table that holds transaction records. In addition I want the grid to display dropdowns to select from to update the transaction record(s). I open two datasets. One is the transaction table and I bind the gridview to that. I put dropdown controls in the gridview cells. The second dataset contains multiple tables - one each for each lookup type of data. These tables fill the dropdowns with all the possible selections. What I am bombing out on is then getting the dropdowns to render as displayed to the "selected" value - from the transaction record. Using the selectedvalue property (or whatever its called) - I assign it to the value in the first dataset(the data from the existing record). Doesn't work.

Some of the columns in the gridview are textboxes. These have no problem displaying the correct data. The textboxes are used for data that is originally entered "freeform". I put text boxes in those columns of the grid and the first dataset values for those fields correctly show.

But not the dropdowns.

Any ideas??
 
How are you setting the value of the DropDownList? I'm guessing from your above statement that you are simply setting the SelectedValue property. What this will do, is assign your value to that property so if you printed out the SelectedValue later on, it would show it as being set to that value. It won't however select the Item with that value so on the screen the first item will still look as though it is selected (even though the Value would be correct). Instead, try using:
Code:
myDropDownList.Items.FindByValue("myValue").Selected = True


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
ca8msm - thanks, but I am not getting that line to work. I have been putting it after the databind, but I get a message that I should use "New" - that an object reference is not set.

The grid is named GridEditLegs and the dropdown is named ddEntity. Here is some code I have been trying, but does not work:

GridEditLegs.DataBind()

Dim gr As New GridView
Dim dd As New DropDownList
gr = Page.FindControl("GridEditLegs")
dd = gr.FindControl("ddEntity")
dd.Items.FindByValue("EntityID").Selected = True



It gives no syntax error, but when run it hangs up on the "dd = gr.FindControl("ddEntity")" line. The error is "NullReferenceException was not handled by user code" Then - "Object reference not set to an instance of an object"

That's why I put the lines in with "New", but makes no difference.

When I had just used your line by itself like this:

"ddEntity.Items.FindByValue("EntityID").Selected = True"

I got a blue line syntax error under ddEntity - that it is not declared.
 
The line of code I posted will work when you get a reference to the control. I assumed you would already have this if you were able to try to set it's SelectedValue.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 



Well, I am not quite getting that. How/where does this reference happen?

In the vb part I only have this for the grid:
GridEditLegs.DataBind()


And in the Source part here is the full grid and SQLData controls:

<asp:GridView ID="GridEditLegs" runat="server" AutoGenerateColumns="False" BackColor="Cornsilk"
BorderColor="IndianRed" BorderStyle="Solid" Font-Names="Arial" Font-Size="Small" DataSourceID="SqlDataSource_grid">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="Legid" HeaderText="." Visible="False" />
<asp:TemplateField HeaderText="Start Date"><ItemTemplate><asp:TextBox runat="server" ID=txtStartDate Text='<%# Bind("StartDate") %>'> </asp:TextBox></ItemTemplate>
<FooterTemplate>
<asp:TextBox ID=txtNewStartDate runat=server></asp:TextBox>
</FooterTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="End Date"><ItemTemplate><asp:TextBox runat="server" ID=txtEndDate Text='<%# Bind("EndDate") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Entity"><ItemTemplate><asp:DropDownList ID="ddEntity" runat="Server" DataSourceID="SQLDataSource_Entity" DataTextField="EntityDesc" DataValueField="EntityId" ></asp:DropDownList></ItemTemplate></asp:TemplateField>

<asp:TemplateField HeaderText="Status"><ItemTemplate><asp:DropDownList ID="ddStatus" runat="Server" DataTextField="StatusName" DataValueField="StatusCode" ></asp:DropDownList></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Volume"><ItemTemplate><asp:TextBox runat="server" ID=txtVolume Text='<%# Bind("Volume") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Price"><ItemTemplate><asp:TextBox runat="server" ID=txtPrice Text='<%# Bind("Price") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Total Volume"><ItemTemplate><asp:TextBox runat="server" ID=txtTotalVolume Text='<%# Bind("TotalVolume") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Schedule"><ItemTemplate><asp:DropDownList ID="ddSchedule" runat="Server" DataTextField="ScheduleName" DataValueField="ScheduleId"></asp:DropDownList></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Instrument"><ItemTemplate><asp:DropDownList ID="ddInstrument" runat="Server" DataTextField="InstrumentName" DataValueField="InstrumentId" ></asp:DropDownList></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="NominalValue"><ItemTemplate><asp:TextBox runat="server" ID=txtNominalValue Text='<%# Bind("NominalValue") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="CounterPartDealRef"><ItemTemplate><asp:TextBox runat="server" ID=txtCPDealRef Text='<%# Bind("CounterpartDealRef") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Strike"><ItemTemplate><asp:TextBox runat="server" ID=txtStrike Text='<%# Bind("Strike") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Unit of Measure."><ItemTemplate><asp:DropDownList ID="ddUOM" runat="Server" DataTextField="UOMName" DataValueField="UOMID"></asp:DropDownList></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Delivery Date"><ItemTemplate><asp:TextBox runat="server" ID=txtDeliveryDate Text='<%# Bind("DeliveryDate") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Settlement Code"><ItemTemplate><asp:DropDownList ID="ddSettlementCode" runat="Server" DataTextField="SettlementName" DataValueField="SettlementCode"></asp:DropDownList></ItemTemplate></asp:TemplateField>

<asp:TemplateField HeaderText="Primary Settlement Price"><ItemTemplate><asp:TextBox runat="server" ID=txtPrimaryPrice Text='<%# Bind("PrimarySettlementPrice") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Agreed Settlement Price" ><ItemTemplate><asp:TextBox runat="server" ID=txtAgreedPrice Text='<%# Bind("AgreedSettlementPrice") %>'></asp:TextBox></ItemTemplate></asp:TemplateField>

</Columns>
</asp:GridView>
&nbsp;<br />


<asp:SqlDataSource ID="SqlDataSource_grid" runat="server" ConnectionString="<%$ ConnectionStrings:Deal Capture Dev LocalConnectionString %>" SelectCommand="sp_get_single_deal" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="" Name="dealnum" QueryStringField="deal"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource_Entity" runat="server" ConnectionString="<%$ ConnectionStrings:Deal Capture Dev LocalConnectionString %>"
SelectCommand="Select [EntityID], [EntityDesc] from cdNYPAEntity Where [Active]=1"></asp:SqlDataSource>

 
Hah! Live and learn, or rather experiment and discover.

I got the dropdown to display the selected value by simply adding a binding in the source. Here is the updated line:


<asp:TemplateField HeaderText="Entity"><ItemTemplate><asp:DropDownList ID="ddEntity" runat="Server" DataSourceID="SQLDataSource_Entity" DataTextField="EntityDesc" DataValueField="EntityId" SelectedValue='<%# Bind("EntityID") %>' ></asp:DropDownList></ItemTemplate></asp:TemplateField>


It works! Thanks though for your guidance, it gave me the direction.

- KB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top