hello everyone, i have a gridview which i have already populated and i got a foreign key Faculty_ID, which i display on the gridview , Now i want to display the Faculty_Name for that Faculty_ID. Can anyone help me out to do this part???? MY CODES BELOW WORKS FINE..
jst help me to display Faculty_Name for that particular Faculty_ID
code for my gridview:

<asp:GridView ID="GridStaff" runat="server" Width="467px" DataKeyNames="Staff_ID" 
            AutoGenerateColumns="False" AllowPaging="True" PageSize="5" 
            CssClass="GridMain" onpageindexchanging="GridStaff_PageIndexChanging" 
            onrowdeleting="GridStaff_RowDeleting" onrowediting="GridStaff_RowEditing" 
            onselectedindexchanged="GridStaff_SelectedIndexChanged" >
            <Columns>
                <asp:TemplateField HeaderText="Staff ID">
                <ItemTemplate>
                    <asp:Label ID="LblID" runat="server" Text='<%#Eval("Staff_ID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>    
                <asp:BoundField HeaderText="Firstname" DataField="Staff_fname" />
                <asp:BoundField  HeaderText="Lastname" DataField="Staff_lname" />
                  <asp:TemplateField HeaderText="Faculty">
                <ItemTemplate>
                    <asp:Label ID="LblFaculty" runat="server" Text='<%#Eval("Faculty_ID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>    
                
                <asp:ButtonField Text="Select" CommandName="Select" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="BtnDelete" ToolTip="Delete Record" OnClientClick="return confirm('Are you sure you want to Delete this record?');" CommandName="Delete" ImageUrl="~/close_a_24.gif" Width="20px" Height="20px" CausesValidation="false" runat="server" />
                    </ItemTemplate>
                 </asp:TemplateField>
                            
            </Columns>
            <RowStyle CssClass="GridRow" />
                <SelectedRowStyle CssClass="GridSelectedRow" />
                <HeaderStyle CssClass="GridHeader" />
        </asp:GridView>

code behind to display the records for the gridview is given below:

protected void BindData()
        {
            SqlConnection Con;
            SqlDataAdapter Da;
            DataSet Ds = new DataSet();
            SqlCommand Cmd = new SqlCommand();

            Con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);

            Cmd.CommandText = @"SELECT * FROM Staff";

            Cmd.Connection = Con;

            try
            {
                Con.Open();
                Da = new SqlDataAdapter(Cmd);
                Da.Fill(Ds);
                Cmd.ExecuteNonQuery();
                GridStaff.DataSource = Ds;
                GridStaff.DataKeyNames = new string[] { "Staff_ID" };
                GridStaff.DataBind();


            }
            finally
            {
                Con.Close();

            }

        }

Recommended Answers

All 3 Replies

<asp:GridView ID="GridStaff" runat="server" Width="467px" DataKeyNames="Staff_ID" 
            AutoGenerateColumns="False" AllowPaging="True" PageSize="5" 
            CssClass="GridMain" onpageindexchanging="GridStaff_PageIndexChanging" 
            onrowdeleting="GridStaff_RowDeleting" onrowediting="GridStaff_RowEditing" 
            onselectedindexchanged="GridStaff_SelectedIndexChanged" >
            <Columns>
                <asp:TemplateField HeaderText="Staff ID">
                <ItemTemplate>
                    <asp:Label ID="LblID" runat="server" Text='<%#Eval("Staff_ID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>    
                <asp:BoundField HeaderText="Firstname" DataField="Staff_fname" />
                <asp:BoundField  HeaderText="Lastname" DataField="Staff_lname" />
                  <asp:TemplateField HeaderText="Faculty">
                <ItemTemplate>
                    <asp:Label ID="LblFaculty" runat="server" Text='<%#Eval("Faculty_ID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>    
 
                <asp:ButtonField Text="Select" CommandName="Select" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="BtnDelete" ToolTip="Delete Record" OnClientClick="return confirm('Are you sure you want to Delete this record?');" CommandName="Delete" ImageUrl="~/close_a_24.gif" Width="20px" Height="20px" CausesValidation="false" runat="server" />
                    </ItemTemplate>
                 </asp:TemplateField>
 
            </Columns>
            <RowStyle CssClass="GridRow" />
                <SelectedRowStyle CssClass="GridSelectedRow" />
                <HeaderStyle CssClass="GridHeader" />
        </asp:GridView>
protected void BindData()
        {
            SqlConnection Con;
            SqlDataAdapter Da;
            DataSet Ds = new DataSet();
            SqlCommand Cmd = new SqlCommand();
 
            Con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
 
            Cmd.CommandText = @"SELECT * FROM Staff";
 
            Cmd.Connection = Con;
 
            try
            {
                Con.Open();
                Da = new SqlDataAdapter(Cmd);
                Da.Fill(Ds);
                Cmd.ExecuteNonQuery();
                GridStaff.DataSource = Ds;
                GridStaff.DataKeyNames = new string[] { "Staff_ID" };
                GridStaff.DataBind();
 
 
            }
            finally
            {
                Con.Close();
 
            }
 
        }
<asp:GridView ID="GridStaff" runat="server" Width="467px" DataKeyNames="Staff_ID" 
            AutoGenerateColumns="False" AllowPaging="True" PageSize="5" 
            CssClass="GridMain" onpageindexchanging="GridStaff_PageIndexChanging" 
            onrowdeleting="GridStaff_RowDeleting" onrowediting="GridStaff_RowEditing" 
            onselectedindexchanged="GridStaff_SelectedIndexChanged" >
            <Columns>
                <asp:TemplateField HeaderText="Staff ID">
                <ItemTemplate>
                    <asp:Label ID="LblID" runat="server" Text='<%#Eval("Staff_ID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>    
                <asp:BoundField HeaderText="Firstname" DataField="Staff_fname" />
                <asp:BoundField  HeaderText="Lastname" DataField="Staff_lname" />
                  <asp:TemplateField HeaderText="Faculty">
                <ItemTemplate>
                    <asp:Label ID="LblFaculty" runat="server" Text='<%#Eval("Faculty_ID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>    
 
                <asp:ButtonField Text="Select" CommandName="Select" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="BtnDelete" ToolTip="Delete Record" OnClientClick="return confirm('Are you sure you want to Delete this record?');" CommandName="Delete" ImageUrl="~/close_a_24.gif" Width="20px" Height="20px" CausesValidation="false" runat="server" />
                    </ItemTemplate>
                 </asp:TemplateField>
 
            </Columns>
            <RowStyle CssClass="GridRow" />
                <SelectedRowStyle CssClass="GridSelectedRow" />
                <HeaderStyle CssClass="GridHeader" />
        </asp:GridView>
protected void BindData()
        {
            SqlConnection Con;
            SqlDataAdapter Da;
            DataSet Ds = new DataSet();
            SqlCommand Cmd = new SqlCommand();
 
            Con = new SqlConnection(ConfigurationSettings.AppSettings["connect"]);
 
            Cmd.CommandText = @"SELECT * FROM Staff";
 
            Cmd.Connection = Con;
 
            try
            {
                Con.Open();
                Da = new SqlDataAdapter(Cmd);
                Da.Fill(Ds);
                Cmd.ExecuteNonQuery();
                GridStaff.DataSource = Ds;
                GridStaff.DataKeyNames = new string[] { "Staff_ID" };
                GridStaff.DataBind();
 
 
            }
            finally
            {
                Con.Close();
 
            }
 
        }

As what I understood,You just need a bound field after Faculty TemplateField

<asp:BoundField  HeaderText="Facultyname" DataField="Faculty_Name" />

As what I understood,You just need a bound field after Faculty TemplateField

<asp:BoundField  HeaderText="Facultyname" DataField="Faculty_Name" />
Thanks you so Ahmedsaud it works fine as i have to modify my query also:-)
thanks u soo much
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.