Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

i am using grid view with insert,update and delete template fields.

when i am opening the page the add,edit and delete button is show.
i want if no data data in gird view i want to show only ADD button and if there is data in grid view so i want ADD,EDIT and DELETE button.
C#
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames = "no"
                       AutoGenerateColumns="False" CellPadding="3"
                       Width="100%" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound" OnPageIndexChanging="GridView1_PageIndexChanging" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1">
                       <Columns>                       
                         <asp:TemplateField HeaderText = "Serial No.">
                        <ItemTemplate>
                        <%# ((GridViewRow)Container).RowIndex + 1%>
                        </ItemTemplate>
                             <FooterStyle Width="6%" />
                             <ItemStyle Width="6%" />
                        </asp:TemplateField> 
                        <asp:BoundField DataField="no" HeaderText = "NO" Visible="False" /> 
                       <asp:TemplateField HeaderText ="MileStone">                        
                       <EditItemTemplate>                 
                       <asp:TextBox ID="txtmilestone" runat="server" Text='<%#Bind("milestone") %>' Height="10%"></asp:TextBox>
                       <asp:RequiredFieldValidator ID="RFVmilestone" runat="server" ErrorMessage="*" ValidationGroup="Gridview" ControlToValidate="txtmilestone"></asp:RequiredFieldValidator>
                         
                         
                       </EditItemTemplate>
                       <ItemTemplate>
                       <asp:Label ID="lblmilestone" runat="server" Text='<%#Eval("milestone") %>'></asp:Label>
                       </ItemTemplate>
                       <FooterTemplate>                      
                       <asp:TextBox ID="txtfootermilestone" runat="server"></asp:TextBox>
                       <asp:RequiredFieldValidator ID="RFVmilestone" runat="server" ErrorMessage="*" ValidationGroup="Gridview" ControlToValidate="txtfootermilestone"></asp:RequiredFieldValidator>
                         
                         
                       </FooterTemplate>
                       <ControlStyle Width="70%" />
                       <FooterStyle Width="18%" />
                           <HeaderStyle Width="18%" />
                           <ItemStyle Width="18%" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText = "Completion Date">
                       <EditItemTemplate>                                         
                       <asp:TextBox ID="txtcompletiondate" runat="server" class="date-pick" Text='<%#Bind("completiondate") %>'></asp:TextBox>
                       <asp:RequiredFieldValidator ID="RFVcompletiondate" runat="server" ErrorMessage="*" ValidationGroup="Gridview" ControlToValidate="txtcompletiondate"></asp:RequiredFieldValidator>
                       </EditItemTemplate>
                       <ItemTemplate>
                       <asp:Label ID="lblcompletiondate" runat="server" Text='<%#Eval("completiondate","{0:dd-MM-yyyy}") %>'></asp:Label>
                       </ItemTemplate>
                       <FooterTemplate>                                      
                       <asp:TextBox ID="txtfootercompletiondate" runat="server" class="date-pick"></asp:TextBox>
                       <asp:RequiredFieldValidator ID="RFVcompletiondate" runat="server" ErrorMessage="*" ValidationGroup="Gridview" ControlToValidate="txtfootercompletiondate"></asp:RequiredFieldValidator>
                       </FooterTemplate>
                           <ControlStyle Width="80%" />
                           <FooterStyle Width="23%" />
                           <HeaderStyle Width="23%" />
                           <ItemStyle Width="23%" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="Payment(%)">
                       <EditItemTemplate>                      
                       <asp:TextBox ID="txtpaymentpercent" runat="server" Text='<%#Bind("paymentpercent") %>' Height="10%"></asp:TextBox>
                       <asp:RequiredFieldValidator ID="RFVpaymentpercent" runat="server" ErrorMessage="*" ValidationGroup="Gridview" ControlToValidate="txtpaymentpercent"></asp:RequiredFieldValidator>
                       <asp:RangeValidator ID="RVtxtpaymentpercent" runat="server" ErrorMessage="Invalid Payment(%)" ValidationGroup="Gridview" ControlToValidate="txtpaymentpercent" Type="Double" MinimumValue="0" MaximumValue="100"></asp:RangeValidator>
                         
                         
                       </EditItemTemplate>
                       <ItemTemplate>
                       <asp:Label ID="lblpaymentpercent" runat="server" Text='<%#Eval("paymentpercent") %>'></asp:Label>
                       </ItemTemplate>
                       <FooterTemplate>                      
                      <asp:TextBox ID="txtfooterpaymentpercent" runat="server"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RFVpaymentpercent" runat="server" ErrorMessage="*" ValidationGroup="Gridview" ControlToValidate="txtfooterpaymentpercent"></asp:RequiredFieldValidator>
                        <asp:RangeValidator ID="RVpaymentpercent" runat="server" ErrorMessage="Invalid Payment(%)" ValidationGroup="Gridview" ControlToValidate="txtfooterpaymentpercent" Type="Double" MinimumValue="0" MaximumValue="100"></asp:RangeValidator>
                          
                          
                       </FooterTemplate>
                            <ControlStyle Width="70%" />
                           <FooterStyle Width="18%" />
                           <HeaderStyle Width="18%" />
                           <ItemStyle Width="18%" />
                       </asp:TemplateField>
                       <asp:TemplateField HeaderText="Payment(Rs)">
                       <EditItemTemplate>     
                       <asp:TextBox ID="txtpaymentrupee" runat="server" Text='<%#Bind("paymentrupee") %>' Height="10%"></asp:TextBox>
                       <asp:RequiredFieldValidator ID="RFVpaymentrupee" runat="server" ErrorMessage="*" ValidationGroup="Gridview" ControlToValidate="txtpaymentrupee"></asp:RequiredFieldValidator>
                       <asp:RangeValidator ID="RVtxtpaymentrupee" runat="server" ErrorMessage="Invalid Payment(Rs)" ValidationGroup="Gridview" ControlToValidate="txtpaymentrupee" Type="Integer" MinimumValue="0" MaximumValue="100000000"></asp:RangeValidator>
                         
                         
                       </EditItemTemplate>
                       <ItemTemplate>
                       <asp:Label ID="lblpaymentrupee" runat="server" Text='<%#Eval("paymentrupee") %>'></asp:Label>
                       </ItemTemplate>
                       <FooterTemplate>                      
                       <asp:TextBox ID="txtfooterpaymentrupee" runat="server"></asp:TextBox>
                       <asp:RequiredFieldValidator ID="RFVpaymentrupee" runat="server" ErrorMessage="*" ValidationGroup="Gridview" ControlToValidate="txtfooterpaymentrupee"></asp:RequiredFieldValidator>
                       <asp:RangeValidator ID="RVpaymentrupee" runat="server" ErrorMessage="Invalid Payment(Rs)" ValidationGroup="Gridview" ControlToValidate="txtfooterpaymentrupee" Type="Integer" MinimumValue="0" MaximumValue="100000000"></asp:RangeValidator>
                         
                         
                       </FooterTemplate>   
                        <ControlStyle Width="70%" />                       
                           <FooterStyle Width="18%" />
                           <HeaderStyle Width="18%" />
                           <ItemStyle Width="18%" />
                       </asp:TemplateField>
                       <asp:TemplateField ShowHeader="False">
                       <ItemTemplate>
                       <asp:LinkButton ID="lnkadd" runat="server" CausesValidation="false" CommandName="Add" Text="Add"></asp:LinkButton>
                       </ItemTemplate>
                       <FooterTemplate>
                       <asp:LinkButton ID="lnkfootersave" runat="server" CausesValidation="true" ValidationGroup="Gridview" Text="Save" CommandName="Save"></asp:LinkButton>
                        <asp:LinkButton ID="lnksavecancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                       </FooterTemplate>
                       </asp:TemplateField>
                       <asp:TemplateField ShowHeader="False">
                       <EditItemTemplate>
                       <asp:LinkButton ID="lnkupdate" runat="server" CausesValidation="true" ValidationGroup="Gridview" CommandName="Update" Text="Update"></asp:LinkButton>
                       <asp:LinkButton ID="lnkcancel" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                       </EditItemTemplate>
                       <ItemTemplate>
                       <asp:LinkButton ID="lnkedit" runat="server" Visible="true" CausesValidation="false" CommandName="Edit" Text="Edit"></asp:LinkButton>
                       </ItemTemplate>
                       </asp:TemplateField>
                       <asp:TemplateField ShowHeader="False">
                       <ItemTemplate>
                       <asp:LinkButton ID="lnkdelete" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this Record?');"></asp:LinkButton>                       
                       </ItemTemplate>
                       </asp:TemplateField>
                       </Columns>
                       <RowStyle BackColor="#DEDFDE" HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="Black" Width="100%" />
                       <FooterStyle BackColor="#C6C3C6" HorizontalAlign="Center" VerticalAlign="Middle" ForeColor="Black" Width="100%" Wrap="True" />
                       <PagerStyle ForeColor="Black" HorizontalAlign="Right" BackColor="#C6C3C6" />
                       <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" Width="100%" />
                       <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" HorizontalAlign="Center" VerticalAlign="Middle" />
                       <EmptyDataRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                       <EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="100%" />
                   </asp:GridView>


