Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
DropDownList In GridView problem
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01455784
Message ID:
01456647
Views:
31
>>>>>>>>>I have a DropDownList in a DGV on my page"
>>>>>>>>>
>>>>>>>>>
>>>>>>>>><asp:GridView ID="GridView1" 
>>>>>>>>>            runat="server" 
>>>>>>>>>            AutoGenerateColumns="False">
>>>>>>>>>
>>>>>>>>>    <Columns>
>>>>>>>>>
>>>>>>>>>        <asp:BoundField DataField="RightKey" HeaderText="RightKey" />
>>>>>>>>>
>>>>>>>>>        <asp:TemplateField HeaderText="Allow / Deny">
>>>>>>>>>            <ItemTemplate>
>>>>>>>>>                <asp:DropDownList DataSource='<# GetAllowDenyValues() >' DataTextField="AllowDeny" 
>>>>>>>>>                    DataValueField="AllowDeny" ID="AllowDeny" runat="server"></asp:DropDownList>
>>>>>>>>>            </ItemTemplate>
>>>>>>>>>        </asp:TemplateField>
>>>>>>>>>
>>>>>>>>>        <asp:BoundField DataField="RightName" HeaderText="Right Name" />
>>>>>>>>>
>>>>>>>>>    </Columns>
>>>>>>>>>
>>>>>>>>></asp:GridView>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>I'm then doing this in the C# code:
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>private string ConnectionString = string.Empty;
>>>>>>>>>
>>>>>>>>>protected void Page_Load(object sender, EventArgs e)
>>>>>>>>>{
>>>>>>>>>    if (!IsPostBack)
>>>>>>>>>    {
>>>>>>>>>        ConnectionString = ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString;
>>>>>>>>>        BindData();
>>>>>>>>>    }
>>>>>>>>>}
>>>>>>>>>
>>>>>>>>>public DataSet GetAllowDenyValues()
>>>>>>>>>{
>>>>>>>>>    SqlConnection myConnection = new SqlConnection(ConnectionString);
>>>>>>>>>    SqlDataAdapter ad = new SqlDataAdapter("SELECT DISTINCT CASE WHEN AllowDeny = 0 " +
>>>>>>>>>        " THEN 'No' WHEN AllowDeny = 1 THEN 'Yes'END AS AllowDeny FROM sec_Rights ", myConnection);
>>>>>>>>>    DataSet ds = new DataSet();
>>>>>>>>>    ad.Fill(ds, "AllowDeny");
>>>>>>>>>    return ds;
>>>>>>>>>}
>>>>>>>>>
>>>>>>>>>private void BindData()
>>>>>>>>>{
>>>>>>>>>    SqlConnection conn = new SqlConnection(ConnectionString);
>>>>>>>>>    SqlParameter pUserKey = new SqlParameter("@UserKey", 2);
>>>>>>>>>    SqlCommand cmd = new SqlCommand();
>>>>>>>>>    cmd.Parameters.Add(pUserKey);
>>>>>>>>>    cmd.Connection = conn;
>>>>>>>>>    cmd.CommandText = "as_GetRightsForUserEx";
>>>>>>>>>    cmd.CommandType = CommandType.StoredProcedure;
>>>>>>>>>
>>>>>>>>>    SqlDataAdapter ad = new SqlDataAdapter();
>>>>>>>>>    ad.SelectCommand = cmd;
>>>>>>>>>    DataSet ds = new DataSet();
>>>>>>>>>    ad.Fill(ds, "Rights");
>>>>>>>>>    GridView1.DataSource = ds;
>>>>>>>>>    GridView1.DataBind();
>>>>>>>>>}
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>The DropDownList is being loaded ok, but all columns show 'No'. The column is not respecting the data's AllowDeny column which
>>>>>>>>>is INT(1 or 0). Only one of the 5 rows being returned in my test actually have a 0 in it.
>>>>>>>>
>>>>>>>>Where are you getting five rows from? The SQL should only return two (assuming there's at least one 1 and one 0 and no other values in the AllowDeny column....
>>>>>>>
>>>>>>>The 5 rows are returned from the call to the stored proc as_GetRightsForUserEx in BindData. The Yes/No are only the allowable values for the AllowDeny column in the grid.
>>>>>>
>>>>>>It's not too clear what you're trying to do - but it looks overly complicated :-}
>>>>>>Instead of getting the Listtems for the DropDownList from the DB would something like this work:
<asp:TemplateField HeaderText="Allow / Deny">
>>>>>>            <ItemTemplate>
>>>>>>              <asp:DropDownList Id="AllowDeny" runat="server"
>>>>>>                 SelectedValue='< %# Bind("AllowDeny") % >' >
>>>>>>                 <asp:ListItem Value="1">Yes</asp:ListItem>
>>>>>>                 <asp:ListItem Value="0">No</asp:ListItem>
>>>>>>              </asp:DropDownList>
>>>>>>          </ItemTemplate>
>>>>>></asp:TemplateField>
>>>>>
>>>>>
>>>>>What I want to do is create a grid with the name of an access right and a combo that says "Yes" or "No". In the dataset there is a
>>>>>column called AllowDeny with 1 or 0. I want the combo to be bound to the AllowDeny column, so that for each row, Yes shows if
>>>>>the AllowDeny column has a 1 in it, and "No" shows if AllowDeny is 0.
>>>>
>>>>That's exactly what the code above does......
>>>>>
>>>>>I could also use a checkbox if that's easier.
>>>
>>>I get a compile err "The 'SelectedValue' property cannot be set declaratively." When I comment it out, it works but not bound.
>>
>>If you pasted my code then get rid of the extra space before and after the % in that line (they were added to satisfy UT checks)
>
>That did it. This also works well for my design:
>
>
><asp:TemplateField HeaderText="Allow / Deny">
>    <ItemTemplate>
>        <asp:CheckBox 
>            ID="chkStatus" 
>            runat="server" 
>            Checked='< %# Convert.ToBoolean(Eval("AllowDeny")) % >'
>            Text='< %# Eval("AllowDeny").ToString().Equals("1") ? " Allow " : " Deny " % >' />
>    </ItemTemplate>                    
></asp:TemplateField>
>
FWIW I think a hybrid solution would be most intuitive : Use a ComboBox but with Allow'/Deny as the options rather than Yes/No. Having the checkbox flip meaning depending on the current setting could be a bit confusing......
Previous
Reply
Map
View

Click here to load this message in the networking platform