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.
Thanks!
James
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