Thanks in Advance.
Posted

Hi,

in page Load event you can write code to disable your buttons or even you can generate dynamic Edit/Delete button in page load event.

check if number of rows are zero then do not create Edit and Delete button.

hope this will help you,

Thanks
-amit.
 
Share this answer
 
Comments
mayankshrivastava 10-Apr-12 5:34am    
i am using this but it gives error
"Object reference not set to an instance of an object. "

if (GridView1.Rows.Count >= 1)
{
LinkButton lnkedit = (LinkButton)GridView1.FindControl("lnkedit");
lnkedit.Visible = true;
}
AmitGajjar 10-Apr-12 5:40am    
on which line you are getting this error ?
mayankshrivastava 11-Apr-12 5:05am    
lnkedit.visible =true;

there gives a error.
AmitGajjar 11-Apr-12 5:22am    
that is because lnkedit button is not found. check if the control with the same name exist. and also debug your code and check if lnkedit is null or not.
in page load method your code need to be like below:

C#
if (GridView1.Rows.Count >= 1)
{
foreach(GridViewRow gr in GridView1.Rows)
{
       LinkButton lnkedit = (LinkButton)gr.FindControl("lnkedit");
       lnkedit.Visible = true;

}       
}
 
Share this answer
 
Comments
mayankshrivastava 11-Apr-12 5:06am    
gives a error "Object reference not set to an instance of an object. "
on lnkedit.Visible = true;
Well for this you need to first bind gird and than need to check number of row in grid so you code will be



