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

GridView problems

Status
Not open for further replies.

JimmyFo

Programmer
Feb 14, 2005
102
US
Hi, I have a gridview that is causing some problems. I have a few questions.

1) Is it possible to have a custom error message? I have autogeneratedeletecolumn set to true, but in some cases I may not be able to delete an item (foreign key restraint, etc) - can I show a custom error message? Perhaps set a label to a value?

2) This is complex. I have image button fields in my gridview; when they are clicked I get the row index, get the value of the first cell which is my datakey, and use that value as my key for the command executed. My first column is my datakey column, but for some reason, I have to access the selectedRow.Cells(1) instead of selectedRow.Cells(0) - why is this? Is this because I have the edit/delete columns generated?

3) When I click edit/delete, it executes fine. But if I click update/cancel in the edit box, I no longer have the ability to access my selected row values, so it errors out saying that my "portfolioCell.Text" in the code below is empty. Ideas? Why can't I access it? Row_Command is there to handle the linkbuttons, which work fine - only the "update" or "cancel" don't work.

Code:
        <asp:UpdatePanel ID="update_panel1" runat="server"><ContentTemplate>
        <asp:GridView ID="grid" runat="server" Width="100%" Height="100%"
            ShowHeader="true"
            AutoGenerateColumns="false"
            AutoGenerateDeleteButton="true"
            AutoGenerateEditButton="true"
            DataSourceID="DSportfolio"
            DataKeyNames="intPortfolioID"
            AllowSorting = "true"
            AllowPaging = "true" PageSize="25"
            EnableSortingAndPagingCallbacks="true"
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <Columns>
                <asp:BoundField DataField="intPortfolioID" HeaderStyle-CssClass="rowID" ItemStyle-CssClass="rowID" Visible="true" />
                <asp:BoundField DataField="strPortfolioName" HeaderText="Name" SortExpression="strPortfolioName" />
                <asp:BoundField DataField="Created" HeaderText="Created By" SortExpression="Created" />
                <asp:BoundField DataField="CreatedDate" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false" HeaderText="Created Date" SortExpression="Created" />
                <asp:ButtonField ButtonType="Image" CommandName="Redirect" ImageUrl="~/images/status/clipboard_edit.png" />
            </Columns>
        </asp:GridView>
        </ContentTemplate></asp:UpdatePanel>
Code:
Protected Sub grid_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grid.RowCommand
        'Retrieve the index of the row selected
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim selectedRow As GridViewRow = grid.Rows(index)
        'Get the value of the selected row.
        'This differes from the other method of assigning a command argument to an item then retrieving it.
        'Selected row is "1" here as opposed to "0" in other screens because "0" is the edit/delete column
        Dim portfolioCell As TableCell = selectedRow.Cells(1)
        Dim id As Integer = portfolioCell.Text
Code:
        <asp:SqlDataSource ID="DSportfolio" ConnectionString="<%$ AppSettings:SQLConnection1 %>"
            SelectCommand="uspSELECT_PORTFOLIO"
            SelectCommandType="StoredProcedure"
            DeleteCommand="uspDELETE_PORTFOLIO"
            DeleteCommandType="StoredProcedure"
            UpdateCommand="uspUPDATE_PORTFOLIO"
            UpdateCommandType="StoredProcedure"
            runat="server">
            <UpdateParameters>
                <asp:Parameter Name="p_CURRENTUSER" />
            </UpdateParameters>
        </asp:SqlDataSource>

Thanks!
James
 
1.) Yes.. Use Try catch blocks to handle your errors.
2.)
Is this because I have the edit/delete columns generated?
Yes.
3.)Try using the Row_Updating event.
 
1) Where do I put the try catch blocks? Since the columns are autogenerated, there is no function call per se that I can access - RowDeleting and RowDeleted don't have that functionality.

2 and 3) Since they are autogenerated, I had moved the selectedRow.Cells from 0 to 1, but how come when I click "cancel" or "update" after clicking "edit", I can no longer access the value? When I go through the debug, the value is
 
Put try catch blocks around any code that accessed your database.

To get the values, use the Row_Deleting event of the grid.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top