Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid dropdownList vb.net

Status
Not open for further replies.

sandylou

Programmer
Jan 18, 2002
147
US
Hi all,

I know this has been posted before, but can't seem to get it right. I have a datagrid which looks like the following:

Code:
<asp:DataGrid ID="dgrdDataUpdates"  Runat="server" 
    AutoGenerateColumns="False" AlternatingItemStyle-BackColor="#eeeeee" HeaderStyle-BackColor="Navy" HeaderStyle-ForeColor="White"
HeaderStyle-Font-Size="15pt" HeaderStyle-Font-Bold="True" 
DataKeyField="vchFieldValue" ShowFooter="True"
OnEditCommand="dgrdDataUpdates_EditCommand"
OnCancelCommand="dgrdDataUpdates_CancelCommand"
OnUpdateCommand="dgrdDataUpdates_UpdateCommand"
OnItemCommand="dgrdDataUpdates_ItemCommand">
<Columns>
<asp:TemplateColumn HeaderText="Translated Source" >
<FooterTemplate>
<asp:DropDownList ID="cboAddSys" AutoPostBack="True" Runat="server">    <asp:ListItem Value="P">P</asp:ListItem>
        <asp:ListItem Value="M">M</asp:ListItem>
        <asp:ListItem Value="B" Selected=True>Both</asp:ListItem>
        </asp:DropDownList>
</FooterTemplate>                
<ItemTemplate>
    <%# Container.DataItem("chrTranslatedSource") %>
</ItemTemplate>
<EditItemTemplate>
        <asp:DropDownList ID="cboSys" AutoPostBack="True" Runat="server">
        <asp:ListItem Value="P">P</asp:ListItem>
        <asp:ListItem Value="M">M</asp:ListItem>        <asp:ListItem Value="B">B</asp:ListItem>
        </asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
What I am trying to do is to on edit ensure that the value is selected in the dropdown list item. I know there are a few ways of achieving this, but for some reason, I am getting mixed up. I tried this:


Code:
Dim ddlPaySys As DropDownList = CType(e.Item.FindControl("cboSys"), DropDownList)
   Dim ddlPaySysValue = ddlPaySys.SelectedIndex

   ddlPaySys.Items.FindByValue(e.Item.Cells(4).Text).Selected = True

which doesn't work. I can read the value of the column/row, but am unable to set that dropdown to the value have from the database. I have tried populating the selected index from the web page using databinder.eval, but i get an error doing that as well.

I am not sure, but maybe it is because I can't access the control in edititemtemplate? Any help?

(i only put the dropdown column in my grid code sample, i have a whole bunch of other columns, that is why e.item.cells(4).text shows 4, it is the 5th column in my grid)
 
Do you get an error? Does it just not work? What event is this code in? Can you post allof the code? Have you debugged and stepped through your code?
 
When I run the code I get the following error, which errors out on my dgrdDataUpdates_ItemCommand:

Object reference not set to an instance of an object.

Code:
Sub dgrdDataUpdates_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrdDataUpdates.ItemCommand
        '    Find the drop down control from datagrid items
        Dim ddlPaySys As DropDownList = CType(e.Item.FindControl("cboPaySys"), DropDownList)
        Dim ddlPaySysValue = ddlPaySys.SelectedIndex

        ddlPaySys.Items.FindByValue(e.Item.Cells(4).Text).Selected = True

        
       
    End Sub
 
What line is the error on? You should step through your code. Most likely it is on this line:

Dim ddlPaySys As DropDownList = CType(e.Item.FindControl("cboPaySys"), DropDownList)
 
no it errors on this:
Dim ddlPaySysValue = ddlPaySys.SelectedIndex

But that is because ddlPaySys is Nothing so it doesn't find it.
 
Did you step through the code? Does ddlPaySys containg a dropdownlist or is it NOTHING.
 
Yes I do step through the code. ddlPaySys is nothing, so it does not find the control. It errors out on this: im ddlPaySysValue = ddlPaySys.SelectedIndex, because ddlPaySys is nothing. I tried to see if in the immediate window i could find it, but can't, so i am unsure how to access it. Is it because it is in EditItemTemplate?
 
webforms had event validation enabled by default. a user cannot pass a value from the DDL that is not already present (preventing one form of scripting attacks).

in other words. if your choices from a DDL are 1, 2, 3. you cannot pass 4 back to the form. If you have event validation disabled, then you can check this manually.

whether you do the validation or not, you should not be comparing values against the DDL, but where the data for the DDL comes from. so if the DDL gets it's values from a member like
Code:
get_items_for_drop_down_list();
then you would validate the users selection against that enumeration
Code:
var items = get_items_for_drop_down_list();
if(items.contains(the_selected_value))
{
   //continue processing
}
else
{
   // something is not right.
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
so i should do the setting of that ddl when the form initially loads that grid?
 
no, the setting just happens by default. i'm saying validate the selected items against where the data comes from, not the GUI.

reviewing your dgrdDataUpdates_ItemCommand the problem is your are not checking the type of row. this event fires for all rows: header, footer, data, select, edit, etc. so you need to either check the type of row, or just make sure the ddl is not nothing.
Code:
var ddl = (ddl)e.row.findcontrol(...);
if(ddl == null) return;
var value = ddl.selectedvalue;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
hmm.. i guess i can't get it right, do you have any links i can use to help with understanding the grid in 1.1 better and using vb.net?
 
google is your best source. I haven't worked with vb.net or 1.1 since 2002.

maybe this will help. the GUI (webcontrols, html, etc.) should only be used to get and set values. if you want to do anything with those values (validate, modify, calculate), it should be done in the domain, not the presentation)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top