Gridview: Label Binding

Asked By Martin Howell
09-Sep-10 03:58 AM
Earn up to 0 extra points for answering this tough question.

I am using a grid control which has an edit mode and shows Support Requests.

I have changed Deaprtment to have a dropdownlist in edit mode which is working fine, what I would like to do is have a label display the "Department Name" column when not in edit mode as a "Department ID" is not very informative unless you know what the various codes mean.

Department Table for example consists of:

DepartmentID    Department
1              Admin
2              HR
3              Sales

The Support Request Table saves the department for each record as "DepartmentID".

I've been searching around and can't find anything on this, can someone help me out please.

Here is my code:

<form id="form1" runat="server">
<div>
  
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="RequestID" DataSourceID="RequestDataSource">
    <Columns>
      <asp:CommandField ShowEditButton="True" />
      <asp:BoundField DataField="RequestID" HeaderText="RequestID" 
        InsertVisible="False" ReadOnly="True" SortExpression="RequestID" />
      <asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" 
        SortExpression="EmailAddress" />
      <asp:BoundField DataField="Extension" HeaderText="Extension" 
        SortExpression="Extension" />
      <asp:TemplateField HeaderText="DepartmentID" SortExpression="DepartmentID">
        <EditItemTemplate>
          <asp:DropDownList ID="DropDownList1" runat="server" 
            DataSourceID="DepartmentDataSource" DataTextField="Department" 
            DataValueField="DepartmentID" SelectedValue='<%# Bind("DepartmentID") %>'>
          </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
          <asp:Label ID="Label1" runat="server" Text='<%# Bind("DepartmentID") %>'></asp:Label>
        </ItemTemplate>
      </asp:TemplateField>
      <asp:BoundField DataField="PriorityID" HeaderText="PriorityID" 
        SortExpression="PriorityID" />
      <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
        SortExpression="CategoryID" />
      <asp:BoundField DataField="Subject" HeaderText="Subject" 
        SortExpression="Subject" />
      <asp:BoundField DataField="Problem" HeaderText="Problem" 
        SortExpression="Problem" />
      <asp:BoundField DataField="Created" HeaderText="Created" 
        SortExpression="Created" />
      <asp:BoundField DataField="StatusID" HeaderText="StatusID" 
        SortExpression="StatusID" />
      <asp:BoundField DataField="AssignedID" HeaderText="AssignedID" 
        SortExpression="AssignedID" />
    </Columns>
  </asp:GridView>
  <asp:SqlDataSource ID="RequestDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ISDSupportConnectionString %>" 
    DeleteCommand="DELETE FROM [supportRequest] WHERE [RequestID] = @RequestID" 
    InsertCommand="INSERT INTO [supportRequest] ([EmailAddress], [Extension], [DepartmentID], [PriorityID], [CategoryID], [Subject], [Problem], [Created], [StatusID], [AssignedID]) VALUES (@EmailAddress, @Extension, @DepartmentID, @PriorityID, @CategoryID, @Subject, @Problem, @Created, @StatusID, @AssignedID)" 
    SelectCommand="SELECT * FROM [supportRequest]" 
    UpdateCommand="UPDATE [supportRequest] SET [EmailAddress] = @EmailAddress, [Extension] = @Extension, [DepartmentID] = @DepartmentID, [PriorityID] = @PriorityID, [CategoryID] = @CategoryID, [Subject] = @Subject, [Problem] = @Problem, [Created] = @Created, [StatusID] = @StatusID, [AssignedID] = @AssignedID WHERE [RequestID] = @RequestID">
    <DeleteParameters>
      <asp:Parameter Name="RequestID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
      <asp:Parameter Name="EmailAddress" Type="String" />
      <asp:Parameter Name="Extension" Type="String" />
      <asp:Parameter Name="DepartmentID" Type="Int32" />
      <asp:Parameter Name="PriorityID" Type="Int32" />
      <asp:Parameter Name="CategoryID" Type="Int32" />
      <asp:Parameter Name="Subject" Type="String" />
      <asp:Parameter Name="Problem" Type="String" />
      <asp:Parameter Name="Created" Type="DateTime" />
      <asp:Parameter Name="StatusID" Type="Int32" />
      <asp:Parameter Name="AssignedID" Type="Int32" />
    </InsertParameters>
    <UpdateParameters>
      <asp:Parameter Name="EmailAddress" Type="String" />
      <asp:Parameter Name="Extension" Type="String" />
      <asp:Parameter Name="DepartmentID" Type="Int32" />
      <asp:Parameter Name="PriorityID" Type="Int32" />
      <asp:Parameter Name="CategoryID" Type="Int32" />
      <asp:Parameter Name="Subject" Type="String" />
      <asp:Parameter Name="Problem" Type="String" />
      <asp:Parameter Name="Created" Type="DateTime" />
      <asp:Parameter Name="StatusID" Type="Int32" />
      <asp:Parameter Name="AssignedID" Type="Int32" />
      <asp:Parameter Name="RequestID" Type="Int32" />
    </UpdateParameters>
  </asp:SqlDataSource>
  
  <br />
  <br />
  <asp:SqlDataSource ID="DepartmentDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ISDSupportConnectionString %>" 
    SelectCommand="SELECT * FROM [supportDepartments]"></asp:SqlDataSource>
  
</div>
</form>





  re: Gridview: Label Binding

Anand Malli replied to Martin Howell
09-Sep-10 05:54 AM
Hi Martin,

this is easy i mean instead of DepartmentID just take your field DepartmentName right so it will display departmentname in your label,now you can take one column visible="false" and store your departmentID over there,so when ever you want to refere to your departmentID you will be having from that column

Thanks

  re: Gridview: Label Binding

Martin Howell replied to Anand Malli
09-Sep-10 06:54 AM

Sorry I don't get what you mean.

This is my Label code right now, which is currently showing the ID, what and how should I change it?

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

  re: Gridview: Label Binding

Anand Malli replied to Martin Howell
09-Sep-10 06:57 AM
what i ment to say is following

<ItemTemplate>
   <asp:Label ID="Label1" runat="server" Text='<%# Bind("DepartmentName") %>'></asp:Label>
</ItemTemplate>
 
and take another column as following
 
<ItemTemplate>
   <asp:Label ID="Label2" runat="server" visible="false" Text='<%# Bind("DepartmentID") %>'></asp:Label>
</ItemTemplate>

now here i what i mean to say is i think you do not want to show id right so instead of that we have taken departmentName so it will display it,now i think you must be having id to track ites record depending on that or you might have to update db as per DepartmentID so we have taken it in hidden (second template column),so both of your purpose would be solved...

am i getting you right???

THanks
  re: Gridview: Label Binding
Martin Howell replied to Anand Malli
09-Sep-10 08:21 AM
I've set the Labels but now it comes up with an error saying that Label1 cannot find the DepartmentName.

The DepartmentName is not present in the Datasource of the Grid which I am guessing is the problem.












  re: Gridview: Label Binding
Anand Malli replied to Martin Howell
09-Sep-10 08:33 AM
you have to select it in your query to display over there right now your select query or SP would be returning only ID so return DepartmentName as well so something like follow

SELECT DepartmentID,DepartmentName from Department

i hope you get it....

Thanks
Create New Account