views:

275

answers:

2

Ok... I have a database table called employees..

This has columns called ID, Name, datejoined and Cannotbedeleted (check boxes)...

Now i want to add a delete column which deletes the rows when clicked.

But there are some entries which cannot be deleted if the Cannotbedeleted field is true (checked)... so the delete button should be invisible for these rows.

Please tell me a way of how to do this...

            <asp:CheckBoxField DataField="CannotBeDeleted" HeaderText="CannotBeDeleted" 
                SortExpression="CannotBeDeleted" />
            <asp:BoundField DataField="TimeAdded" HeaderText="TimeAdded" 
                SortExpression="TimeAdded" />
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"   
                        CommandName="Delete" Text="Delete" ></asp:LinkButton> 
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

    </asp:GridView>

I also tried using in delete template field... ' >

But i did not know what to do in code behind?? protected bool GetShowDeleteButton() {

}

The solution is below.... but is there a way i can refresh the page once i click the delete button in the gridview....

A: 

Do it like this:

if($row['cannotbedeleted'] == true) {
$buttonDisplay = 'none';
}

echo "<button onclick='delete();' style='display: $buttonDisplay;'>Delete</button>";

Well that would be the solution in PHP but if the style of the button has "display: none;" in it the button will be hidden. :) HTH

hamstar
sorry but i dont know PHP... im doing this in C#..It will be great if this was in C#..thanks
+1  A: 

Try looping through the rows in GridView_DataBound, and hide the button for each row that has the checkbox checked.

protected void GridView1_DataBound(object sender, EventArgs e)
{
    foreach(GridViewRow myRow in GridView1.Rows)
    {
        //Find the checkbox
        CheckBox ckbox1 = (CheckBox)myRow.FindControl("nameOfCheckBox");
        if(ckbox1.Checked)
        {
            //Find the Delete linkbutton and hide it
            LinkButton deleteButton = (LinkButton)myRow.FindControl("nameOfDeleteLinkButton");
            deleteButton.Visible = false;
        }
    }
}

Now, here's the difference you need:

Implement the CheckBox column as a TemplateField with a CheckBox in it. You can bind the CheckBox to the data from your datasource.

Matthew Jones
yes i thought so...waiting for ur samplecode...thanks
Does this do what you want?
Matthew Jones
error... Object reference not set to an instance of an object.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:Line 167://Find the checkboxLine 168:CheckBox ckbox1 = (CheckBox)myRow.FindControl("CanBeDeleted");Line 169:if (ckbox1.Checked)Line 170:{Line 171: //Find the Delete linkbutton and hide it
That error most likely occurred because you did not make the CheckBox column a TemplateColumn. You need to do that, place a CheckBox in the template, bind the checkbox to the appropriate field in the database, and then replace 'nameOfCheckBox' with the name of the check box.
Matthew Jones
yup.. il try that now and get back..thanks
one more thing... the Cannotbedeleted column is a checkbox column...
Right. That one is the one that should be changed to a template field.
Matthew Jones
yes i figued it out and now its working... thanks a lot man
one more thing... how can i refresh the page if i click the delete button...