Click here to Skip to main content
15,905,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all ,
iam trying to make insert link in gridview as edit link which already implemented in grid
i want when the user click on insert link the grid open anew row and the user enter data in fileds except primar key
this is my gridview
ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                        BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" 
                        CellPadding="4" Height="263px" 
                        onrowcancelingedit="GridView1_RowCancelingEdit" 
                         onrowdeleting="GridView1_RowDeleting" 
                        onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
                         Width="1124px" onrowcommand="GridView1_RowCommand" 
                       >
                        <RowStyle BackColor="White" ForeColor="#003399" />
                        <Columns>
                            <asp:TemplateField HeaderText="Transaction Number">
                           
                                 <ItemTemplate>
                                    <asp:Label ID="lbltransaction_num" runat="server" 
                                        Text='<%#Bind("transaction_num")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Label ID="lbltrans_num" runat="server" 
                                        Text='<%#Bind("transaction_num")%>'></asp:Label>
                                </EditItemTemplate>
                                  <FooterTemplate>
                                <asp:Label ID="LBLNUM" runat ="server" >
                                </asp:Label>
                                </FooterTemplate>
                                
                               
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Transaction Type ">
                            
                                 <EditItemTemplate>
                                     <asp:TextBox ID="TXTTYPE" runat="server" Text='<%# BIND("transaction_type") %>'></asp:TextBox>
                                 </EditItemTemplate>
                            
                            
                                 <ItemTemplate>
                                
                                    <asp:Label  ID="lblTrans_type" runat="server" 
                                        Text='<%# Bind("transaction_type") %>'></asp:Label>
                                </ItemTemplate>
                               <FooterTemplate>
                                <asp:TextBox ID="txtAddtransaction_type" runat="server" ></asp:TextBox>
                               
                               </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Transaction Date">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TXTDATE" runat="server" 
                                        Text='<%# Bind("transaction_date") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("transaction_date") %>'></asp:Label>
                                  
                                </ItemTemplate>
                                <FooterTemplate>
                                <asp:TextBox ID ="txttransaction_date" runat ="server" >
                                </asp:TextBox>
                                
                                
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Customer Number ">
                                <EditItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("customer_num") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("customer_num") %>'></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                <asp:Label ID="textcustomer" runat ="server">
                                </asp:Label>
                                
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Service Code">
                                <EditItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("service_code") %>'></asp:Label>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("service_code") %>'></asp:Label>
                                </ItemTemplate>
                                <FooterTemplate>
                                <asp:Label ID="txtservicecode" runat ="server" >
                                </asp:Label>
                                
                                </FooterTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Quantity">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("qty") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("qty") %>'></asp:Label>
                                </ItemTemplate>
                                
                                <FooterTemplate>
                                <asp:TextBox ID="txtqty" runat="server">
                                </asp:TextBox>
                                
                                </FooterTemplate>
                            </asp:TemplateField>
                            
                            <asp:TemplateField HeaderText ="ACTION">
                             <FooterStyle HorizontalAlign="Right" />
                             <ItemTemplate>
                                    <asp:LinkButton ID ="BTNADD" runat="server"   CommandName="ADD" Text ="ADD" >insert</asp:LinkButton>
                             </ItemTemplate>
                            <FooterTemplate >
                            <asp:LinkButton ID ="BTNADD" runat="server"   CommandName="ADD" Text ="ADD">insert</asp:LinkButton>
                            </FooterTemplate>
                            <EditItemTemplate>
                                <asp:LinkButton ID ="BTNADD" runat="server"   CommandName="ADD" Text ="ADD" >insert</asp:LinkButton>
                            </EditItemTemplate>
                            </asp:TemplateField>
                          
                            <asp:CommandField ShowEditButton="True" />
                           
                            <asp:CommandField ShowSelectButton="True" />
                        </Columns>
                        
                        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                       
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                    </asp:GridView>


and this is my code behind

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("ADD"))
{

TextBox txtAddtransaction_type = (TextBox)GridView1.FooterRow.FindControl("txtAddtransaction_type");
t.Trans_type = txtAddtransaction_type.Text;
TextBox txttransaction_date = (TextBox)GridView1.FooterRow.FindControl("txttransaction_date");
t.Trans_date = txttransaction_date.Text;
Label textcustomer = (Label)GridView1.FooterRow.FindControl("textcustomer");
t.Cust_no = Convert.ToInt32(textcustomer.Text.ToString());
Label txtservicecode = (Label)GridView1.FooterRow.FindControl("txtservicecode");
t.Obj.Service_code = Convert.ToInt32(txtservicecode.Text);
TextBox txtqty = (TextBox)GridView1.FooterRow.FindControl("txtqty");
t.Obj.Quanity = Convert.ToInt32(txtqty.Text);

int done = h.Add_transa(t);
}

}




each time running insert link when click it exception fire in
t.Cust_no = Convert.ToInt32(textcustomer.Text.ToString());

exception say input string waas not in correct format
i want to make the behaviour of insert link like edite button in case of clikikng it will open
anew row
i do not know how and i do not know if iam working in right way or not
could any one help me ?
Posted

 
Share this answer
 
Hi,

Your code is having error, so you got exception say "input string was not in correct format".

First of all you need to correct your code.

Sol 1:

Please replace following lines

Label textcustomer = (Label)GridView1.FooterRow.FindControl("textcustomer");
t.Cust_no = Convert.ToInt32(textcustomer.Text.ToString());

by

TextBox textcustomer = (TextBox)GridView1.FooterRow.FindControl("textcustomer");
t.Cust_no = Convert.ToInt32(textcustomer.Text.ToString());

So the error will be removed.

Sol 2:
Second thing I observed is, you are not assigning value to "textcustomer" in your footer template

<FooterTemplate>
<asp:Label ID="textcustomer" Text='<%# Bind("customer_num") %>' runat ="server">
</asp:Label>

</FooterTemplate>
 
Share this answer
 
Comments
fady_hasnaa 9-Apr-14 0:05am    
well , i got why the error happen because t.cust_no is null ,
but you did not get my point i want to make insert when clicking the link i mean i want an empty row then i can fill it with data this is the way i found on google to make insert link,
but it odes not work did you have if iam going in the right way or not ?
Bh@gyesh 9-Apr-14 0:35am    
Ok, I got what you want. you can refer following link to insert new row in grid with controls.

http://geekswithblogs.net/dotNETvinz/dotNETvinz/archive/2009/06/29/faq-dynamically-adding-rows-in-asp-table-on-button-click.aspx

http://geekswithblogs.net/casualjim/archive/2006/05/04/77151.aspx

if you are comfortable with javascript so you can use following link.

http://webcache.googleusercontent.com/search?q=cache:http://stackoverflow.com/questions/8490231/grid-view-add-new-blank-row-for-entry
fady_hasnaa 9-Apr-14 11:58am    
thanks my dear but i want to ask about other thing
in code in asp in linkboutton when i write onclick="functio_name"
and in code behind the header of the function name like this :
protected void function_name()
error said :No overload for 'insert_row' matches delegate 'System.EventHandler'
after search i found that to fix this you have to change the header of function to
protedted void function_name(object sender , Eventargs e )
it's working well , could you tell me why shall i add this ?
Bh@gyesh 10-Apr-14 0:28am    
Hi,
The reason is, when u declare any server side control event (i.e. linkbutton click), the code behind function should know from which control this even thas been called.
If you have idea of delegates so you can understand very well of this concept. Actually sender has all properties of control has been called.
This mechanism is called event handler.

You can get more idea from following link:
http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/system.eventhandler.aspx
fady_hasnaa 14-Apr-14 2:01am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900