Click here to Skip to main content
15,569,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a column in gridview and if the value is "Yes" then the gridview should not editable else user can edit.

I tried 2 different methods in row command and row data bound.

What I have tried:

protected void grdChangeRequirement_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
        Label myTextBox = row.FindControl("lbl_SignOff") as Label;

        if (myTextBox.ToString() == "Yes")
        {
            Label btnUpdate = row.FindControl("btn_Update") as Label;
            Label btnCancel = row.FindControl("btn_Cancel") as Label;

            btnUpdate.Visible = false;
            btnCancel.Visible = false;
        }

    }


protected void grdChangeRequirement_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           Label btnUpdate = (e.Row.FindControl("btn_Update") as Label);
           Label btnCancel = (e.Row.FindControl("btn_Cancel") as Label);
           if (e.Row.Cells[12].Text == "Yes")
           {
               btnUpdate.Enabled = false;
           }
       }

   }
Posted
Updated 20-Aug-22 13:26pm
v3
Comments
Karthik_Mahalingam 30-Oct-17 9:04am    
post the markup of gridview
Renjith_R 31-Oct-17 6:25am    
<asp:GridView ID="grdChangeRequirement" runat="server" AutoGenerateColumns="False"
Width="100%" CellPadding="0" CellSpacing="0" BorderStyle="None" BorderWidth="0"
AllowPaging="true" OnRowCancelingEdit="grdChangeRequirement_RowCancelingEdit" OnRowDeleting="grdChangeRequirement_RowDeleting"
OnRowEditing="grdChangeRequirement_RowEditing" OnRowUpdating="grdChangeRequirement_RowUpdating"
OnPageIndexChanging="grdChangeRequirement_PageIndexChanging" OnRowDataBound="grdChangeRequirement_RowDataBound"
OnRowCommand="grdChangeRequirement_RowCommand" OnSelectedIndexChanged="grdChangeRequirement_SelectedIndexChanged">
<columns>

<asp:TemplateField HeaderText="SignOff" ItemStyle-BorderStyle="Solid"
ItemStyle-HorizontalAlign="Center" HeaderStyle-BorderStyle="Solid" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Width="6%" HeaderStyle-Height="10%">
<itemtemplate>
<asp:Label ID="lbl_SignOff" runat="server" Text='<%#Eval("Signoff") %>'>


<edititemtemplate>
<asp:DropDownList ID="ddl_Signoff" CssClass="dropdown" runat="server" SelectedValue='<%# Eval("Signoff") %>'>
<asp:listitem>--Select--
<asp:listitem>Yes
<asp:listitem>No
<asp:listitem>NA






<asp:TemplateField ItemStyle-BorderStyle="Solid" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BorderStyle="Solid" HeaderStyle-HorizontalAlign="Center">
<itemtemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" CssClass="input_button"
CommandArgument='<%# Container.DataItemIndex %>' />

<edititemtemplate>
<asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update" CssClass="input_button"
CausesValidation="false" />
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel" CssClass="input_button"
CausesValidation="false" />




Karthik_Mahalingam 31-Oct-17 6:37am    
check the solution

I think, you need the "RowDataBound event" og grid.
C#
protected void grdChangeRequirement_RowDataBound(object sender, GridViewRowEventArgs e)  
{ 
    //Code to get the textbox value [To Do]
    //Check the valeu 
    if (myTextBox.ToString() == "Yes")
    {
    // Code to disable the edit..
    }
}
 
Share this answer
 
v3
Comments
Renjith_R 30-Oct-17 8:17am    
Here the issue is not with editing or deleting. I want to disable the edit option based on the column condition. if you have any workaround please post that.
Thanks in advance :)
Sibasisjena 30-Oct-17 9:32am    
Please check, the soulution is updated. I think you need the RowDataBound event in the grid. Checked your code you have used this one. What is the exact problem you are facing? Your 2nd method should work. Put a debugger in the grdChangeRequirement_RowDataBound event and let me know, whether the control is going there or not.
Renjith_R 30-Oct-17 11:06am    
I have tried all those. While clicking on edit button it doesn't allow me to get inside the loop.
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label btnUpdate = (e.Row.FindControl("btn_Update") as Label);
Label btnCancel = (e.Row.FindControl("btn_Cancel") as Label);
if (e.Row.Cells[12].Text == "Yes")
{
btnUpdate.Enabled = false;
}
}
Sibasisjena 31-Oct-17 2:16am    
You mean, method grdChangeRequirement_RowDataBound is not executed while page load?

