Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > can't set the selected item of the dropdownlist

Reply
Thread Tools

can't set the selected item of the dropdownlist

 
 
dada
Guest
Posts: n/a
 
      03-02-2004
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
 
Reply With Quote
 
 
 
 
SB
Guest
Posts: n/a
 
      03-02-2004
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("ddlControlN ame");
ddl.SelectedIndex=ddl.Items.IndexOf(ddl.Items.Find ByValue("TheSelectedValue)
);

Good luck.

Selim
"dada" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
 
dada
Guest
Posts: n/a
 
      03-03-2004
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.Find ByValue("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>
<aspropDownList id=DropDownList1 runat="server" Width="239px"
DataMember="Institucije" DataSource="<%# dsDepartmani %>"
DataTextField="Naziv" DataValueField="ID institucije">
</aspropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
 
Reply With Quote
 
SB
Guest
Posts: n/a
 
      03-03-2004
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.Find ByValue(theValueToSearch))
;
}
}


"dada" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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.Find ByValue("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>
> <aspropDownList id=DropDownList1 runat="server" Width="239px"
> DataMember="Institucije" DataSource="<%# dsDepartmani %>"
> DataTextField="Naziv" DataValueField="ID institucije">
> </aspropDownList>
> </EditItemTemplate>
> </asp:TemplateColumn>



 
Reply With Quote
 
dada
Guest
Posts: n/a
 
      03-04-2004
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.Find ByValue("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));
//
// }
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net Web Controls 0 10-05-2008 10:29 PM
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net 0 10-05-2008 10:29 PM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
Getting te ID of a Selected Item in a dropdownlist. Rob Venable ASP .Net 4 01-21-2004 11:39 PM



Advertisments