Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data;
public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int intselectindex = 0;
        int intpageindex = 0;
        int intgridviewpage = GridView1.PageCount;
        //loop threw each page in gridveiw
        for (int i = 0; i < intgridviewpage; i++)
        {
            GridView1.PageIndex = i;
            GridView1.DataBind();
            foreach (GridViewRow gvr in GridView1.Rows)
            {
                if (gvr.Cells[1].Text == DropDownList1.SelectedValue)
                {
                    intselectindex = gvr.RowIndex;
                    intpageindex = i;
                    break;
                }
            }


        }
        GridView1.PageIndex = intpageindex;
        GridView1.SelectedIndex = intselectindex;
        GridView1.DataBind();
    }

}
tin this code when user select any dropdownlist index than it should select related gridview row
error:
in this code when i select one name from dropdownlist than it doesnot select that row of gridview but at that time when i click any select button than it select gridview row(the row in which one coloumn is mention in dropdown)
Posted
Comments
sakshi111 8-Apr-13 6:10am    
i bind both dropdownlist and gridview with same sqldatasource
Deependra Khangarot 8-Apr-13 6:16am    
I think you should add above code with some modification in DropDownList1's SelectedIndexChanged event.
sakshi111 8-Apr-13 6:24am    
i already enable select index and page index
sakshi111 8-Apr-13 7:37am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2"
DataSourceID="SqlDataSource1" GridLines="None" Height="16px"
onselectedindexchanged="GridView1_SelectedIndexChanged" PageSize="5"
style="margin-right: 0px" Width="425px" ForeColor="Black">
<alternatingrowstyle backcolor="PaleGoldenrod">
<columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="student_id" HeaderText="student_id"
SortExpression="student_id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
<asp:BoundField DataField="dob" HeaderText="dob" SortExpression="dob" />

<footerstyle backcolor="Tan">
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<sortedascendingcellstyle backcolor="#FAFAE7">
<sortedascendingheaderstyle backcolor="#DAC09E">
<sorteddescendingcellstyle backcolor="#E1DB9C">
<sorteddescendingheaderstyle backcolor="#C2A47B">

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
CausesValidation="True" DataSourceID="SqlDataSource1" DataTextField="name"
DataValueField="student_id" AutoPostBack="True">

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:learnConnectionString %>"
SelectCommand="SELECT * FROM [student]">
<br />

</div>
</form>
</body>
</html>

1 solution

Try below code on DropDownList1_SelectedIndexChanged and set autopostback property of dropdownlist to true.


C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       int intselectindex = 0;
       int intpageindex = 0;
       int intgridviewpage = GridView1.PageCount;
       //loop threw each page in gridveiw
       for (int i = 0; i < intgridviewpage; i++)
       {
           foreach (GridViewRow gvr in GridView1.Rows)
           {
               if (gvr.Cells[1].Text == DropDownList1.SelectedValue)
               {
                   intselectindex = gvr.RowIndex;
                   intpageindex = i;
                   break;
               }
           }


       }
       GridView1.PageIndex = intpageindex;
       GridView1.SelectedIndex = intselectindex;

   }
 
Share this answer
 
Comments
sakshi111 8-Apr-13 7:26am    
in if condition without GridView1.PageIndex = i; it does not goes to next page and by using your code same condition is happened.
sakshi111 8-Apr-13 7:38am    
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2"
DataSourceID="SqlDataSource1" GridLines="None" Height="16px"
onselectedindexchanged="GridView1_SelectedIndexChanged" PageSize="5"
style="margin-right: 0px" Width="425px" ForeColor="Black">
<alternatingrowstyle backcolor="PaleGoldenrod">
<columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="student_id" HeaderText="student_id"
SortExpression="student_id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
<asp:BoundField DataField="dob" HeaderText="dob" SortExpression="dob" />

<footerstyle backcolor="Tan">
<HeaderStyle BackColor="Tan" Font-Bold="True" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<sortedascendingcellstyle backcolor="#FAFAE7">
<sortedascendingheaderstyle backcolor="#DAC09E">
<sorteddescendingcellstyle backcolor="#E1DB9C">
<sorteddescendingheaderstyle backcolor="#C2A47B">

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
CausesValidation="True" DataSourceID="SqlDataSource1" DataTextField="name"
DataValueField="student_id" AutoPostBack="True">

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:learnConnectionString %>"
SelectCommand="SELECT * FROM [student]">
<br />

</div>
</form>
</body>
</html>
Anuja Pawar Indore 8-Apr-13 9:43am    
Why posting code again and again. Give reference

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900