Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
1.20/5 (5 votes)
See more:
XML
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <title>Register:</title>
    <asp:Label ID="lblFb" runat="server" />
    <center>
        <table cellpadding="10px">
            <tr>
                <td>
                    Name:
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    Mobile:
                </td>
                <td>
                    <asp:TextBox ID="txtMobile" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    Age:
                </td>
                <td>
                    <asp:TextBox ID="txtAge" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    DoB:
                </td>
                <td>
                    <asp:TextBox ID="txtDoB" runat="server" />
                </td>
            </tr>
            <tr>
            <td>Country</td>
            <td><asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
                    onselectedindexchanged="ddlCountry_SelectedIndexChanged">
            </asp:DropDownList></td>
            </tr>
            <tr>
            <td>State</td>
            <td><asp:DropDownList ID="ddlState" runat="server" AutoPostBack="true"
                    onselectedindexchanged="ddlState_SelectedIndexChanged">
            <asp:ListItem Text="--select state--"></asp:ListItem>
            </asp:DropDownList></td>
            </tr>
            <tr>
            <td>City</td>
            <td><asp:DropDownList ID="ddlCity" runat="server" AutoPostBack="true">
            <asp:ListItem Text="--select city--"></asp:ListItem>
            </asp:DropDownList></td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSubmit" Text="Submit" runat="server" Font-Bold="true" OnClick="btnSubmit_Click" />
                </td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="100%" DataKeyNames="ID"
            BackColor="#DEBA84" AutoGenerateColumns="false" BorderColor="#DEBA84" BorderStyle="None"
            BorderWidth="1px" CellPadding="5" CellSpacing="2"
            onrowediting="GridView1_RowEditing"
            onrowcancelingedit="GridView1_RowCancelingEdit"
            onrowupdating="GridView1_RowUpdating"
            onrowdatabound="GridView1_RowDataBound">
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <Columns>
                <asp:TemplateField HeaderText="Sr. No">
                    <ItemTemplate>
                        <%#Container.DisplayIndex+1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditName" runat="server" Text='<%#Eval("Name")%>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemName" runat="server" Text='<%#Eval("Name") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobile">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditMobile" runat="server" Text='<%#Eval("Mobile") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemMobile" runat="server" Text='<%#Eval("Mobile")%>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Age">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditAge" runat="server" Text='<%#Eval("Age") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemAge" runat="server" Text='<%#Eval("Age") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date of Birth">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEditDoB" runat="server" Text='<%#Eval("DoB") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblItemDoB" runat="server" Text='<%#Eval("DoB") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Country">
                <EditItemTemplate>
                <asp:DropDownList ID="ddleditCountry" runat="server" OnSelectedIndexChanged="ddlEditCountry_selectedIndexChange" AutoPostBack="true" />
                 <asp:HiddenField ID="hdnCountry" runat="server" Value='<%#Eval("Country") %>' />
                </EditItemTemplate>
                <ItemTemplate>
                <center>
                <asp:Label ID="lblCountry" runat="server" Text='<%#Eval("Country") %>' />
                </center>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="State">
                <EditItemTemplate>
                <asp:DropDownList ID="ddleditState" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                <center>
                <asp:Label ID="lblState" runat="server" Text='<%#Eval("State") %>' />
                </center>
                </ItemTemplate>
                </asp:TemplateField>
                 <asp:TemplateField HeaderText="City">
                <EditItemTemplate>
                <asp:DropDownList ID="ddleditCity" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                <center>
                <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>' />
                </center>
                </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Edit">
                    <EditItemTemplate>
                        <asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server" ImageUrl="~/Images/update.jpg"
                            ToolTip="Update" Height="20px" Width="20px" />
                        <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/Cancel.jpg"
                            ToolTip="Cancel" Height="20px" Width="20px" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:ImageButton ID="imgbtnEdit" runat="server" ImageUrl="~/Images/Edit.jpg" ToolTip="Edit"
                            Height="20px" Width="20px" CommandName="Edit" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete.jpg"
                            ToolTip="Delete" Height="20px" Width="20px" OnClick="GridView1_RowDeleting" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BorderColor="#FF99FF" />
        </asp:GridView>
    </center>
