Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6162
  • Last Modified:

how to add a new row in gridview ?

I have the below GridView which is bound to the datasource odsCustomers. How can I add a new row on the click
of the button (btnNewCustomer). I would need to add a new row which pulls all the columns as in edit mode.
eg: in view mode I show the customer code as label but in edit mode I show it as a combobox. when I click
the button a new row should be added with the controls in edititemtemplate or emptyitemtemplate rathern than itemtemplate?



can this done ?

<asp:Button ID="btnNewCustomer" runat="server" Text="Add New Customer" />
 
   <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="False" 
        DataSourceID="odsCustomers"
        DataKeyNames="CUST_ID"
        AllowPaging="true" PageSize="5"       
        Width="958px" >
          <Columns>
              <asp:CommandField ShowEditButton="True" ControlStyle-Width="35px" UpdateText="Save" />
              <asp:BoundField DataField="CUST_NUMBER" HeaderText="Line Number" ReadOnly="True"
                  ApplyFormatInEditMode="false">
                  <ItemStyle HorizontalAlign="Center" />
              </asp:BoundField>
              <asp:TemplateField HeaderText="Line Description" ItemStyle-HorizontalAlign="Left" >
                  <ItemTemplate >
                      <asp:Label runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "DESCRIPTION") %>'>
                      </asp:Label>
                  </ItemTemplate>
                  <EditItemTemplate>
                      <asp:TextBox ID="Label1" runat="server" TextMode="MultiLine" Rows="4" Text='<%# Bind("DESCRIPTION") %>' />
                  </EditItemTemplate>
              </asp:TemplateField>
              
              <asp:TemplateField HeaderText="Customer Code">
              <ItemTemplate>
                      <asp:Label ID="lblCustomerCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Customer_CODE_DESC") %>'>
                      </asp:Label>
              </ItemTemplate>
              
              <EditItemTemplate >
              <telerik:RadComboBox ID="RadComboBox1" runat="server" Width="100px" Height="200px"
                AllowCustomText="False" ShowToggleImage="True" ShowMoreResultsBox="false"    
                    EnableLoadOnDemand="True" DropDownWidth="500px" DataTextField="DESCRIPTION"  DataSource='<%# LoadCustomerCodes() %>' 
                      MarkFirstMatch="True"  OnClientItemsRequesting="" AutoPostBack="true"  Skin="Office2007">
                   <HeaderTemplate>
                        <table style="width: 500px" cellspacing="0" cellpadding="0">
                            <tr>
                                <td style="width: 200px;">
                                    Description</td> 
                                <td style="width: 60px;">
                                    Account</td>
                                <td style="width: 200px;">
                                    Category</td>
                            </tr>
                        </table>
                    </HeaderTemplate>
                
                    <ItemTemplate>
                        <table style="width: 500px" cellspacing="0" cellpadding="0">
                            <tr>
                                <td style="width: 200px;">
                                    <%# DataBinder.Eval(Container.DataItem,"DESCRIPTION")%>
                                </td>
                                <td style="width: 60px;">
                                    <%# DataBinder.Eval(Container.DataItem, "ACCOUNT")%>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </telerik:RadComboBox>
                </EditItemTemplate>
                </asp:TemplateField> 
              
              
              <asp:BoundField DataField="QUANTITY" HeaderText="Quantity" ControlStyle-Width="30px">
                  <ItemStyle HorizontalAlign="Center" />
              </asp:BoundField>
             
	      <asp:BoundField DataField="TOTAL_AMOUNT" HeaderText="Total Amount" ControlStyle-Width="50px" DataFormatString="{0:c}" >
                  <ItemStyle HorizontalAlign="Right" />
              </asp:BoundField>
              
              <asp:CommandField ShowDeleteButton="true" />
          </Columns>
        
        </asp:GridView>

Open in new window

0
tech_question
Asked:
tech_question
  • 4
  • 3
1 Solution
 
