?
Solved

ASP.NET Gridview Dropdownlist

Posted on 2009-02-23
8
Medium Priority
?
1,038 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:thomasmutton
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:sunithnair
ID: 23711495
Why have you used inner join? Inner join will not give you any results when no image_Id does not exist in gallery table
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23711524
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

0
 

Author Comment

by:thomasmutton
ID: 23711801
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?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 16

Expert Comment

by:sunithnair
ID: 23712428
Can you post your table structure?
0
 

Author Comment

by:thomasmutton
ID: 23712847
tbl_images
image_Id            int PK
image_title        
image_filename
etc etc

tbl_gallery
gallery_Id           int PK
image_Id            int FK
etc etc
0
 
LVL 16

Accepted Solution

by:
sunithnair earned 2000 total points
ID: 23713207
Try this one
select i.*, imgexists=case when g.image_id is null then 'False' else 'True' end from tbl_images i left join tbl_gallery g on i.image_id=g.image_id
 
or
 
select i.*, case when g.image_id is null then 'False' else 'True' end as imgexists from tbl_images i left join tbl_gallery g on i.image_id=g.image_id

Open in new window

0
 

Author Comment

by:thomasmutton
ID: 23713430
is this for a stored procedure or Sql text?

thanks
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23713564
You can do it either way..
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question