Click here to Skip to main content
15,916,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
1.I have a gridview1 and sqldatasource1 for that gridview in design mode i assigned sqldatasource1 to gridview1

2.i have 3 dropdownlists each have different fields like Country State aand City
3.I select three of them and click on search the result is shown according to the country state and city

4.when i try to edit any of the result (ie when i click on edit on the gridview) the edit is goes to first record in the database

5.that means if there are 10 records when i searched if the result is 8 and 9 th records if i wanted to modify 8th record the edit field goes to 1st record

6.I need a situation like when i try to edit 8 and 9 th records must be there and i have to edit 8th record then when i click on reload it has to reload whole gridview

My .aspx Code is below
----------------------
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Country :
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="CountrySqlDataSource" DataTextField="Country" 
            DataValueField="Country">
        </asp:DropDownList>
        <asp:SqlDataSource ID="CountrySqlDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:GridviewDemoConnectionString %>" 
            SelectCommand="SELECT DISTINCT [Country] FROM [GridViewDemo]">
        </asp:SqlDataSource>
        Stste :
        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
            DataSourceID="StateSqlDataSource" DataTextField="State" DataValueField="State">
        </asp:DropDownList>
        <asp:SqlDataSource ID="StateSqlDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:GridviewDemoConnectionString %>" 
            SelectCommand="SELECT DISTINCT [State] FROM [GridViewDemo] WHERE ([Country] = @Country)">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Country" 
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
        City :
        <asp:DropDownList ID="DropDownList3" runat="server" 
            DataSourceID="CitySqlDataSource" DataTextField="City" DataValueField="City">
        </asp:DropDownList>
    
        <asp:SqlDataSource ID="CitySqlDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:GridviewDemoConnectionString %>" 
            SelectCommand="SELECT DISTINCT [City] FROM [GridViewDemo] WHERE (([Country] = @Country) AND ([State] = @State))">
            <SelectParameters>
                <asp:ControlParameter ControlID="DropDownList1" Name="Country" 
                    PropertyName="SelectedValue" Type="String" />
                <asp:ControlParameter ControlID="DropDownList2" Name="State" 
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    
        <asp:Button ID="ButtonSearch" runat="server" onclick="ButtonSearch_Click" 
            Text="Search" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="ButtonNameSearch" runat="server" 
            onclick="ButtonNameSearch_Click" Text="Search" />
    
    </div>
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" DataSourceID="GridviewSqlDataSource" 
            onrowediting="GridView1_RowEditing">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Country" HeaderText="Country" 
                    SortExpression="Country" />
                <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="GridviewSqlDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:GridviewDemoConnectionString %>" 
            DeleteCommand="DELETE FROM [GridViewDemo] WHERE [ID] = @ID" 
            InsertCommand="INSERT INTO [GridViewDemo] ([Name], [Country], [State], [City]) VALUES (@Name, @Country, @State, @City)" 
            SelectCommand="SELECT * FROM [GridViewDemo]" 
            UpdateCommand="UPDATE [GridViewDemo] SET [Name] = @Name, [Country] = @Country, [State] = @State, [City] = @City WHERE [ID] = @ID">
            <DeleteParameters>
                <asp:Parameter Name="ID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="State" Type="String" />
                <asp:Parameter Name="City" Type="String" />
                <asp:Parameter Name="ID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Country" Type="String" />
                <asp:Parameter Name="State" Type="String" />
                <asp:Parameter Name="City" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>

-------------------
My .cs File
using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        GridviewSqlDataSource.SelectCommand = "SELECT *FROM GridViewDemo WHERE Country='" + DropDownList1.SelectedItem.ToString() + "' AND State='" + DropDownList2.SelectedItem.ToString() + "' AND City='" + DropDownList3.SelectedItem.ToString() + "'";
    }
    protected void ButtonNameSearch_Click(object sender, EventArgs e)
    {
        GridviewSqlDataSource.SelectCommand = "SELECT *FROM GridViewDemo WHERE Name LIKE '%"+TextBox1.Text+"%' ";

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridviewSqlDataSource.SelectCommand = "SELECT *FROM GridViewDemo WHERE Name LIKE '%" + TextBox1.Text + "%' ";
    }
}


-------------------
My SQL script for your reference

SQL
CREATE DATABASE GridviewDemo

USE [GridviewDemo]

CREATE TABLE [dbo].[GridViewDemo](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [Country] [varchar](50) NULL,
    [State] [varchar](50) NULL,
    [City] [varchar](50) NULL,
 CONSTRAINT [PK_GridViewDemo] PRIMARY KEY CLUSTERED
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

INSERT INTO GridviewDemo VALUES('Krishna','India','Andhra Pradesh','Hyderabad')
Posted
Updated 4-Nov-11 19:55pm
v2
Comments
Anil Honey 206 4-Nov-11 2:12am    
How Your Going to Edit.Your Going to Edit in Gridview or By selection Index changed through controls Your Going to Edit.
krishna_goluguri 4-Nov-11 3:56am    
can i post my whole code here
Mehdi Gholam 5-Nov-11 1:56am    
EDIT -> moved the code from the OP solution

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