can't set the selected item of the dropdownlist

Discussion in 'ASP .Net Datagrid Control' started by dada, Mar 2, 2004.

  1. dada

    dada Guest

    I generate template columns in datagrid dynamic
    and I want dropdownlist in the EditItemTemplate, I did it

    But I can't set the selected item of the dropdownlist
    All the dropdownlists are selected at the 1st one item

    How to set it?
    Somebody can help me, thanks Dada
    dada, Mar 2, 2004
    #1
    1. Advertising

  2. dada

    SB Guest

    Dada,

    In the DataaGrid_ItemDataBound event, you must detect wether the current
    line is an EditItem. Then you can select the value in the dropdownlist.

    ex:
    DropDownList ddl=(DropDownList)e.Item.FindControls("ddlControlName");
    ddl.SelectedIndex=ddl.Items.IndexOf(ddl.Items.FindByValue("TheSelectedValue)
    );

    Good luck.

    Selim
    "dada" <> wrote in message
    news:...
    > I generate template columns in datagrid dynamic
    > and I want dropdownlist in the EditItemTemplate, I did it
    >
    > But I can't set the selected item of the dropdownlist
    > All the dropdownlists are selected at the 1st one item
    >
    > How to set it?
    > Somebody can help me, thanks Dada
    SB, Mar 2, 2004
    #2
    1. Advertising

  3. dada

    dada Guest

    Than you Selim for your help, I have tried that but it doesn`t work.
    I get an error
    "System.NullReferenceException: Object reference not set to an
    instance of an object."

    I am sending you my code, if you be so cind to see what I am doing
    wrong.

    Thank you again,
    Dada

    ***
    using WebRaspored2.localhost;

    namespace WebRaspored2
    {
    public class Departmani : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Table Table1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected WebRaspored2.localhost.DsDepartmani dsDepartmani;

    private void Page_Load(object sender, System.EventArgs e)
    {
    localhost.RasporedService s= new localhost.RasporedService();
    dsDepartmani=s.WebPopuni("Departmani");
    DataGrid1.DataSource=dsDepartmani;
    DataGrid1.DataMember="Departmani";
    if(!IsPostBack)
    DataGrid1.DataBind();
    }
    private void DataGrid1_UpdateCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    localhost.RasporedService s= new localhost.RasporedService();

    string naz;
    int Institucije;
    string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
    TextBox tb;
    tb=(TextBox)(e.Item.Cells[3].Controls[1]);
    naz = tb.Text;
    DropDownList ddl;
    ddl=(DropDownList)(e.Item.Cells[5].Controls[1]);
    Institucije=Convert.ToInt32(ddl.SelectedItem.Value);
    DsDepartmani.DepartmaniRow rd;
    rd=dsDepartmani.Departmani.FindByID_departmana(int.Parse(key));
    if (rd!=null)
    {
    rd.Naziv=naz;
    rd.ID_institucije=Convert.ToInt32(Institucije);

    s.WebSnimi(dsDepartmani);
    dsDepartmani.AcceptChanges();
    }

    DataGrid1.EditItemIndex = -1;
    DataGrid1.DataBind();

    }

    private void DataGrid1_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    DropDownList ddl=(DropDownList) e.Item.FindControl("DropDownList1");
    ddl.SelectedIndex=ddl.Items.IndexOf(ddl.Items.FindByValue("ID
    institucije"));

    //if(e.Item.ItemType == ListItemType.EditItem)
    // {
    //DataRowView drv = (DataRowView) e.Item.DataItem;
    //String IDinst = drv["ID institucije"].ToString();
    //DropDownList ddl =(DropDownList)
    e.Item.FindControl("DropDownList1");
    //ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(IDinst));
    //
    // }

    }
    HTML:

    <asp:TemplateColumn HeaderText="Institucija">
    <HeaderStyle Width="350pt"></HeaderStyle>
    <ItemTemplate>
    <asp:Label id=Label1 runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.Institucije") %>'>
    </asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:DropDownList id=DropDownList1 runat="server" Width="239px"
    DataMember="Institucije" DataSource="<%# dsDepartmani %>"
    DataTextField="Naziv" DataValueField="ID institucije">
    </asp:DropDownList>
    </EditItemTemplate>
    </asp:TemplateColumn>
    dada, Mar 3, 2004
    #3
  4. dada

    SB Guest

    Hi Dada,

    You are missing the test to check if the current row is an EditItem. If you
    skip that test, you are trying to get the dropdownlist from the header row
    of your grid, then maybe on a row wich is not edited, etc ...

    Here is the code:


    private void DataGrid1_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType==ListItemType.EditItem)
    {
    DropDownList ddl=(DropDownList) e.Item.FindControl("DropDownList1");
    //theValueToSearch => warning: this is a string

    ddl.SelectedIndex=ddl.Items.IndexOf(ddl.Items.FindByValue(theValueToSearch))
    ;
    }
    }


    "dada" <> wrote in message
    news:...
    > Than you Selim for your help, I have tried that but it doesn`t work.
    > I get an error
    > "System.NullReferenceException: Object reference not set to an
    > instance of an object."
    >
    > I am sending you my code, if you be so cind to see what I am doing
    > wrong.
    >
    > Thank you again,
    > Dada
    >
    > ***
    > using WebRaspored2.localhost;
    >
    > namespace WebRaspored2
    > {
    > public class Departmani : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.Table Table1;
    > protected System.Web.UI.WebControls.DataGrid DataGrid1;
    > protected WebRaspored2.localhost.DsDepartmani dsDepartmani;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > localhost.RasporedService s= new localhost.RasporedService();
    > dsDepartmani=s.WebPopuni("Departmani");
    > DataGrid1.DataSource=dsDepartmani;
    > DataGrid1.DataMember="Departmani";
    > if(!IsPostBack)
    > DataGrid1.DataBind();
    > }
    > private void DataGrid1_UpdateCommand(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > localhost.RasporedService s= new localhost.RasporedService();
    >
    > string naz;
    > int Institucije;
    > string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
    > TextBox tb;
    > tb=(TextBox)(e.Item.Cells[3].Controls[1]);
    > naz = tb.Text;
    > DropDownList ddl;
    > ddl=(DropDownList)(e.Item.Cells[5].Controls[1]);
    > Institucije=Convert.ToInt32(ddl.SelectedItem.Value);
    > DsDepartmani.DepartmaniRow rd;
    > rd=dsDepartmani.Departmani.FindByID_departmana(int.Parse(key));
    > if (rd!=null)
    > {
    > rd.Naziv=naz;
    > rd.ID_institucije=Convert.ToInt32(Institucije);
    >
    > s.WebSnimi(dsDepartmani);
    > dsDepartmani.AcceptChanges();
    > }
    >
    > DataGrid1.EditItemIndex = -1;
    > DataGrid1.DataBind();
    >
    > }
    >
    > private void DataGrid1_ItemDataBound(object sender,
    > System.Web.UI.WebControls.DataGridItemEventArgs e)
    > {
    > DropDownList ddl=(DropDownList) e.Item.FindControl("DropDownList1");
    > ddl.SelectedIndex=ddl.Items.IndexOf(ddl.Items.FindByValue("ID
    > institucije"));
    >
    > //if(e.Item.ItemType == ListItemType.EditItem)
    > // {
    > //DataRowView drv = (DataRowView) e.Item.DataItem;
    > //String IDinst = drv["ID institucije"].ToString();
    > //DropDownList ddl =(DropDownList)
    > e.Item.FindControl("DropDownList1");
    > //ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(IDinst));
    > //
    > // }
    >
    > }
    > HTML:
    >
    > <asp:TemplateColumn HeaderText="Institucija">
    > <HeaderStyle Width="350pt"></HeaderStyle>
    > <ItemTemplate>
    > <asp:Label id=Label1 runat="server" Text='<%#
    > DataBinder.Eval(Container, "DataItem.Institucije") %>'>
    > </asp:Label>
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:DropDownList id=DropDownList1 runat="server" Width="239px"
    > DataMember="Institucije" DataSource="<%# dsDepartmani %>"
    > DataTextField="Naziv" DataValueField="ID institucije">
    > </asp:DropDownList>
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    SB, Mar 3, 2004
    #4
  5. dada

    dada Guest

    SB, thanks for your help, but it doesn`t work with me:(. I have tried
    that before, but it didn`t help, and again today, maybe is the problem
    that that coloumn is with FKey?!

    private void DataGrid1_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    DropDownList ddl=(DropDownList) e.Item.FindControl("DropDownList1");
    ddl.SelectedIndex=ddl.Items.IndexOf(ddl.Items.FindByValue("ID
    institucije"));

    //if(e.Item.ItemType == ListItemType.EditItem)
    // {
    //DataRowView drv = (DataRowView) e.Item.DataItem;
    //String IDinst = drv["ID institucije"].ToString();
    //DropDownList ddl =(DropDownList)
    e.Item.FindControl("DropDownList1");
    //ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(IDinst));
    //
    // }
    }
    dada, Mar 4, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Venable
    Replies:
    4
    Views:
    642
    Rob Venable
    Jan 21, 2004
  2. Iain
    Replies:
    3
    Views:
    926
  3. Nathan Sokalski
    Replies:
    0
    Views:
    604
    Nathan Sokalski
    Oct 5, 2008
  4. mldardy
    Replies:
    0
    Views:
    961
    mldardy
    Sep 28, 2010
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    289
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page