Link to home
Start Free TrialLog in
Avatar of thomasmutton
thomasmuttonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.NET Gridview Dropdownlist

Hello experts,

I have a table called tbl_images which stores information about images..

I am binding this to a gridview and all is working fine.

I also have a table called tbl_gallery which stores records from the tbl_images table.

I want the dropdownlists to be databound and to select yes if that record is in the gallery table and to select no if the record is not in the gallery table.

What is the best way to do this?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" Width="100%"
                DataSourceID="SqlDataSource1" AllowPaging="true" PageSize="20"
                DataKeyNames="image_Id" SkinID="Table1" 
            onrowdatabound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"
                        HeaderStyle-VerticalAlign="Middle" ItemStyle-Width="70">
                        <HeaderTemplate>
                            <input id="chkAll" title="Select all" onclick="javascript:SelectAllCheckboxes(this);" runat="server"
                                type="checkbox" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkDelete" ToolTip="Select photo" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Image" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Image ID="Image1" ImageUrl='<%# "~/Images/Uploaded/100/" + Convert.ToString(DataBinder.Eval(Container.DataItem, "image_filename")) %>'
                                runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Title" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
                        <ItemTemplate>
                            <asp:Literal ID="Name" Text='<%# DataBinder.Eval(Container.DataItem, "image_title") %>' runat="server"></asp:Literal>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField ReadOnly="true" HeaderStyle-Wrap="true" HeaderText="Date Added"
                        DataFormatString="{0:dd/MM/yy }" DataField="image_dateadded" />
                    <asp:BoundField ReadOnly="true" HeaderStyle-Wrap="true" HeaderText="Date Taken"
                        DataFormatString="{0:dd/MM/yy }" DataField="image_datetaken" NullDisplayText="Unknown" />
                    <asp:TemplateField HeaderText="Visible in Gallery" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>                                
                            <asp:DropDownList ID="DropDownList1" runat="server">
                                <asp:ListItem Text="Yes" Value="True"></asp:ListItem>
                                <asp:ListItem Text="No" Value="False"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <EmptyDataTemplate>You do not have any photos</EmptyDataTemplate>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server">
            </asp:SqlDataSource>
 
    public void displayall() // Display all
    {
        MembershipUser user = Membership.GetUser();
 
        StringBuilder sb = new StringBuilder();
        sb.Append("SELECT * ");
        sb.Append("FROM Gower_tbl_images i ");
        sb.Append("INNER JOIN Gower_tbl_gallery g ON ");
        sb.Append("i.image_Id = g.image_Id ");
        sb.Append("WHERE i.UserId = '" + user.ProviderUserKey.ToString() + "' ");
        sb.Append("ORDER BY i.image_dateadded DESC");
 
        SqlDataSource1.SelectCommand = sb.ToString();
    }

Open in new window

Avatar of sunithnair
sunithnair

Why have you used inner join? Inner join will not give you any results when no image_Id does not exist in gallery table
You can use left join and then ha ve a column that evaluates to "True" or "False" according to the image_Id ecisting in gallery table or not.
<asp:TemplateField HeaderText="Visible in Gallery" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>                                
                            <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue="<%#Bind('Exists')%>">
                                <asp:ListItem Text="Yes" Value="True"></asp:ListItem>
                                <asp:ListItem Text="No" Value="False"></asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>

Open in new window

Avatar of thomasmutton

ASKER

Ok thanks for that. I will use left join. How do I code the sql to evaluate if the image_Id exists in the gallery table?
Can you post your table structure?
tbl_images
image_Id            int PK
image_title        
image_filename
etc etc

tbl_gallery
gallery_Id           int PK
image_Id            int FK
etc etc
ASKER CERTIFIED SOLUTION
Avatar of sunithnair
sunithnair

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
is this for a stored procedure or Sql text?

thanks
You can do it either way..