The process:
1. On page load the grdChangeRequirement_RowDataBound method would be executed for all rows recursively.
2. Inside this method you need to check the myTextBox.ToString()
3. If the value is "Yes", then only make the edit button disable.
Then it should work. Please post your Gridview markup.
Renjith_R 31-Oct-17 4:49am    
I have mentioned it doesn't allow me to get inside the If loop.

<asp:GridView ID="grdChangeRequirement" runat="server" AutoGenerateColumns="False"
Width="100%" CellPadding="0" CellSpacing="0" BorderStyle="None" BorderWidth="0"
AllowPaging="true" OnRowCancelingEdit="grdChangeRequirement_RowCancelingEdit" OnRowDeleting="grdChangeRequirement_RowDeleting"
OnRowEditing="grdChangeRequirement_RowEditing" OnRowUpdating="grdChangeRequirement_RowUpdating"
OnPageIndexChanging="grdChangeRequirement_PageIndexChanging" OnRowDataBound="grdChangeRequirement_RowDataBound"
OnRowCommand="grdChangeRequirement_RowCommand" OnSelectedIndexChanged="grdChangeRequirement_SelectedIndexChanged">
<columns>

<asp:TemplateField HeaderText="SignOff" ItemStyle-BorderStyle="Solid"
ItemStyle-HorizontalAlign="Center" HeaderStyle-BorderStyle="Solid" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Width="6%" HeaderStyle-Height="10%">
<itemtemplate>
<asp:Label ID="lbl_SignOff" runat="server" Text='<%#Eval("Signoff") %>'>


<edititemtemplate>
<asp:DropDownList ID="ddl_Signoff" CssClass="dropdown" runat="server" SelectedValue='<%# Eval("Signoff") %>'>
<asp:listitem>--Select--
<asp:listitem>Yes
<asp:listitem>No
<asp:listitem>NA






<asp:TemplateField ItemStyle-BorderStyle="Solid" ItemStyle-HorizontalAlign="Center"
HeaderStyle-BorderStyle="Solid" HeaderStyle-HorizontalAlign="Center">
<itemtemplate>
<asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" CssClass="input_button"
CommandArgument='<%# Container.DataItemIndex %>' />

<edititemtemplate>
<asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update" CssClass="input_button"
CausesValidation="false" />
<asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel" CssClass="input_button"
CausesValidation="false" />



There are multiple columns in the grid and i have mentioned only the single column within the grid.
try
protected void grdChangeRequirement_RowDataBound(object sender, GridViewRowEventArgs e)
       {

           if (e.Row.RowType == DataControlRowType.DataRow) {
               GridViewRow row = e.Row;
               Label lbl_SignOff = row.FindControl("lbl_SignOff") as Label;
               Label btnUpdate = row.FindControl("btn_Update") as Label;
               Label btnCancel = row.FindControl("btn_Cancel") as Label;
               if (lbl_SignOff.Text == "Yes")
               {
                   btnUpdate.Visible = false;
                   btnCancel.Visible = false;
               }
           }
       }
 
Share this answer
 
Comments
Renjith_R 31-Oct-17 11:29am    
This is partially working. Now the problem is only columns with values "Yes" are displaying in the grid. i called the grid binding function on the page load.

Please assist
Thanks in advance
Karthik_Mahalingam 31-Oct-17 12:17pm    
Show the page load event code
Renjith_R 1-Nov-17 4:02am    
if (!IsPostBack)
{
lblSuccess.Text = "";
ShowData();
DropdownBind();

}
Karthik_Mahalingam 1-Nov-17 4:35am    
everything looks fine
which part is not working
Renjith_R 1-Nov-17 4:40am    
On page load event the grid is binding with value Yes in the column. before it used to bind all the records.

protected void grdChangeRequirement_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow) {
GridViewRow row = e.Row;
Label lbl_SignOff = row.FindControl("lbl_SignOff") as Label;
Label btnUpdate = row.FindControl("btn_Update") as Label;
Label btnCancel = row.FindControl("btn_Cancel") as Label;
if (lbl_SignOff.Text == "Yes")
{
btnUpdate.Visible = false;
btnCancel.Visible = false;
}
}
}

if i comment/Skip this section it is binding properly.
var btnUpdate = (LinkButton)e.Row.Cells[0].Controls[0];
var btnCancel = (LinkButton)e.Row.Cells[0].Controls[2];
if (!string.IsNullOrEmpty(rowView["rAcknowOn"].ToString()))
{
btnUpdate.Visible = false;
btnCancel.Visible = false;
}
 
Share this answer
 

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