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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disable edit button in gridview 1

Status
Not open for further replies.

mrp9090

Programmer
May 22, 2006
71
GB
How do you disable the edit button for selected rows in a gridview? Here is my button :

<asp:CommandField ShowEditButton="True" ButtonType="Link" ShowCancelButton="True"
UpdateText="Update" EditText="Edit" CancelText="Cancel" />
 
Use the ItemDataBound event, get a reference to the button and change the ShowEditButton property.

As a side note, you've asked a lot of questions recently yet you haven't acknowledged any of the answers. To do so, read point 15 from the FAQ in my signature.


____________________________________________________________

Need help finding an answer?

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

 
Apologies, I'm in a mad rush to get my code working and I'm using the GridView control for the first time. I'll go back to the replies later and acknowledge the answers that were useful to me.

BTW, in the DataBound event , how do you reference the ShowEditButton property?

Thanks,

Mike
 
You can use FindControl in the ItemDataBound event to get a reference to the actual button. Then, you just set the property as normal e.g.
Code:
myButton.ShowEditButton = False


____________________________________________________________

Need help finding an answer?

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

 
Using this syntax in the RowDataBound event does not allow access to the ShowEditButton property :

EDIT BUTTON
LinkButton EditButton = (LinkButton)e.Row.Cells[8].Controls[0];

DELETE BUTTON
LinkButton DeleteButton = (LinkButton)e.Row.FindControl("DeleteButton");
 
Can you paste the full code (the HTML view) you are usign for your GridView?


____________________________________________________________

Need help finding an answer?

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

 
Sure, here's my gridiview :

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
SkinID="Grey" AutoGenerateColumns="false" DataKeyNames="ForecastKey" AllowSorting="true"
OnRowDataBound="GridView1_RowDataBound" EditRowStyle-CssClass="dgedit" OnRowUpdated="GridView1_RowUpdated" OnRowEditing="GridView1_RowEditing"
OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Key" SortExpression="ForecastKey">
<ItemTemplate>
<asp:Label ID="lblForecastKey" Text='<%# Bind("ForecastKey") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Company" SortExpression="CompanyName">
<ItemTemplate>
<asp:Label ID="lblCompany" Text='<%# Eval("CompanyName") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlCompanyName" DataSourceID="SqlDataSource2" Runat="Server"
DataTextField="CompanyName" DataValueField="CompanyKey" SelectedValue='<%# Bind("CompanyKey") %>' />
</EditItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Forecast Type" SortExpression="ForecastDescription">
<ItemTemplate>
<asp:Label ID="lblForecastType" Text='<%# Eval("ForecastDescription") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlForecastType" DataSourceID="SqlDataSource3" Runat="Server"
DataTextField="ForecastDescription" DataValueField="ForecastType" SelectedValue='<%# Bind("ForecastType") %>' />
</EditItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Value (£)" SortExpression="MoneyValue">
<ItemTemplate>
<asp:Label ID="lblMoneyValue" Text='<%# Eval("MoneyValue", "{0:#,###.00}") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMoneyValue" Text='<%# Bind("MoneyValue", "{0:#,###.00}") %>' runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtMoneyValue" Display="None" ErrorMessage="Please enter a Value" />
<asp:CompareValidator ID="CompareValidator1" runat="server" Display="None"
ErrorMessage="Value must be numeric" ControlToValidate="txtMoneyValue"
Type="Double" Operator="DataTypeCheck"></asp:CompareValidator>
</EditItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Probability (%)" SortExpression="ProbabilityValue">
<ItemTemplate>
<asp:Label ID="lblProbability" Text='<%# Eval("ForecastPercentage") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlForecastPercentage" DataSourceID="SqlDataSource4" Runat="Server"
DataTextField="ForecastPercentageDescription" DataValueField="ForecastPercentage" SelectedValue='<%# Bind("ForecastPercentage") %>' />
</EditItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Value (£) x Probability (%)" SortExpression="ProbabilityValue">
<ItemTemplate>
<asp:Label ID="lblProbabilityValue" Text='<%# Eval("ProbabilityValue", "{0:#,###.00}") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Due Date" SortExpression="DueDate">
<ItemTemplate>
<asp:Label ID="lblDueDate" Text='<%# Eval("DueDate", "{0:dd/MM/yyyy}") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDueDate" Text='<%# Bind("DueDate", "{0:dd/MM/yyyy}") %>' runat="server"></asp:TextBox>
<!--or use this in SQL : Select Convert(VarChar(10), GetDate(), 103)-->
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="txtDueDate" Display="None" ErrorMessage="Please enter a Due Date" />
<asp:CompareValidator ID="CompareValidator2" runat="server"
Display="None" ErrorMessage="Please enter a valid Due Date in format dd/mm/yyyy"
ControlToValidate="txtDueDate" Type="Date" Operator="DataTypeCheck"></asp:CompareValidator>
</EditItemTemplate>
<ItemStyle Height="24px" Width="65px" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Last Edit Date" SortExpression="LastEditDate">
<ItemTemplate>
<asp:Label ID="lblLastEditDate" Text='<%# Eval("LastEditDate", "{0:dd/MM/yyyy}") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle Height="24px" Width="50px" />
</asp:TemplateField>

<asp:CommandField ShowEditButton="True" ButtonType="Link" ShowCancelButton="True"
UpdateText="Update" EditText="Edit" CancelText="Cancel" />

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="DeleteButton"
CommandArgument='<%# Eval("ForecastKey") %>'
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
 
Are you checking the RowType as this works fine for me:
Code:
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim l As New LinkButton
            l = e.Row.Cells(8).Controls(0)
        End If


____________________________________________________________

Need help finding an answer?

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

 
I can create a reference to the button, I just can't acccess the ShowEditButton method, it does not appear in Intellisense.
 
Oh ok - just try setting the Visible property to False then.


____________________________________________________________

Need help finding an answer?

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

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top