0

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();

            }

        }

Edited by Ezzaral: Added code tags. Please use them to format all code that you post.

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Sai Isha
0
<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();
 
            }
 
        }
0
<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" />

Edited by AhmedSaud: n/a

0

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
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.