wht1986Commented:
I see you are using the RadComboBox, do you also have the license for the RadGrid? It can automatically create that insert row for you.  If not, what I typically do is add controls to a footer of a standard grid that match what is in the edit template.  I add the Insert button to the footer cell of update/cancel column.
0
 
tech_questionAuthor Commented:
I have the following radGrid - I have two problems with it. This is not the entire .aspx page - a  masterpage file is embedded within this page , also there are other tables. The relevant code is posted below.

1) When I hit edit on the record, the entire table collapses - I cannot see anything - can someone please explain me why?  Also - the delete works fine.
2) There is only one record being shown in the radGrid when I bind to the datasource.

<telerik:RadGrid ID="gridInvLines" AllowMultiRowSelection="true" AllowMultiRowEdit="true"
 
Skin="Office2007" runat="server" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
 
Width="100%" PageSize="14" AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateColumns="False"
 
DataSourceID="odsInvLines" ShowStatusBar="true" EnableAJAX="true" EnableAJAXLoadingTemplate="true"
 
LoadingTemplateTransparency="50">
 
<PagerStyle Mode="Slider" />
 
<ClientSettings>
 
<Selecting AllowRowSelect="true" />
 
</ClientSettings>
 
<MasterTableView CommandItemDisplay="Top" DataKeyNames="LINE_NUMBER" DataSourceID="odsInvLines"
 
EditMode=InPlace" AutoGenerateColumns="False" Width="100%">
 
<Columns>
 
 
<telerik:GridEditCommandColumn ButtonType="ImageButton" UpdateText="Save" CancelText="Cancel"
 
UniqueName="EditColumn">
 
<HeaderStyle Width="20px" />
 
<ItemStyle HorizontalAlign="Center" />
 
</telerik:GridEditCommandColumn> 
 
 
<telerik:GridTemplateColumn>
 
<ItemStyle HorizontalAlign="Center" />
 
<ItemTemplate>
 
<a href="">Copy</a>
 
</ItemTemplate>
 
</telerik:GridTemplateColumn>
 
 
<telerik:GridBoundColumn DataField="LINE_NUMBER" HeaderText="Line Number" 
 
UniqueName="LINE_NUMBER" ReadOnly="True">
 
<HeaderStyle Width="30px" />
 
<ItemStyle HorizontalAlign="Center" />
 
</telerik:GridBoundColumn> 
 
 
<telerik:GridTemplateColumn EditFormColumnIndex="2" HeaderText="Line Description" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="Left">
 
<ItemTemplate >
 
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DESCRIPTION") %>'>
 
</asp:Label>
 
</ItemTemplate>
 
<EditItemTemplate>
 
<asp:TextBox ID="Label1" runat="server" TextMode="MultiLine" Rows="4" Text='<%# Bind("DESCRIPTION") %>' />
 
</EditItemTemplate>
 
</telerik:GridTemplateColumn> 
 
 
<telerik:GridTemplateColumn HeaderText="Activity Code" >
 
<ItemTemplate>
 
<asp:Label ID="lblActivityCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ACTIVITY_CODE_DESC") %>'>
 
</asp:Label>
 
</ItemTemplate>
 
 
<EditItemTemplate >
 
<telerik:RadComboBox ID="RadComboBox1" runat="server" Width="100px" Height="200px"
 
AllowCustomText="False" ShowToggleImage="True" ShowMoreResultsBox="false" 
 
EnableLoadOnDemand="True" DropDownWidth="500px" DataTextField="DESCRIPTION" DataSource='<%# LoadActivityCodes() %>' 
 
MarkFirstMatch="True" OnClientItemsRequesting="" AutoPostBack="true" Skin="Office2007">
 
<HeaderTemplate>
 
<table style="width: 500px" cellspacing="0" cellpadding="0">
 
<tr>
 
<td style="width: 200px;">
 
Description</td>
 
<td style="width: 60px;">
 
