dropdownlist in datagrind - selected index problem

W

wolfgang wagner

hi all!

after successfully integrating a dropdownlist in my datagrid i have
another problem: i cannot set the selected index of the dropdownlistbox.

here is my code:

hardware.aspx
-------------

<asp:TemplateColumn HeaderText="Rechnertyp">
<ItemTemplate>
<%# getHtrtyp(DataBinder.Eval(Container.DataItem,
"ht_rtyp_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ItemDropDownRechnertyp"
Runat="server" DataTextField="htrtyp_name_str"
DataValueField="htrtyp_ref" SelectedIndex='<%#
GetSelIndex(DataBinder.Eval(Container.DataItem, "ht_rtyp_ref")) %>' />
</EditItemTemplate>
</asp:TemplateColumn>

and the codebehind:
hardware.aspx.cs

protected int GetSelIndex(object strId)
{
return (Convert.ToInt32(strId))-1;
}


this should select the right index. but if i go into edit mode, always
the first item in the ddl is selected!?!?! even if i return e.g. 2.
what am i doing wrong???


regards
wolfgang
 
S

Scott Allen

Are you doing any data binding with the DropDownList? If so, set the
selected index after data binding.
 
W

wolfgang wagner

Scott said:
Are you doing any data binding with the DropDownList? If so, set the
selected index after data binding.

yes is do. but how do i get this selected index _after_ the
databinding?? isn't it ture that the selected index is restetted to 0
after databinding??

greets
wolfgang
 
S

Scott Allen

yes is do. but how do i get this selected index _after_ the
databinding?? isn't it ture that the selected index is restetted to 0
after databinding??

Yes - it is reset. If you have the same data in the drop down -
couldn't you just let the data be restored from viewstate and not
rebind? Perhaps I am not understanding your question, apologies.
 
W

wolfgang wagner

Scott said:
Yes - it is reset. If you have the same data in the drop down -
couldn't you just let the data be restored from viewstate and not
rebind? Perhaps I am not understanding your question, apologies.

hi scott!

if i display the data then an index is selected. but if i go to edit
mode, this selected index is set to 0 after i get the data from the drop
downlist again (requesting the data again from database.

heres some sample code:
page.aspx
<asp:datagrid id="dgSoftware" style="Z-INDEX: 102; LEFT: 23px; POSITION:
absolute; TOP: 68px"
runat="server" Height="235px" Width="856px"
AutoGenerateColumns="False" DataKeyField="sw_ref" GridLines="None"
CellPadding="3" BackColor="White"
BorderWidth="2px" CellSpacing="1" BorderStyle="Ridge"
BorderColor="White">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF"
BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="sw_bez_str"
HeaderText="Bezeichnung"></asp:BoundColumn>
<asp:BoundColumn DataField="sw_herst_str"
HeaderText="Hersteller"></asp:BoundColumn>
<asp:BoundColumn DataField="sw_filesys_str"
HeaderText="Dateisystem"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Softwareart">
<ItemTemplate>
<%# getHtswtyp(DataBinder.Eval(Container.DataItem,
"htswtyp_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownSwref" Runat="server"
DataTextField="htswtyp_name_str" DataValueField="htswtyp_ref" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="sw_bem_str"
HeaderText="Bemerkung"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="zugehörige Hardware">
<ItemTemplate>
<%# getHwref(DataBinder.Eval(Container.DataItem,
"hw_ref")) %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownHwref" Runat="server"
DataTextField="id" DataValueField="hwId"/>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="LinkButton"
UpdateText="Aktualisieren" CancelText="Abbrechen"
EditText="Bearbeiten"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Löschen"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
Position="TopAndBottom" BackColor="#C6C3C6"></PagerStyle>
</asp:datagrid>

and heres the page.aspx.cs:
....
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
showDataSoftware();
showDataHardware();
}
}
....
private void dgSoftware_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.dgSoftware.EditItemIndex = e.Item.ItemIndex;
showDataSoftware();
}
....
protected void dgSoftware_ItemDataBound ( System.Object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e )
{
if( e.Item.ItemType == ListItemType.EditItem)
{
//erzeuge ddl fuer softwareart
DropDownList list2 =
(DropDownList)e.Item.FindControl("DropDownSwref");
string query = "SELECT htswtyp_ref, htswtyp_name_str FROM htswtyp";
this.sqlConn.Open();
SqlCommand cmd = new SqlCommand(query, this.sqlConn);
list2.DataSource = cmd.ExecuteReader();
list2.DataBind();
this.sqlConn.Close();
}
}
....
private void showDataSoftware()
{
string strWherebuilder = "";
for (int i =0; i < this.alHwId.Count; i++)
{
if (this.alHwId.Count == 1 || i == (this.alHwId.Count-1) )
strWherebuilder += this.alHwId.ToString();
else strWherebuilder += this.alHwId.ToString() + " OR hw_ref
= ";
}
string query = "SELECT * FROM sw WHERE hw_ref = " + strWherebuilder;

//DEBUG
this.tbDebug.Text += "QUERY: " + query;

this.sqlConn = new SqlConnection(this.strDbConn);
this.sqlAdapter = new SqlDataAdapter(query, this.sqlConn);
this.sqlConn.Open();
this.dsDaten = new DataSet();
this.sqlAdapter.Fill(this.dsDaten, "sw" );

this.createList(query);

//neue zeile einfuegen
DataRow BlankRow = this.dsDaten.Tables["sw"].NewRow( );
BlankRow[1] = "Bitte Wert eingeben!";
this.dsDaten.Tables[ "sw" ].Rows.InsertAt( BlankRow, 0 );

//daten binden
this.dgSoftware.DataSource = this.dsDaten;
this.dgSoftware.DataBind();
this.sqlConn.Close();
}

i think the problem is to re-request the data from database.

hope you may help me.

regards
wolfgang
 

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

Top