MadDev
MadDev

Reputation: 1150

How to return the selected row from gridview row command?

I cannot figure out what's wrong with my code. I have looked at multiple similar posts, with no luck. My code is returning the first row from the gridview. I want it to return the selected row. Can anybody see the problem?

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table cellpadding="0" cellspacing="0">
    <tr>
        <td class="grid-white-body-left"></td>
        <td class="grid-white-body-center">
            <asp:GridView ID="gvTradeCallOffList" runat="server" AutoGenerateColumns="False" EmptyDataText="No Trade Call-Off(s) found."
                GridLines="Horizontal" SkinID="SimpleBlackWhite" OnRowCommand="gvTradeCallOffList__RowCommand">
                <Columns>
                    <asp:BoundField DataField="BookingID" HeaderText="Booking ID" SortExpression="BookingID"/>
                    <asp:BoundField DataField="RegistrationID" HeaderText="Registration ID" SortExpression="RegistrationID" />
                    <asp:BoundField DataField="BookingDateTime" HeaderText="Current Booking DateTime" SortExpression="BookingDateTime" />
                    <asp:BoundField DataField="CreatedDate" HeaderText="CreateD DateTime" SortExpression="CreatedDate" />
                    <asp:BoundField DataField="TradeName" HeaderText="Trade Name" SortExpression="TradeName" />
                    <asp:BoundField DataField="PreferredFirstName" HeaderText="Preferred Contact First Name" SortExpression="PreferredFirstName" />
                    <asp:BoundField DataField="PreferredLastName" HeaderText="Preferred Contact Last Name" SortExpression="PreferredLastName" />
                    <asp:BoundField DataField="Telephone" HeaderText="Telephone" SortExpression="Telephone" />
                    <asp:BoundField DataField="Mobile" HeaderText="Mobile" SortExpression="Mobile" />
                    <asp:TemplateField HeaderText="Contacted?">
                        <ItemTemplate>
                            <asp:TextBox ID="txtNotes" Text='<%# Bind("Notes") %>' TextMode="MultiLine" Height="70px" Width="300px" runat="server">
                            </asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Button ID="btnSaveNotes" Text="Save Notes" CommandName="SaveNotes" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label ID="lblSaveNotesSuccess" runat="server" Text="" EnableViewState="false"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </td>
        <td class="grid-white-body-right"></td>
    </tr>
    <tr>
        <td class="grid-white-footer-left-roundL"></td>
        <td class="grid-white-footer-center"></td>
        <td class="grid-white-footer-right-roundL"></td>
    </tr>
</table>
</asp:Content>

Code Behind:

protected void gvTradeCallOffList__RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "SaveNotes")
        {
            int index;
            bool bIsConverted = int.TryParse(e.CommandArgument.ToString().Trim(), out index);
            GridViewRow selectedRow = gvTradeCallOffList.Rows[index];

            TextBox txtNotes = (TextBox)selectedRow.FindControl("txtNotes");
            string tradeCallOffListNotes = txtNotes.Text.Trim();

            int bookingID = Convert.ToInt32(selectedRow.Cells[0].Text);

            DataTable dt = new TradeBookingDAL().SaveTradeCallOffListNotes(bookingID, tradeCallOffListNotes);
            if (dt.Rows.Count > 0)
            {
                string val = dt.Rows[0]["Notes"].ToString();
                txtNotes.Text = val;

                Label lblSaveNotesSuccess = (Label)selectedRow.FindControl("lblSaveNotesSuccess");
                lblSaveNotesSuccess.Text = "Notes saved successfully";
                lblSaveNotesSuccess.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                Label lblSaveNotesSuccess = (Label)selectedRow.FindControl("lblSaveNotesSuccess");
                lblSaveNotesSuccess.Text = "Notes not saved successfully";
                lblSaveNotesSuccess.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
    catch (Exception ex)
    {
        new Util().LogError(ex);
    }
}

Upvotes: 0

Views: 2493

Answers (3)

MadDev
MadDev

Reputation: 1150

Thanks everybody for your help but I've figured out the answer.

Replaced these lines:

int index;
bool bIsConverted = int.TryParse(e.CommandArgument.ToString().Trim(), out index);
GridViewRow selectedRow = gvTradeCallOffList.Rows[index];

With this:

GridViewRow selectedRow = (GridViewRow)(((Button)e.CommandSource).NamingContainer);

Upvotes: 1

TSungur
TSungur

Reputation: 406

The dt.Rows[0] will always give the first row as expected since you request the first item (0 index) in your DataTable.

  1. You can get the selected row by the SelectedRow property of gridview. Then you can use the DataItemIndex property of this row to know what row it is in your underlying datatable. But since you are using the rowcommand event, this does not mean the row of the command is also the selected row.

  2. You can bind in your rows to the CommandArgument and assign the index or primary key value and use this value in your RowCommand method like e.CommandArgument.

Upvotes: 1

Crettig
Crettig

Reputation: 121

I believe it should be

int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvTradeCallOffList.Rows[index];

works for me in my Rowcommand

Upvotes: 0

Related Questions