C# .NET - Gridview: Label Binding

Asked By Martin Howell
09-Sep-10 03:58 AM

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>





  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
  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>
  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
  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.












  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
help
www.teresablanco.com / Listing / VirtualTour.aspx?ListingID = 958035< / VirtualTourUrl> <Location> <GeoArea id = "207550" type = "Neighborhood" name = "Midtown" / > <GeoArea id = "44569" type = "City" name = "Atlanta" / > <GeoArea id = "289" type = "State" name = "Georgia" abbreviation = "GA" / > <GeoArea id = "44" type = "Country" name = "United States" abbreviation = "US" / > <Address publiclyVisible = "true" suite = "1505"> 120 Ralph McGill Blvd< / Address> <PostalCode p2a / listing / 8842 / 1168 / 8e2b / e709c93a42ec0f0a5eb5 / w64h48.jpg< / Url> < / Item> < / Media> <ListingAgent> <AgentID> 216636< / AgentID> <Name> Teresa Blanco< / Name> <ContactInfo> <Telephone name = "Work"> 404-664-4927< / Telephone> <WebSite> http: / / www.teresablanco.com< / WebSite> <ProfileUrl> http: / / NLS.point2 point2.com / p2a / agency / bf4c / 9c4b / 9561 / 638f22f013dbc6597213 / w160h120.jpg< / Url> < / Item> < / Media> < / ListingAgent> <Broker> <Name> Blue Sky Real Estate Group< / Name> < / Broker> < / Listing> <Listing> <ListingID> 958041< / ListingID> <RegionalMLSNumber publiclyVisible = "1
Sample XML file <?xml version = "1.0" encoding = "utf-8"?> < params > < param > < value > < struct > < member > < name > result_count< / name > < value > < int > 1< / int > < / value > < / member > < member > < name > next_offset< / name > < value > < int > 1< / int > < / value > < / member > < member > < name > field_list< / name > < value > < array > < data > < value > < struct > < member > < name > name< / name > < value > < string > id< / string > < / value > < / member > < member > < name > type< / name > < value > < string > id< / string
when the query is run from .NET Application under sql username informa: <Event id = "14" name = "Audit Login"> <Column id = "1" name = "TextData"> - - network protocol: TCP / IP set quoted_identifier on set arithabort off set numeric_roundabort off set set language us_english set dateformat mdy set datefirst 7 set transaction isolation level read committed < / Column> <Column id = "9" name = "ClientProcessID"> 6820< / Column> <Column id = "11" name = "LoginName"> informa< / Column> <Column id = "10" name = "ApplicationName"> .Net SqlClient Data Provider
error here ? Additional information: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'department'. < / asp : TemplateField > < asp : TemplateField HeaderText = "Department"> < ItemTemplate > < asp : Label ID = "lbldepartment" runat = "server" Text = ' <%#Eval ("department ") %> '> < / asp : Label > < / ItemTemplate > < EditItemTemplate > < asp : DropDownList ID = "dropdepartment" runat = "server"> < / asp : DropDownList > < / EditItemTemplate > my qry : adap = new a.postalcode from tbl_register a, tbl_departmentmaster b, tbl_regionmaster c, tbl_statemaster d, tbl_citymaster e where a.department = b.departmentid and a.region = c.id and a.state = d.stateid and a.city = e.cityid