</asp:Content>



CS Code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;


public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"server=DELL\SQLEXPRESS; Database=Bussiness; user id=sa; Password=vaishnavisingh;");
    SqlParameter[] prm;
    SqlDataAdapter da = new SqlDataAdapter();
    SqlCommand cmd = new SqlCommand();
    DataSet ds = new DataSet();
    string str = "";
    static int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            bindGrid();
            bindcountry();
            
        }

    }
    protected void bindGrid()
    {
        con.Open();
        cmd = new SqlCommand("Select * From Grid", con);
        da = new SqlDataAdapter(cmd);
        ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        prm = new SqlParameter[7];
        prm[0] = new SqlParameter("@Name", txtName.Text.Trim());
        prm[1] = new SqlParameter("@Mobile", txtMobile.Text.Trim());
        prm[2] = new SqlParameter("@Age", txtAge.Text.Trim());
        prm[3] = new SqlParameter("@DoB", txtDoB.Text.Trim());
        prm[4] = new SqlParameter("@Country", ddlCountry.SelectedItem.Text);
        prm[5] = new SqlParameter("@State", ddlState.SelectedItem.Text);
        prm[6] = new SqlParameter("@City", ddlCity.SelectedItem.Text);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "insertData";
        cmd.Connection = con;
        cmd.Parameters.AddRange(prm);
        con.Open();
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result > 0)
        {
            Response.Write("Registered successfully");
            Response.Redirect("Default.aspx");
        }

    }


    protected void GridView1_RowDeleting(object sender, EventArgs e)
    {
        ImageButton lnk = ((ImageButton)(GridView1.FindControl("imgbtnDelete")));
        lnk = (ImageButton)sender;
        GridViewRow gvr = (GridViewRow)lnk.NamingContainer;
        String str = GridView1.DataKeys[gvr.RowIndex].Value.ToString();
        int id = Convert.ToInt32(str);
        prm = new SqlParameter[1];
        prm[0] = new SqlParameter("@id", id);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "deleteGrid";
        cmd.Connection = con;
        con.Open();
        cmd.Parameters.AddRange(prm);
        int result = cmd.ExecuteNonQuery();
        if (result > 0)
        {
            Response.Redirect("Default.aspx");
        }


    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bindGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindGrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gvr = GridView1.Rows[e.RowIndex];
        String str = GridView1.DataKeys[gvr.RowIndex].Value.ToString();
        int id = Convert.ToInt32(str);
        String editName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditName")).Text;
        String editMobile = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditMobile")).Text;
        String editAge = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEditAge")).Text;
        prm = new SqlParameter[2];
        prm[0] = new SqlParameter("@id", id);
        prm[1] = new SqlParameter("@RoomType", editName);

        Response.Redirect("addRoomType.aspx");
        //Its Ryt Code//
    }
    protected void bindcountry()
    {
        ddlCountry.AppendDataBoundItems = true;
        ddlCountry.Items.Add(new ListItem("--Select Country--", ""));
        String strQry = "select Countryid, countryname from country";
        cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQry;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlCountry.DataSource = cmd.ExecuteReader();
            ddlCountry.DataTextField = "CountryName";
            ddlCountry.DataValueField = "CountryID";
            ddlCountry.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlState.Items.Clear();
        ddlState.Items.Add(new ListItem("--select state--", ""));
        ddlCity.Items.Clear();
        ddlCity.Items.Add(new ListItem("--select city--", ""));
        ddlState.AppendDataBoundItems = true;
        String Qry = "Select StateID, StateName from State where CountryID=@CountryID";
        cmd = new SqlCommand();
        cmd.Parameters.AddWithValue("@CountryID", ddlCountry.SelectedItem.Value);
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = Qry;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlState.DataSource = cmd.ExecuteReader();
            ddlState.DataTextField = "StateName";
            ddlState.DataValueField = "StateID";
            ddlState.DataBind();
            if (ddlState.Items.Count > 1)
            {
                ddlState.Enabled = true;
            }
            else
            {
                ddlState.Enabled = false;
                ddlCity.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlCity.Items.Clear();
        ddlCity.Items.Add(new ListItem("--select city--", ""));
        ddlCity.AppendDataBoundItems = true;
        String Qry = "select Cityid, cityname from city where Stateid=@StateID";
        cmd = new SqlCommand();
        cmd.Parameters.AddWithValue("@StateID", ddlState.SelectedItem.Value);
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = Qry;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlCity.DataSource = cmd.ExecuteReader();
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();
            if (ddlCity.Items.Count > 1)
            {
                ddlCity.Enabled = true;
            }
            else
            {
                ddlCity.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (GridView1.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
        {
            
            DropDownList ddllist1 = ((DropDownList)e.Row.FindControl("ddleditCountry"));
            var hdnCountryName = ((HiddenField)e.Row.FindControl("hdnCountry"));
            ddllist1.AppendDataBoundItems = true;
            
            String strQry = "select Countryid, countryname from country";
            cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strQry;
            cmd.Connection = con;
            try
            {
                con.Open();
                ddllist1.DataSource = cmd.ExecuteReader();
                ddllist1.DataTextField = "CountryName";
                ddllist1.DataValueField = "CountryID";
                ddllist1.DataBind();
                ddllist1.Items.Insert(0,new ListItem("--Select Country--", "0"));
                ddllist1.Items.FindByText(hdnCountryName.Value).Selected=true;
               
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }

    }

    protected void ddlEditCountry_selectedIndexChange(object sender, EventArgs e)
    {
        var ddl1 = ((DropDownList)(GridView1.FindControl("ddleditCountry")));
        ddl1 = (DropDownList)sender;
        GridViewRow gvr = (GridViewRow)ddl1.NamingContainer;
        

            ddlState.Items.Clear();
            ddlState.Items.Add(new ListItem("--select state--", ""));
            ddlCity.Items.Clear();
            ddlCity.Items.Add(new ListItem("--select city--", ""));
            ddlState.AppendDataBoundItems = true;
            String Qry = "Select StateID, StateName from State where CountryID=@CountryID";
            cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@CountryID", ddlCountry.SelectedItem.Value);
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = Qry;
            cmd.Connection = con;
            try
            {
                con.Open();
                ddlState.DataSource = cmd.ExecuteReader();
                ddlState.DataTextField = "StateName";
                ddlState.DataValueField = "StateID";
                ddlState.DataBind();
                if (ddlState.Items.Count > 1)
                {
                    ddlState.Enabled = true;
                }
                else
                {
                    ddlState.Enabled = false;
                    ddlCity.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                con.Dispose();
            }
        }
    
}
Posted
Updated 23-May-19 0:05am
v3
Comments
Christian Graus 20-Jan-13 20:37pm    
If you are new to ASP.NET, then this is way too complex for you. ASP.NET is also nowhere near as good as MVC, so just move to MVC, obviously if you're new you're not on a real project, so learn the better framework

 
Share this answer
 
Comments
Sujee1 21-Jan-13 4:28am    
thnx mam..bt i hv 3 dropdownlist in gridview.. country, state nd city..when i select the country then state will be changed automatically according to country....& so on....so pls help me to solve this
Vani Kulkarni 21-Jan-13 4:43am    
I have provided the links that can help you to start working your issue. You need to figure out or modify the code per your requirements.
Sujee1 22-Jan-13 3:20am    
thnx alot mam...i hv solved my problem...i was putting the same name of the states in both..in the gridview nd outside the gridview...bt thnk u vry much for helping me to solve this.........:)
Vani Kulkarni 22-Jan-13 4:29am    
Great!
just change the name of the dropdownlist control ddlstate...b'coz its alredy exist....
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-May-13 1:43am    
Are you talking to yourself? Or just cheating? :-)
—SA
thatraja 12-Oct-13 2:37am    
This person always close the his questions by posting his solutions(which is rare). He got 5! from me for his answers.
Sergey Alexandrovich Kryukov 12-Oct-13 20:04pm    
Why? I think this is nothing but abuse and cheating. If one has a problem and a solution which might be interesting to other people, this person should write some article, and let's see how people accept this publication... The gibberish we see here has no value, as any other gibberish...
—SA
thatraja 12-Oct-13 20:30pm    
Not all OP returns again to post solutions to their answers. Atleast he did post the solution which could help the others in future. We can't expect all OPs to write article/Tip.
BTW I didn't vote this one, only other answers of him.
thatraja 14-Oct-13 2:59am    
We can't expect all OPs(most of them are newbie) to post article or tip/trick regarding this. They hardly come here to ask questions. Atleast this person has posted solution for his question which could help others in future. Besides he's not after reputation points IMO. Now this site has 1,248*20 questions without answers, if OP like this person starts to post solutions for their question possibly the count(unanswered questions) will decrease.
Hope you got the point now.
XML
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="BrainTree._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 id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%--<asp:GridView ID="grdCompany" runat="server" AutoGenerateColumns="False" DataKeyNames="CompanyID"
            OnRowUpdating="grdCompany_RowUpdating" OnRowCancelingEdit="grdCompany_RowCancelingEdit"
            OnRowEditing="grdCompany_RowEditing" OnRowDeleting="grdCompany_RowDeleting" Width="100%"
            BackColor="Azure">
            <%--<Columns>
                <asp:TemplateField HeaderText="Find Country">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlLocation" runat="server" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged"
                            AutoPostBack="true" />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField HeaderText="Company Name">
                    <ItemTemplate>
                        <asp:Label ID="lblCompanyName" runat="server" Text='<%#Eval("CompanyName") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField HeaderText="Company ID">
                    <ItemTemplate>
                        <asp:Label ID="lblCompanyID" runat="server" Text='<%#Eval("CompanyId") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField HeaderText="Company Desc">
                    <ItemTemplate>
                        <asp:Label ID="lblCompanyDesc" runat="server" Text='<%#Eval("CompanyDesc") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>--%>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_OnRowDataBound"
            OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit"
            OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:BoundField HeaderText="Name" DataField="Location" />
                <asp:TemplateField HeaderText="Country">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCountries_SelectedIndexChanged">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="lbBuildingUpdate" runat="server" CausesValidation="False" CommandName="Edit"
                            Text="Edit"></asp:LinkButton>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:LinkButton ID="lbBuildingUpdateSave" runat="server" CausesValidation="True"
                            CommandName="Update" Text="Update"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="lbBuildingUpdateCancel" runat="server" CausesValidation="False"
                            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnAddNewCompany" runat="server" Text="Add New Company" OnClick="btnAddNewCompany_Click" />
    </div>
    </form>
</body>
</html>




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace BrainTree
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bindGrid();
GridView1.DataSource = GetData();
GridView1.DataBind();
}

}