Account</td>
 
<td style="width: 200px;">
 
Category</td>
 
</tr>
 
</table>
 
</HeaderTemplate>
 
 
<ItemTemplate>
 
<table style="width: 500px" cellspacing="0" cellpadding="0">
 
<tr>
 
<td style="width: 200px;">
 
<%# DataBinder.Eval(Container.DataItem,"DESCRIPTION")%>
 
</td>
 
<td style="width: 60px;">
 
<%# DataBinder.Eval(Container.DataItem, "ACCOUNT")%>
 
</td>
 
<td style="width: 200px;">
 
<%# DataBinder.Eval(Container.DataItem,"CATEGORY")%>
 
 
</td>
 
</tr>
 
</table>
 
</ItemTemplate>
 
</telerik:RadComboBox>
 
</EditItemTemplate>
 
</telerik:GridTemplateColumn>
 
 
 
<telerik:GridBoundColumn DataField="QUANTITY" HeaderText="Quantity" 
 
EditFormColumnIndex="1" UniqueName="QUANTITY">
 
<HeaderStyle Width="30px" />
 
<ItemStyle HorizontalAlign="Center" />
 
 
</telerik:GridBoundColumn> 
 
<telerik:GridBoundColumn DataField="UNIT_SELLING_PRICE" EditFormColumnIndex="1" HeaderText="Amount" DataFormatString="{0:c}">
 
<HeaderStyle Width="50px" />
 
<ItemStyle HorizontalAlign="Right" />
 
</telerik:GridBoundColumn>
 
 
<telerik:GridBoundColumn DataField="TOTAL_AMOUNT" HeaderText="Total Amount" DataFormatString="{0:c}" >
 
<HeaderStyle Width="50px" />
 
<ItemStyle HorizontalAlign="Right" />
 
</telerik:GridBoundColumn>
 
<telerik:GridCheckBoxColumn DataField="TAXABLE_FLAG" HeaderText="Taxable" 
 
UniqueName="TAXABLE_FLAG" EditFormColumnIndex="1">
 
<HeaderStyle Width="50px" />
 
</telerik:GridCheckBoxColumn>
 
<%-- <asp:TemplateField HeaderText="Taxable">
 
<ItemTemplate>
 
<itemstyle horizontalalign="Center" />
 
<asp:CheckBox ID="TAXABLE_FLAG" runat="Server" Checked='<%# Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TAXABLE_FLAG")) %>' />
 
</ItemTemplate>
 
</asp:TemplateField>
 
--%>
 
 
<telerik:GridBoundColumn DataField="BEGIN_BILLING_PERIOD" HeaderText="Billing Period Start Date"
 
UniqueName="BEGIN_BILLING_PERIOD" >
 
<HeaderStyle Width="62px" />
 
<ItemStyle HorizontalAlign="Center" />
 
</telerik:GridBoundColumn>
 
 
<telerik:GridBoundColumn DataField="END_BILLING_PERIOD" HeaderText="Billing Period End Date"
 
UniqueName="END_BILLING_PERIOD" >
 
<HeaderStyle Width="62px" />
 
<ItemStyle HorizontalAlign="Center" />
 
</telerik:GridBoundColumn>
 
 
<telerik:GridClientDeleteColumn ConfirmText="Delete this item?" ButtonType="LinkButton" 
 
CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
 
<HeaderStyle Width="20px" />
 
<ItemStyle HorizontalAlign="Center" />
 
</telerik:GridClientDeleteColumn>
 
 
 
</Columns>
 
<EditFormSettings EditFormType="AutoGenerated" ColumnNumber="2" CaptionDataField="LINE_NUMBER" CaptionFormatString="Edit Line Number:{0}">
 
<FormTableItemStyle Wrap="False"></FormTableItemStyle>
 
<FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
 
<FormMainTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="3" BackColor="White"
 
Width="100%" />
 
