[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4114
  • Last Modified:

GridView ItemTemplate Field Data Binding

Hello,

I am trying to build a Gridview that has a HeaderRow that consists of both a Text label and a DropDownList right below it for filtering.  Is there a better way to get both of these in the header than what I have in the code snippet HeaderTemplate?

My second issue is that I am not sure how to get my data into the ItemTemplate tags and bind it in code-behind. It only takes server controls.  Currently I have these as boundfields which I just declare the datafield of a dataset in the code-behind to populate them.  

Thanks, sk
<!-- GridView code in .aspx page -->
 
<asp:GridView ID="gvTripFileSearch" runat="server" AutoGenerateColumns="False">
                            <Columns>
                                <asp:TemplateField HeaderText="TripFile Name" SortExpression="TripFileName">
                                    <HeaderTemplate>
                                        TripFile Name</br>
                                       <asp:DropDownList ID="ddTripFileName" runat="server" AutoPostBack="True" />
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        ?????? 
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>

Open in new window

0
kruegerste
Asked:
kruegerste
  • 3
  • 2
1 Solution
 
kruegersteAuthor Commented:
Anybody out there with any suggestions?
0
 
karakavCommented:
You can bind the dropdownlist to value you want it to have. You can use Eval(one way) or Bind(two way) to bind it to a field from a sql query result set, or you can type to values directly if there are not many.

As for the second questio, you will have to bind it this way:

                                   <ItemTemplate>
                                        <asp:Label id="lblName" Text = <%#Eval('name')%>
                                    </ItemTemplate>
name is the columnname from you result set.  If you want two-way binding. Use Bind instead of Eval.
0
 
kruegersteAuthor Commented:
karakav,

Thanks for the response but I believe you misunderstood my first question, binding the dropdownlist is not a problem.  I will re-iterate it here:

1.  I am trying to build a Gridview that has a HeaderRow that consists of both a Text label and a DropDownList right below it for filtering.  Is there a better way to get both the text and the dropdownlist in the header than what I have in the code snippet HeaderTemplate (which is the Text<br/>"and then dropdownlist")?

2. As for the 2nd question, you state I need to use a Label field with the Bind or Eval, those are the only way to bind it.  What would the code behind look like?  Are you saying that if I specify the datafield in the Eval and then bind the dataset to the gridview, that the label will automatically find that datafield and populate.

Thanks.
0
 
karakavCommented:
I am not sure if I understand you well. I am sending you the snippet of what I did. Is that what you were expecting for?
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SortableGrid.aspx.cs" Inherits="SortableGrid" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvTripFileSearch" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="TripFile Name" SortExpression="TripFileName">
                    <HeaderTemplate>
                        <asp:LinkButton ID="CompanyHeader" runat="server" Text="TripFile Name" CommandArgument="" CommandName="sort" />
                        <asp:DropDownList ID="ddTripFileName" runat="server" AutoPostBack="True"  OnSelectedIndexChanged="SortData">
                            <asp:ListItem Text="Asc" Value="0" Selected="True" />
                            <asp:ListItem Text="Desc" Value="1" />
                        </asp:DropDownList>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="custId" runat="server" Text='<%#Eval("CompanyName") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
 
//Here is the code behind file
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
 
public partial class SortableGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        gvTripFileSearch.DataSource = LoadData(String.Empty, String.Empty);
        gvTripFileSearch.DataBind();
    }
    private DataSet LoadData(String SortColumn, String SortDirection)
    {
        DataSet result = null;
 
        SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");
 
        connection.Open();
        try
        {
            String command = null;
            if (!String.IsNullOrEmpty(SortColumn) && !String.IsNullOrEmpty(SortDirection))
            {
                command= String.Format("select CustomerID, CompanyName from customers order by {0} {1}", SortColumn, SortDirection);
            }
            else
            {
                command = "select CustomerID, CompanyName from customers ";
            }
            SqlDataAdapter adapter = new SqlDataAdapter(command, connection);
 
            adapter.SelectCommand.CommandType = CommandType.Text;
 
            result = new DataSet();
 
            adapter.Fill(result);
 
        }
        finally
        {
            connection.Close();
        }
 
 
        return result;
    }
}

Open in new window

0
 
kruegersteAuthor Commented:
Yes, that is what I was looking for.  Using Eval did not work for some reason, but as soon as I used Bind, the data showed up in the Label fields.

Thanks.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now