protected void grdCompany_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

}

//protected void grdCompany_RowEditing(object sender, GridViewEditEventArgs e)
//{
// grdCompany.EditIndex = e.NewEditIndex;
// bindGrid();
//}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = GetData();
GridView1.DataBind();
}

//protected void grdCompany_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
//{
// grdCompany.EditIndex = -1;
// bindGrid();
//}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataSource = GetData();
GridView1.DataBind();
}

protected void grdCompany_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

}

protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtOriginalCompany = new DataTable();
DataRow drRowtoAddBRC = dtOriginalCompany.NewRow();
drRowtoAddBRC["CompanyName"] = "Barclays";
drRowtoAddBRC["CompanyID"] = "BRC";
drRowtoAddBRC["CompanyDesc"] = "Barclays Pvt Ltd";
dtOriginalCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtOriginalCompany.NewRow();
drRowtoAddACC["CompanyName"] = "Accenture";
drRowtoAddACC["CompanyID"] = "ACC";
drRowtoAddACC["CompanyDesc"] = "Accenture Pvt Ltd.";
dtOriginalCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtOriginalCompany.NewRow();
drRowtoAddCRSUI["CompanyName"] = "Credit Suisse";
drRowtoAddCRSUI["CompanyID"] = "CRSUI";
drRowtoAddCRSUI["CompanyDesc"] = "United Kingdom";
dtOriginalCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtOriginalCompany.NewRow();
drRowtoAddDLL["CompanyName"] = "Delloite";
drRowtoAddDLL["CompanyID"] = "DLL";
drRowtoAddDLL["CompanyDesc"] = "Delloite";
dtOriginalCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtOriginalCompany.NewRow();
drRowtoAddGLT["CompanyName"] = "HSBC";
drRowtoAddGLT["CompanyID"] = "GLT";
drRowtoAddGLT["CompanyDesc"] = "HSBC Pvt Ltd.";
dtOriginalCompany.Rows.Add(drRowtoAddGLT);
ViewState["dtOriginalCompany"] = dtOriginalCompany;
DataTable dtCurrentCompany = new DataTable();
DataTable dtOldCompany = new DataTable();

dtCurrentCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany = ViewState["dtOriginalCompany"] as DataTable;

dtOldCompany.Rows[1].Delete();

ViewState["dtOriginalCompany"] = dtOldCompany;

}

protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtCompany = new DataTable("Location");
dtCompany.Columns.Add(new DataColumn("Location", System.Type.GetType("System.String")));
DataRow drRowtoAddBRC = dtCompany.NewRow();
drRowtoAddBRC["Location"] = "India";
dtCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtCompany.NewRow();
drRowtoAddACC["Location"] = "United Kingdom";
dtCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtCompany.NewRow();
drRowtoAddCRSUI["Location"] = "United States";
dtCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtCompany.NewRow();
drRowtoAddDLL["Location"] = "Australia";
dtCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtCompany.NewRow();
drRowtoAddGLT["Location"] = "Canada";
dtCompany.Rows.Add(drRowtoAddGLT);
ViewState["dtOriginalCompany"] = dtCompany;
DataTable dtCurrentCompany = new DataTable();
DataTable dtOldCompany = new DataTable();

dtCurrentCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany = ViewState["dtOriginalCompany"] as DataTable;

dtOldCompany.Rows[1].Delete();

ViewState["dtOriginalCompany"] = dtOldCompany;

}

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlCompany = (e.Row.FindControl("ddlCountries") as DropDownList);
if (ddlCompany != null)
{
ddlCompany.Items.Add(new ListItem("--Select Location--", ""));
ddlCompany.Items.Add(new ListItem("Pune", "PUNE"));
ddlCompany.Items.Add(new ListItem("Bangalore", "BGL"));
ddlCompany.Items.Add(new ListItem("Chennai", "CHN"));
ddlCompany.Items.Add(new ListItem("Hyderabad", "HYD"));
ddlCompany.Items.Add(new ListItem("Mumbai", "MUM"));
ddlCompany.DataTextField = "CityName";
ddlCompany.DataValueField = "CityId";
ddlCompany.DataBind();
}
}
}

