Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:35:33+00:00 2026-06-11T10:35:33+00:00

I have a gridview that works fine with SQLDataSource. Edit, Delete buttons works perfectly.

  • 0

I have a gridview that works fine with SQLDataSource. Edit, Delete buttons works perfectly.
However, when I search any record and try to edit that record, the gridview opens up the first row in the edit mode.
I don’t know what i have done wrong.

Here is my code

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Customer_id"
        DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display." AllowPaging="True" 
        AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" 
        PageSize="5" Width="873px" onrowediting="GridView1_RowEditing" >
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
                        CommandName="Update" Text="Update"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                        CommandName="Edit" Text="Edit" ></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                        CommandName="Select" Text="Select"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" 
                        CommandName="Delete" Text="Delete"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Customer_id" HeaderText="Customer_id" ReadOnly="True"
                SortExpression="Customer_id" InsertVisible="False" />
            <asp:BoundField DataField="Customer_Name" HeaderText="Customer_Name" 
                SortExpression="Customer_Name" />
            <asp:BoundField DataField="Customer_Type" HeaderText="Customer_Type" SortExpression="Customer_Type" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" 
            Font-Bold="True" Font-Italic="True" Font-Overline="True" Font-Size="Large" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>

And the Code Behind looks like this:

protected void Page_Load(object sender, EventArgs e)
{
    string vs = (string)ViewState["buttonClicked"];
    string isEditing = (string)ViewState["isEditing"];
    if (IsPostBack)
    {
        if (vs == "False")
        {
            RadioButtonListChanged();
            GridView1.DataBind();
        }
        else if (vs == "True")
        {
            btnSearch_Click(sender, e);
            GridView1.DataBind();
        }
    }
}

protected void RadioButton1_SelectedIndexChanged(object sender, EventArgs e)
{
    RadioButtonListChanged();

}

private void RadioButtonListChanged()
{
    ViewState["buttonClicked"] = "False";

    string sqlString;

    if (RadioButton1.SelectedItem.Text != "All")
    {
        sqlString = "Select * from customers where status='True' and  customer_type = '" + RadioButton1.SelectedValue.ToString() + "' order by customer_name";
    }
    else
    {
        sqlString = "Select * from customers where  status='True' order by customer_name";
    }

    SqlDataSource1.SelectCommand = sqlString;
    SqlDataSource1.DataBind();

}

protected void btnSearch_Click(object sender, EventArgs e)
{

    ViewState["buttonClicked"] = "True";

    string sqlString;
    sqlString = "Select * from customers where status='True' and customer_name like '%" + txtCustomerName.Text + "%' order by customer_type, customer_name";
    SqlDataSource1.SelectCommand = sqlString;
    SqlDataSource1.DataBind();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:35:34+00:00Added an answer on June 11, 2026 at 10:35 am

    I found the solution here
    Gridview Selects Wrong Row For Editing

    in page_load event

    if (IsPostBack)
    {
        SqlDataSource1.SelectCommand = (string)Session["sqlString"];
        SqlDataSource1.DataBind();
    }
    

    and when i am searching i did this in button event

    Session["sqlString"] = sqlString;
    

    That wasn’t simple 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a GridView scrolling vertically, and everything works fine. But now that
Hey guys, I have a GridView which is binded to SqlDataSource, everything works fine,
I have that works fine without Script manager. but if I add script manager
I have a update panel with a gridview inside. The gridview works fine when
I have a command field in a gridview, that as it is, works as
I have a gridview that has databound items. it is bound to a SQLDATASOURCE.
I have a gridview that needs to be exported to Excel. I have managed
I have a gridview that shows an image as part of one of its
I have a gridview that displays items details, I added two template fields one
I have a gridview that is only shown in a modal popup. right before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.