SelectedIndexChaged for DropDownList in datagrid

asp.net , csharp Madagascar
  • 13 years ago

    hello

    I developpe with visual studio 2003 and C#

    I want to create a event SelectedIndex on the DropDownList in datagrid but i can' t

    so i have two DropdownList and  if i change the first i want to change automatically the second

    if it is not in datagrid i can do it but my problem is the dropdownlist are in a datagrid

    can you help me please

  • 13 years ago

    Hi,

    This is known as event bubbling.

    When you have many controls in data grid and you want to handle their events.

    For all the controls in data grid there is one common event i.e OnItemCommand(object o, DataGridCommandEventArgs).

    protected void OnItemCommand(object o, DataGridCommandEventArgs e).

    {

    when the index of the drop list will be changed  this event is fired and you can populate as mant combo box as you want.

    Also if you want to fetch the values of any controls you can do like this.

    string val = e.Item.Cells[3].Controls[0].value;

    or any thing depends on your control.

    I Hope it will solve your problem.

     

    }

  • 13 years ago

    hi,

    i don't know whether we can handle selectedindexchange in data grid or not.

    Do one thing

    set autopostback=true and define commandname property with drop down list in data grid.

    in itemcommand event check for e.commandsource.commandname=""

    or check this one.

    http://dotnetjunkies.com/Tutorial/64CB8B03-8801-45C7-AC3E-F97EDC4CA0BD.dcik

  • 13 years ago

    hi

    ok i try it!

  • 13 years ago

    I don't inderstand

    can you give more explation please

  • 13 years ago

    i have this error

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS0122: 'FicheCCC.DocLegaux.DropDownList_SelectedIndexChanged(object, System.EventArgs)' is inaccessible due to its protection level

    Source Error:





    Line 203: </ItemTemplate>
    Line 204: <FooterTemplate>
    Line 205: <asp:DropDownList id="ddlBankDescNew" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"></asp:DropDownList>

    Line 206: </FooterTemplate>
    Line 207: <EditItemTemplate>


     

    what does it mean?

    thanks

  • 13 years ago

    I think we can't handle drop down changes event coz data grid is its parent control.

    whenever any events occure in child controls it is passed to parents controlsi.e Data grid should handle it.  Remove OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"

    from declaration. instead specify commandname="ddlchanged".

    Onitemcommand event of data grid check for

    if(e.Commandsource.CommandName=="ddlchanged")

    {

    }

    commandname  property is different for every control that we include in data grid.

    when index of list changed you can put whatever code you want in these blocks.

    also it is talking about inaccessible level.

    try to changed drop down declaration from protection to public and see if it works.

    Bye

     

    .

     

  • 13 years ago

    hi,

    so you say that i must remove the event SelectedIndexChaged on the datagrid

    and declare  commandname="ddlchanged". so where can i declare it because when i create the dropdownlist there is no CommandName in his properties

    thanks

  • 13 years ago
    programatically, you need to cast the dropdown from the grid and make the changes for the second gdropdown from the event of the first dropdown control.
  • 13 years ago

    hi

    but i'am a hight beginers in the C# and asp.net so i need a help

    thanks 

  • 13 years ago

    Help

    i'm blocked

  • 13 years ago
    hey, can you paste your code from here. just the html and i'll provide the solution for your problem.
  • 13 years ago
    You can add SelectedIndexChanged event for the child dropdownlist inside the datagrid in the ItemCreated event of the datagrid

            private void Datagrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
            {
                if (e.Item.ItemType==ListItemType.EditItem)
                {
                    DropDownList dropdownlist1 = (DropDownList) e.Item.FindControl("dropdownlist1");
                    dropdownlist1.SelectedIndexChanged += new EventHandler(dropdownlist1_SelectedIndexChanged);
                }
            }

            private void dropdownlist1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                    DropDownList dropdownlist1 = (DropDownList) e.Item.FindControl("dropdownlist1");
                    DropDownList dropdownlist2 = (DropDownList) e.Item.FindControl("dropdownlist2");
           //     BindDropdownlist2
            }

    i hope this helps you



















  • 13 years ago

    hi 

    i will try it and i will give news

    thanks

  • 13 years ago

    hi

    when i use the code

    private

    void ddlBankDescNew_SelectedIndexChanged(object sender, System.EventArgs e)

    {

    DropDownList ddlBankDescNew = (DropDownList) e.Item.FindControl("ddlBankDescNew");

    //DropDownList dropdownlist2 = (DropDownList) e.Item.FindControl("dropdownlist2");

    // BindDropdownlist2

    lblMess.Text = ddlBankDescNew.SelectedValue;

    }

    i have a errot like "System.EvetArgs does not contain a definition for Item"

    i think tah it's true because Item is an

    System.Web.UI.WebControls.DataGridItemEventArgs

    so what can i do

    thanks

  • 13 years ago

    http://www.developerfusion.co.uk/forums/post/156853/

    try to check this post papat.....

  • 13 years ago
    DropDownList ddlBankDescNew =Datagrid1.Items[Datagrid1.EditItemIndex].FindControl("ddlBankDescNew");

    try this



  • 13 years ago
    See my posting on this link for more information

  • 13 years ago

    hi

    thanks ,  it's ok for the EditItem(when i make update) but my datagrid has a footer for the insertion and i want to make the same but i don't know what are the code that i must change

    it's the code for the EditItem(update)

    DropDownList ddlBankDescMaj = (DropDownList) dgBank.Items[dgBank.EditItemIndex].FindControl("ddlBankDescMaj");

    DropDownList ddlBankAgenceDescMaj = (DropDownList) dgBank.Items[dgBank.EditItemIndex].FindControl("ddlBankAgenceDescMaj");

    lblMess.Text = ddlBankDescMaj.SelectedValue.ToString();

    BindAgence(ddlBankAgenceDescMaj);

    i think that i must change this code : dgBank.EditItemIndex but i don't know how

    thanks

  • 13 years ago

    hi

    thanks

    i found it

    code

    protected

    void ddlBankDescNew_SelectedIndexChanged(object sender, System.EventArgs e)

    {

    int footerIndex = dgBank.Controls[0].Controls.Count-1;

    DropDownList ddlBankDescNew = dgBank.Controls[0].Controls[footerIndex].FindControl("ddlBankDescNew")

    as DropDownList;

    DropDownList ddlBankAgenceDescNew= dgBank.Controls[0].Controls[footerIndex].FindControl("ddlBankAgenceDescNew")

    as DropDownList;

    string c =ddlBankDescNew.SelectedValue.ToString();

    BindAgence(ddlBankAgenceDescNew,c);

    }

    ok

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter” - Eric Raymond