protected void btnAddNewCompany_Click(object sender, EventArgs e)
{
DataTable dtOriginalCompany = new DataTable("CompanyData");
dtOriginalCompany.Columns.Add(new DataColumn("CompanyName", System.Type.GetType("System.String")));
dtOriginalCompany.Columns.Add(new DataColumn("CompanyID", System.Type.GetType("System.String")));
dtOriginalCompany.Columns.Add(new DataColumn("CompanyDesc", System.Type.GetType("System.String")));
DataRow drRowtoAddBRC = dtOriginalCompany.NewRow();
drRowtoAddBRC["CompanyName"] = "Barclays";
drRowtoAddBRC["CompanyID"] = "BRC";
drRowtoAddBRC["CompanyDesc"] = "Barclays Pvt Ltd";
dtOriginalCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtOriginalCompany.NewRow();
drRowtoAddACC["CompanyName"] = "Accenture";
drRowtoAddACC["CompanyID"] = "ACC";
drRowtoAddACC["CompanyDesc"] = "Accenture Pvt Ltd.";
dtOriginalCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtOriginalCompany.NewRow();
drRowtoAddCRSUI["CompanyName"] = "Credit Suisse";
drRowtoAddCRSUI["CompanyID"] = "CRSUI";
drRowtoAddCRSUI["CompanyDesc"] = "United Kingdom";
dtOriginalCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtOriginalCompany.NewRow();
drRowtoAddDLL["CompanyName"] = "Delloite";
drRowtoAddDLL["CompanyID"] = "DLL";
drRowtoAddDLL["CompanyDesc"] = "Delloite";
dtOriginalCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtOriginalCompany.NewRow();
drRowtoAddGLT["CompanyName"] = "HSBC";
drRowtoAddGLT["CompanyID"] = "GLT";
drRowtoAddGLT["CompanyDesc"] = "HSBC Pvt Ltd.";
dtOriginalCompany.Rows.Add(drRowtoAddGLT);

ViewState["dtOriginalCompany"] = dtOriginalCompany;

DataTable dtCurrentCompany = new DataTable();
DataTable dtOldCompany = new DataTable();

dtCurrentCompany = ViewState["dtOriginalCompany"] as DataTable;
dtOldCompany = ViewState["dtOriginalCompany"] as DataTable;

dtOldCompany.Rows[1].Delete();

ViewState["dtOldCompany"] = dtOldCompany;
}

