CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2007
    Posts
    39

    GridView RowDeleting

    Hi I have the following gridview which contains a CheckBoxField. Now when delete is clicked I want to do an if statement to see if the checkbox is checked and if so cancel the delete and display an error message.

    Not quite sure how to do this.

    Gridview code:
    Code:
    <asp:GridView ID="SharedGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlManageSharedArea" CellPadding="4" ForeColor="#333333" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" datakeynames="doc_area_id" onrowdeleted="SharedGridView_RowDeleted" onrowdeleting="SharedGridView_RowDeleting" AllowPaging="True" AllowSorting="True" PageSize="5" style="position: static">
                
                <Columns>
                    <asp:BoundField DataField="doc_area_name" HeaderText="Name" SortExpression="doc_area_name" />
                    <asp:templatefield headertext="Type">
    	                <itemtemplate>
    		                <%# GetAreaTypeSharedString(Eval("doc_area_type")) %>
    	                </itemtemplate>
                    </asp:templatefield> 
                    <asp:CheckBoxField DataField="doc_area_default" HeaderText="Default" SortExpression="doc_area_default" />
                    <asp:HyperLinkField Text="<img src='../images/edit.gif' border='0' />" DataNavigateUrlFormatString="EditSharedDocumentArea.aspx?doc_area_id={0}" DataNavigateUrlFields="doc_area_id" />
                    <asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/Delete.gif" DeleteText="Delete Area" ShowDeleteButton="True">
                        <ItemStyle HorizontalAlign="Center" Width="20px" />
                    </asp:CommandField>
                </Columns>
    </asp:GridView>
    If statement in code behind:
    Code:
    protected void SharedGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            if (SharedGridView ??????????
            {
                e.Cancel = true;
                MsgShared.Text = "The area has not been deleted. You may not delete the Shared Area Default.";
            }
        }
    Thanks for any help you can give!

  2. #2
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: GridView RowDeleting

    I am struggling to understand why you need a delete button on every row as well as a checkbox. Surely you either need checkboxes with single button, or buttons on every row and no checkboxes?

    Anyway,

    Heres how I would do it with 1 delete button. This will show you how to check if a certain row's checkbox is checked. Although, for this to work - you need to give you CheckBoxField a name (called chkBox in this code).
    Code:
        protected void buttonDelete_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow r in SharedGridView.Rows)
            {
                CheckBox cBox = r.Controls.ToString("chkBox");
                if (cBox.Checked)
                {
                    // Delete Row? Heres your doc_area_id.
                    String docAreaID = r.DataItemIndex;
                }
                else
                {
                    // Probably best to just not delete if not selected.
                    // Your user could be tied into pressing no a load of times otherwise.
                };
            };
        }
    Hope that is useful.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  3. #3
    Join Date
    Feb 2007
    Posts
    39

    Re: GridView RowDeleting

    No, the checkbox is not for the user to check if he wants to delete it. The checkbox is not editable, only one checkbox is ever checked in the gridview (from a value from the database).

    I want it so if the delete button is clicked and the checkbox is checked then just to display an error message but if its not checked then to delete.

    Cheers, Mark

  4. #4
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: GridView RowDeleting

    Oh right, sorry..

    makes sense now

    Try something like this:
    Code:
        protected void SharedGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow r = SharedGridView.Rows(e.RowIndex);
            CheckBox cBox = r.Controls("chkBox");
            if (cBox.Checked)
            {
                e.Cancel;
            };
        }
        protected void SharedGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            CheckBox cBox = e.Row.Controls("chkBox");
            if (cBox.Checked)
            {
                // Get your delete button
                ImageButton iBtn = e.Row.Cells(4).Controls(0);
                // You could make it invisible i.e
                // iBtn.Visible = false;
                // Or you can add a javascript alert
                iBtn.Attributes.Add("onclick", "alert('You cant delete this');");
            }
        }
    HTH
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  5. #5
    Join Date
    Mar 2000
    Location
    Vancouver, BC, Canada
    Posts
    278

    Re: GridView RowDeleting

    sort of a hibrid of my own stuff mixed with the dot net:

    Code:
    protected void deleteBookmarks(object sender, ImageClickEventArgs e)
        {
            DataKeyArray aryKeys = GridView1.DataKeys;//these need to be defined in the aspx file gridview attributes
    
    
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                CheckBox RowCheckBox = (CheckBox)gvr.FindControl("bookmarkID");//bookmarkID is the primary key value I have defined as a non-visible column representing a unique identifier in the database
                if (RowCheckBox.Checked == true)
                {
                    deleteBookmark(long.Parse(aryKeys[gvr.DataItemIndex].Value.ToString()));//this is a call to my own function to delete the row from the database
                    GridView1.DeleteRow(gvr.RowIndex);
                }
            }
        }
    David Meikle
    Quantum Unit Solutions, LLC
    www.quantumunit.com

  6. #6
    Join Date
    Feb 2007
    Posts
    39

    Re: GridView RowDeleting

    Thanks for the replies, I have now got the following message from doing the below code:
    'System.Web.UI.WebControls.GridView.Rows' is a 'property' but is used like a 'method'


    Code:
    <asp:GridView ID="SharedGridView" runat="server" AutoGenerateColumns="False" DataSourceID="SqlManageSharedArea" CellPadding="4" ForeColor="#333333" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" datakeynames="doc_area_id" onrowdatabound="SharedGridView_RowDataBound" onrowdeleted="SharedGridView_RowDeleted" AllowPaging="True" AllowSorting="True" PageSize="5" style="position: static">
                
                <Columns>
                    <asp:BoundField DataField="doc_area_name" HeaderText="Name" SortExpression="doc_area_name" />
                    <asp:templatefield headertext="Type">
    	                <itemtemplate>
    		                <%# GetAreaTypeSharedString(Eval("doc_area_type")) %>
    	                </itemtemplate>
                    </asp:templatefield> 
                    <asp:CheckBoxField DataField="doc_area_default" HeaderText="Default" SortExpression="doc_area_default" />
                                    
                    <asp:HyperLinkField Text="&lt;img src='../images/edit.gif' border='0' /&gt;" DataNavigateUrlFormatString="EditSharedDocumentArea.aspx?doc_area_id={0}" DataNavigateUrlFields="doc_area_id" />
                    <asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/Delete.gif" DeleteText="Delete Area" ShowDeleteButton="True">
                        <ItemStyle HorizontalAlign="Center" Width="20px" />
                    </asp:CommandField>
                </Columns>
            </asp:GridView>
    Code:
    protected void SharedGridView_RowDeleted(Object sender, GridViewDeletedEventArgs e)
        {
            // Display whether the delete operation succeeded.
            if (e.Exception == null)
            {
                MsgShared.Text = "The Shared Area has been deleted successfully.";
            }
            else
            {
                MsgShared.Text = "An error occurred while attempting to delete the area.";
                e.ExceptionHandled = true;
            }
        }
        protected void SharedGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow r = SharedGridView.Rows(e.RowIndex);
            CheckBox cBox = r.Controls("chkBox");
            if (cBox.Checked)
            {
                e.Cancel;
            };
        }
        protected void SharedGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
        {
            CheckBox cBox = e.Row.Controls("chkBox");
            if (cBox.Checked)
            {
                // Get your delete button
                ImageButton iBtn = e.Row.Cells(4).Controls(0);
                // You could make it invisible i.e
                // iBtn.Visible = false;
                // Or you can add a javascript alert
                iBtn.Attributes.Add("onclick", "alert('You cant delete this');");
            }
        }
    Cheers, Mark

  7. #7
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: GridView RowDeleting

    Which line throws the error?

    ps: maybe you need to change your checkbox field into a regualar checkbox inside a template field. Remember to call the checkbox "chkBox".
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  8. #8
    Join Date
    Feb 2007
    Posts
    39

    Re: GridView RowDeleting

    Hi, that message is referring to the line:
    GridViewRow r = SharedGridView.Rows(e.RowIndex);

    I have now changed the checkbox field to:

    Code:
    <asp:TemplateField HeaderText="Default" SortExpression="doc_area_default">
                    <ItemTemplate>
                    <asp:CheckBox id="chkBox" runat="server" value="<%# Eval("doc_area_default") %>"></asp:CheckBox>
                    </ItemTemplate>
                    </asp:TemplateField>
    And am getting the following message:
    The server tag is not well formed.

    Have I done the value bit correct? should there be a DataField somewhere in here like there was in the checkbox field?

    Mark

  9. #9
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: GridView RowDeleting

    Try your template field like this:
    Code:
    <asp:TemplateField HeaderText="Default" SortExpression="doc_area_default">
      <ItemTemplate>
        <asp:CheckBox id="chkBox" runat="server" checked="<%# Eval('doc_area_default') %>" />
        </ItemTemplate>
    </asp:TemplateField>
    I have only recently started getting into c#. Before, I was big into vb, so I am still making a few syntax mistakes. Try the error raising line like this maybe:
    Code:
    GridViewRow r = SharedGridView.Rows[e.RowIndex];
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  10. #10
    Join Date
    Feb 2007
    Posts
    39

    Re: GridView RowDeleting

    Hi, I now have:

    Too many characters in character literal

    pointing to the line:
    <asp:CheckBox id="chkBox" runat="server" checked="<%# Eval('doc_area_default') %>" />

    Cheers, Mark

  11. #11
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: GridView RowDeleting

    Exactly what data is in doc_area_default? should be 'true' or 'false' or '1' or '0'
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  12. #12
    Join Date
    Feb 2007
    Posts
    39

    Re: GridView RowDeleting

    yep it is either a 1 or a 0

  13. #13
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: GridView RowDeleting

    Instead of
    Code:
    <%# Eval('doc_area_default') %>
    try
    Code:
    <%# DataBinder.Eval (Container.DataItem, 'doc_area_default') %>
    Last edited by HairyMonkeyMan; February 21st, 2007 at 09:19 AM.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  14. #14
    Join Date
    Feb 2007
    Posts
    39

    Re: GridView RowDeleting

    No, its still saying Too many characters in character literal on the same line.

  15. #15
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: GridView RowDeleting

    Not sure what the problem could be. Would it be possible to take a look at the project?
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured