locked
GridView Lookup RRS feed

  • Question

  • User878358631 posted

    Hi

    I’m struggling to find a way to show Parent Category data as the ‘Name’, rather that the CategoryID.

    I am using the GridView with a SQLDataSource connection string to retrieve the following information from a database table.

    Here is the current output.

    CategoryID Name ParentCategory
    01 Antiques
    02 Bikes
    03 Food
    04 Biscuits 03
    05 Crisps 03
    06 Banana 03


    And the desired output is to be as below.  I have converted the ParentCategory field to a template field, but so far have not able to figure out how to show the ParentCategory Name, and not the CategoryID.

    ID Name ParentCategory
    01 Antiques
    02 Bikes
    03 Food
    04 Biscuits Food
    05 Crisps Food
    06 Banana Food



    Any pointers would be greatly appreciated.

    Many Thanks, James

    Monday, March 18, 2013 12:29 PM

Answers

  • User1428336426 posted

    Remove SQLDatasource and write below code on page load

    string strqry = " select c.ID, c.Name, P.ParentCategory from childtable c inner join parenttable p on c.ParentCategory = p.ParentCategory";

    SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strCon"]);
    public SqlCommand Com;
    public SqlDataReader dr;
    this.Com = new SqlCommand(strqry, connection);
    connection.Open();
    Com.CommandType = CommandType.Text;
    Com.CommandText = strqry;
    SQLDataAdapter da = new SQLDataAdapter(cmd);
    Dataset ds = new Dataset();}
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 21, 2013 1:07 AM

All replies

  • User1428336426 posted

    use inner Join for ex below query might be useful to you

    select c.ID, c.Name, P.ParentCategory from childtable c
    inner join parenttable p on c.ParentCategory = p.ParentCategory

    Tuesday, March 19, 2013 4:31 AM
  • User878358631 posted

    Hello rimagandhi, 

    Thank you for your response, however my code produced by Visual Web developer is as follows; so I'm not sure how to incorporate the code you offered.  

    Is there another way to achieve what I need by develeoping the code below further ?

    Thank in advanced for reading, 

    Kind Regards, James

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

    ConnectionString="<%$ ConnectionStrings:SQLConnectionString %>"
    SelectCommand="SELECT * FROM [Category]"></asp:SqlDataSource>

    ----------------

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
         <Columns>
                 <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
                 <asp:CheckBoxField DataField="visible" HeaderText="visible" SortExpression="visible" />
                 <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
                 

                 <asp:TemplateField HeaderText="parentCategoryId" SortExpression="parentCategoryId">
                              <ItemTemplate>
                              <asp:Label ID="Label1" runat="server" Text='<%# Bind("parentCategoryId") %>'></asp:Label>
                              </ItemTemplate>
                 </asp:TemplateField>

         </Columns>
    </asp:GridView>

    Wednesday, March 20, 2013 9:53 AM
  • User1428336426 posted

    Remove SQLDatasource and write below code on page load

    string strqry = " select c.ID, c.Name, P.ParentCategory from childtable c inner join parenttable p on c.ParentCategory = p.ParentCategory";

    SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strCon"]);
    public SqlCommand Com;
    public SqlDataReader dr;
    this.Com = new SqlCommand(strqry, connection);
    connection.Open();
    Com.CommandType = CommandType.Text;
    Com.CommandText = strqry;
    SQLDataAdapter da = new SQLDataAdapter(cmd);
    Dataset ds = new Dataset();}
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 21, 2013 1:07 AM