private void bindGrid()
{
DataTable dtCompany = new DataTable("CompanyData");
dtCompany.Columns.Add(new DataColumn("CompanyName", System.Type.GetType("System.String")));
dtCompany.Columns.Add(new DataColumn("CompanyID", System.Type.GetType("System.String")));
dtCompany.Columns.Add(new DataColumn("CompanyDesc", System.Type.GetType("System.String")));
DataRow drRowtoAddBRC = dtCompany.NewRow();
drRowtoAddBRC["CompanyName"] = "Barclays";
drRowtoAddBRC["CompanyID"] = "BRC";
drRowtoAddBRC["CompanyDesc"] = "Barclays Pvt Ltd";
dtCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtCompany.NewRow();
drRowtoAddACC["CompanyName"] = "Accenture";
drRowtoAddACC["CompanyID"] = "ACC";
drRowtoAddACC["CompanyDesc"] = "Accenture Pvt Ltd.";
dtCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtCompany.NewRow();
drRowtoAddCRSUI["CompanyName"] = "Credit Suisse";
drRowtoAddCRSUI["CompanyID"] = "CRSUI";
drRowtoAddCRSUI["CompanyDesc"] = "United Kingdom";
dtCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtCompany.NewRow();
drRowtoAddDLL["CompanyName"] = "Delloite";
drRowtoAddDLL["CompanyID"] = "DLL";
drRowtoAddDLL["CompanyDesc"] = "Delloite";
dtCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtCompany.NewRow();
drRowtoAddGLT["CompanyName"] = "HSBC";
drRowtoAddGLT["CompanyID"] = "GLT";
drRowtoAddGLT["CompanyDesc"] = "HSBC Pvt Ltd.";
dtCompany.Rows.Add(drRowtoAddGLT);
//grdCompany.DataSource = dtCompany;
//grdCompany.DataBind();
}

private DataTable GetData()
{
DataTable dtCompany = new DataTable("Location");
dtCompany.Columns.Add(new DataColumn("Location", System.Type.GetType("System.String")));
DataRow drRowtoAddBRC = dtCompany.NewRow();
drRowtoAddBRC["Location"] = "India";
dtCompany.Rows.Add(drRowtoAddBRC);
DataRow drRowtoAddACC = dtCompany.NewRow();
drRowtoAddACC["Location"] = "United Kingdom";
dtCompany.Rows.Add(drRowtoAddACC);
DataRow drRowtoAddCRSUI = dtCompany.NewRow();
drRowtoAddCRSUI["Location"] = "United States";
dtCompany.Rows.Add(drRowtoAddCRSUI);
DataRow drRowtoAddDLL = dtCompany.NewRow();
drRowtoAddDLL["Location"] = "Australia";
dtCompany.Rows.Add(drRowtoAddDLL);
DataRow drRowtoAddGLT = dtCompany.NewRow();
drRowtoAddGLT["Location"] = "Canada";
dtCompany.Rows.Add(drRowtoAddGLT);
return dtCompany;
}
}
}
 
Share this answer
 
