Fentcheasse
New Member
I am new at this so please bear with me.I am having a problem with retrieving data from \[code\]gridview\[/code\] and fill to \[code\]textboxes\[/code\].My gridview has 2 pages. Page 1 contains data with the \[code\]course_id\[/code\] from \[code\]CR0001 to CR0015.\[/code\] Page 2 contains data from \[code\]CR0016\[/code\] and go on.Normally, on page 1, If I click on the first row which contains \[code\]CR0001\[/code\], then I will get the data with the \[code\]course_id\[/code\] is \[code\]CR0001\[/code\]. If I move to page 2 and click on the first row which contains \[code\]CR0016\[/code\], then I will get the data with the course_id is \[code\]CR0016\[/code\].When I use the search function to search for \[code\]CR0016\[/code\], then there will be only one result which is displayed in one row in the \[code\]gridview\[/code\] with the \[code\]course_id\[/code\] is \[code\]CR0016\[/code\]. But if I click on that row, I will get the data with the course_id is \[code\]CR0001\[/code\] instead of \[code\]CR0016\[/code\]. When I debugged into the code, I saw that the \[code\]selectedrow\[/code\] was 0 (the only row), and that was right because there was only one search result found. But the problem was that I got the \[code\]CR0001\[/code\] instead of \[code\]CR0016.\[/code\]The right data should contain the \[code\]course_id\[/code\] is \[code\]CR0016\[/code\], not \[code\]CR0001\[/code\].Can anyone help me to fix this problem please?This is my gridview\[code\]<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvDetails_RowDataBound" AllowSorting="true" AllowPaging="true" PageSize="15" OnSelectedIndexChanged="gvDetails_SelectedIndexChanged" DataKeyNames="course_id" OnPageIndexChanging="gvDetails_PageIndexChanging"> <HeaderStyle BackColor="Black" ForeColor="White" /> <Columns> <asp:TemplateField HeaderText="ID" ItemStyle-Width="80" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:Label ID="courseidlabel" runat="server" Text='<%# Eval("course_id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="coursenamelabel" runat="server" Text='<%# Eval("course_name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Description" Visible="false"> <ItemTemplate> <asp:Label ID="coursedesclabel" runat="server" Text='<%# Eval("course_desc") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>\[/code\]These are the code behind\[code\]private void searchdatagrd(string searchstring){ if (conn.State == ConnectionState.Open) { conn.Close(); } conn.ConnectionString = connstr; conn.Open(); SqlCommand cmd = new SqlCommand("select_course_search", conn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@idsearchstr", searchstring); SqlDataAdapter adapt = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapt.Fill(ds); gvDetails.DataSource = ds; gvDetails.DataBind(); conn.Close();}protected void searchbutton_Click(object sender, EventArgs e){ searchdatagrd(searchbox.Text);}protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e){ if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.backgroundColor='LightGray'; this.style.fontWeight='bold'"; e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.backgroundColor='White'; this.style.fontWeight='normal'"; e.Row.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this.gvDetails, "Select$" + e.Row.RowIndex); }}protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e){ GridViewRow row = gvDetails.SelectedRow; Label idlabel = (Label)gvDetails.SelectedRow.FindControl("courseidlabel"); Label namelabel = (Label)gvDetails.SelectedRow.FindControl("coursenamelabel"); Label desclabel = (Label)gvDetails.SelectedRow.FindControl("coursedesclabel"); string id = idlabel.Text.ToString(); string name = namelabel.Text.ToString(); string course = desclabel.Text.ToString(); idbox.Text = id; namebox.Text = name; descbox.Text = course; idbox.ReadOnly = true; searchdatagrd(id);}\[/code\]the code used to bind data at page load: with the stored procedure is to select all from table Course\[code\]private void binddata(){ if (conn.State == ConnectionState.Open) { conn.Close(); } conn.ConnectionString = connstr; conn.Open(); SqlCommand cmd = new SqlCommand("select_course_grd", conn); cmd.CommandType = CommandType.StoredProcedure; SqlDataAdapter adapt = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapt.Fill(ds); gvDetails.DataSource = ds; gvDetails.DataBind(); conn.Close();}\[/code\]