C#
private void BindGrid()
{
   gridview.DataSource = datasource;
   gridview.DataBind();

   if(gridview.Rows.Count <=0)
   {
      btnAd.Visible= true;
      btnDel.Visible= false;
      btnUP.Visible= false;
   } 
}
 
Share this answer
 
Before binding the grid, u can check as follows...
C#
// Check whether gridview is empty or not from the dataset
// here ds is a dataset which holds values from database tables.
if (ds.Tables[0].Rows.Count == 0)
{
      // Gridview is empty now.
      // Add ur logic like below.
      // The column no 6 (start from 0) is 'lnkadd'.
     ((TemplateField)grdBuglist.Columns[6]).Visible = true;

}
else
{
      // Gridview is having data.
      // The column no 7 is 'lnkedit' and 8 is 'lnkdelete'.
      ((TemplateField)grdBuglist.Columns[7]).Visible = true;
      ((TemplateField)grdBuglist.Columns[8]).Visible = true;
}
grdBuglist.DataBind();

Happy coding....
 
Share this answer
 
you have to select data from table and bind it grid view
like
C#
if(gridview.rows.count>0)
{
foreach(datarow dr in gridview.rows)
{

else
{
((TemplateField)dr.Columns[6]).Visible =false;
}
 
Share this answer
 
v2

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