his can work and setting the property of the DropDownList onprerender = " Drop_g_SelectedIndexChanged " .This , activate SelectedIndexChaged. Personal , in my case, it did not work only by setting the Drop in DataGridView, AutoPostback = "true ".
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
int id_persoana = (int)(Session["id_pers"]);
grup_pers(id_persoana);
foreach (GridViewRow row in GridView1.Rows)
{
string strId = (string)(Session["Id"]);

DropDownList Drop_g =(DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("Drop_g");
TextBox TxtNume = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("TxtNume");
TxtNume.Enabled = false;
TextBox TxtPrenume = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("TxtPrenume");
TxtPrenume.Enabled = false;
TextBox TxtCNP = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("TxtCNP");
TxtCNP.Enabled = false;
TextBox TxtSex = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("TxtSex");
TxtSex.Enabled = false;
TextBox Txt_dataN = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl("Txt_dataN");
Txt_dataN.Enabled = false;
SqlConnection conexiune = new SqlConnection();
conexiune.ConnectionString = "Data Source=10.2.20.00;Initial Catalog=test;Integrated Security=True";
DataSet ds = new DataSet();

SqlDataAdapter da_g = new SqlDataAdapter("exec dbo.[PPPP] '" + strId + "'", conexiune);
da_g.Fill(ds, "AAAA");
Drop_g.DataSource = ds.Tables["AAAA"];
Drop_g.DataTextField = "id_grup";
Drop_g.DataValueField = "id_grup";
Drop_g.DataBind();



}
}

protected void Drop_g_SelectedIndexChanged(object sender, EventArgs e)
{
string strId = (string)(Session["Id"]);
DropDownList Drop_g = (DropDownList)sender;
GridViewRow GridView1= ((GridViewRow)GridView1.Parent.Parent);
TextBox TxtDescrie = (TextBox)Drop_.FindControl("TxtDescrie");
SqlConnection conexiune = new SqlConnection();
conexiune.ConnectionString = "Data Source=10.2.20.00;Initial Catalog=test;Integrated Security=True";
DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter("exec dbo.[DESCRIERE] '" + strId + "' , '"+Drop_g.SelectedItem.Text+"'", conexiune);
da.Fill(ds, "DESCRIERE");
TxtDescrie.Text = ds.Tables["DESCRIERE"].Rows[0]["Descriere"].ToString();

}

XML
<asp:TemplateField HeaderText="Some fild">
                                                    <ItemTemplate>
                                                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("id_grup") %>'></asp:Label>
                                                    </ItemTemplate>
                                                    <EditItemTemplate>
                                                        <asp:DropDownList ID="Drop_grupari" runat="server"  Height="18px"
                                                            Width="63px" AutoPostBack="True"
                                                            onselectedindexchanged="Drop_g_SelectedIndexChanged"
                                                            onprerender="Drop_g_SelectedIndexChanged" >
                                                        </asp:DropDownList>
                                                    </EditItemTemplate>
                                                </asp:TemplateField>
 
Share this answer
 
CS CODE:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddlcouse = (DropDownList)sender;
        DataListItem Grow = (DataListItem)ddlcouse.NamingContainer;
        foreach (DataListItem dst in dstAllSite.Items)
        {
            DropDownList ddl_AccountInfo = (DropDownList)dst.FindControl("ddlAccountInfo");
            if (ddlcouse.SelectedIndex > 0)
            {
                BindAccountDDL(ddl_AccountInfo, ddlcouse.SelectedValue.Trim());
                ddlcouse.Focus();
            }
        }
    }

    private void BindAccountDDL(DropDownList ddlAccountInfo, string ddlcouse)
    {
        csPL.ddlcouse = ddlcouse;
        DataTable dt = csBL.BindAccountInfoDDL(csPL);
        if (dt.Rows.Count > 0)
        {
            ddlAccountInfo.DataSource = dt;
            ddlAccountInfo.DataTextField = "BankName";
            ddlAccountInfo.DataValueField = "BankId";
            ddlAccountInfo.DataBind();
            ddlAccountInfo.Items.Insert(0, "Select");
        }
    }
 
Share this answer
 

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