can't set the selected item of the dropdownlist

D

dada

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
 
S

SB

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
 
D

dada

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>
 
S

SB

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 said:
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>
 
D

dada

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));
//
// }
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top