C# – asp.net gridview edit mode index

asp.netcgridview

I have a gridview which is populated via a sql data source with the SELECT command: Select * FROM myTable

When the page is first loaded that displays every record in the table.

The gridview has an Edit button for each row and when the user clicks the row I want to update the gridview to display only that result in the gridview so I change the SELECT command of the sql data source to SELECT * FROM myTable WHERE ID = currentEditRow

That works except for one issue, when editing any other row but the first, the Update and Cancel buttons do not show up on the row. It is like is not in edit mode.

Any idea why that is?

Code:

 protected void gvResults_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvResults.EditIndex = e.NewEditIndex;
            SqlDataSource1.SelectCommand = "SELECT * FROM [myTable] WHERE ID = '" + ID + "'";
        }

Best Answer

GridView will bind data again automatically when you set a new command to SqlDataSource1.SelectCommand. This made it returns to start mode (Display read-only data with Edit button in GridView).

Otherwise, you trick it as my sample code below ...

This is my GridView binding data with SqlDataSource1, has DataKey names "code". There are three event handlings to implement: OnRowEditing, OnSelectedIndexChanged and OnRowCancelingEdit.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
    DataKeyNames="code" 
    OnRowEditing="GridView1_RowEditing" 
    OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit">
    <Columns>
        <asp:BoundField DataField="std_room_id" HeaderText="Room ID" SortExpression="std_room_id" />
        <asp:BoundField DataField="booking_name" HeaderText="Name" SortExpression="booking_name" />
        <asp:BoundField DataField="course_id" HeaderText="Course" SortExpression="course_id" />
        <asp:BoundField DataField="course_period" HeaderText="Period" SortExpression="course_period" />
        <asp:BoundField DataField="start_date" HeaderText="Start Date" SortExpression="start_date" />
        <asp:BoundField DataField="end_date" HeaderText="End Date" SortExpression="end_date" />
        <asp:CheckBoxField DataField="approved" HeaderText="Approved" SortExpression="approved" />
        <asp:CommandField ShowSelectButton="true" SelectText="Edit"/>
        <asp:CommandField ShowEditButton="true" Visible="false"/>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
    SelectCommand="SELECT * FROM [BookingRoom]"></asp:SqlDataSource>
    </div>

I have two CommandFields: SelectButton to click when user want open Edit Mode, and the second CommandField for display Update and Cancel buttons.

And this is my code-behide

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.Columns[7].Visible = false;
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    string code = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
    SqlDataSource1.SelectCommand = "SELECT * FROM [BookingRoom] WHERE code = '" + code + "'";

    GridView1.Columns[8].Visible = true;
    GridView1.SetEditRow(0);
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.Columns[7].Visible = true;
    GridView1.Columns[8].Visible = false;
}

I tricked it by show the SelectButton (display text as "Edit") in GridView with all loaded data in start mode. When user selects the row by clicking the SelectButton, it will start to do the steps in GridView1_SelectedIndexChanged which try to display only the selected row in GridView and set Edit Mode to that row togather with displaying the second CommandField (Update and Cancel buttons).

To set Edit Mode by this line GridView1.SetEditRow(0);, it will do the step in GridView1_RowEditing which try to hide the SelectButton.

Final, you have to handle the cancling Edit Mode as in GridView1_RowCancelingEdit which try to display the SelectButton, and hide the Update and Cancel buttons.

I access those two CommandFields by their column index according to my columns in GridView: column index of Select button is 7 and column index of Update and Cancel buttons is 8

Related Topic