<FormTableStyle CellSpacing="0" CellPadding="2" CssClass="module" Height="110px"
 
BackColor="White" />
 
<FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
 
<EditColumn ButtonType="ImageButton" UpdateText="Save" CancelText="Cancel"
 
InsertText="Insert" 
 
UniqueName="EditCommandColumn1">
 
</EditColumn>
 
<FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
 
</EditFormSettings>
 
</MasterTableView> 
 
</telerik:RadGrid> 

Open in new window

0
 
wht1986Commented:
I'm working on a quick example for you. To ensure I create a relavent one, can you tell me if your data source is a sql data source on that page, an object datasource based on a dataset table adapter, or something else.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tech_questionAuthor Commented:
I just use a plain ObjectDataSource. I put the code in the middle layer which talks to oracle and gives me data back.
<asp:ObjectDataSource ID="odsInvLines" TypeName="ARA.HRPayroll.Common.Users" runat="server"
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetInvoiceLinesByInvoiceID" >
        <UpdateParameters>
        <asp:Parameter Name="Description" Type="String" />
        <asp:Parameter Name="Quantity" Type="String" />
 
        </UpdateParameters>
        <SelectParameters>
            <asp:Parameter Name="UserID" DefaultValue="31592" Type="Int64"  />
            <asp:QueryStringParameter Name="InvoiceID" QueryStringField="invoice_id" Type="Int64" />
        </SelectParameters>
    </asp:ObjectDataSource>
    

Open in new window

0
 
wht1986Commented:
Well I have to run home for my sons b-day party.  Here is a simple example i did using an object data source bound to dataset table adapter. Perhaps it will help.  Dont forget if you have a primary key, I always change the objectdatsource string from
OldValuesParameterFormatString="original_{0}"
to
OldValuesParameterFormatString="{0}"

The attached picture is what I get at run time
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Web_Form.WebForm1" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1" GridLines="None">
        <MasterTableView DataKeyNames="NoteID" DataSourceID="ObjectDataSource1" EditMode="InPlace" InsertItemDisplay="Top" AllowAutomaticInserts="true" CommandItemDisplay="Top">
            <Columns>
                <telerik:GridEditCommandColumn>
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn DataField="NoteID" DataType="System.Int32" HeaderText="NoteID" ReadOnly="True" SortExpression="NoteID" UniqueName="NoteID" />
                <telerik:GridBoundColumn DataField="NoteTypeID" DataType="System.Int32" HeaderText="NoteTypeID" SortExpression="NoteTypeID" UniqueName="NoteTypeID" />
                <telerik:GridBoundColumn DataField="NoteTitle" HeaderText="NoteTitle" SortExpression="NoteTitle" UniqueName="NoteTitle" />
                <telerik:GridBoundColumn DataField="NoteText" HeaderText="NoteText" SortExpression="NoteText" UniqueName="NoteText" />
                <telerik:GridBoundColumn DataField="AuthorID" DataType="System.Int32" HeaderText="AuthorID" SortExpression="AuthorID" UniqueName="AuthorID" />
                <telerik:GridBoundColumn DataField="DateEntered" DataType="System.DateTime" HeaderText="DateEntered" SortExpression="DateEntered" UniqueName="DateEntered" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="{0}" SelectMethod="GetData" TypeName="Web_Form.DataSet1TableAdapters.NoteTableAdapter"></asp:ObjectDataSource>
    </form>
</body>
</html>

Open in new window

Capture.JPG
0
 
tech_questionAuthor Commented:
cool - thanks. that worked. I think there was one problem with one of my columns inside the radGrid.

one more question - is there a copy functionality within the radGrid ? or do we have to write custom code for that ? Please let me know I will close this question and I will open a new one.
0
 
wht1986Commented:
Custom. The way I did that was create a new LinkButton column that had CommandName as "Copy" and the CommandArgument as the primary key column  Then in the RowCommand even, i looked for the copy command. Then do whatever it takes to copy the value.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now