Using Bound Controls in Gridview Column Header

Hi Folks,

Please tell me how do I use bound controls in Gridview Column Header. Lets say, I want to see the User Name as the Header Text for a Gridview Column.


Red in Heart...
0
kiransdusk
4/15/2008 6:43:39 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

4 Replies
554 Views

Similar Articles

[PageSpeed] 59

Hi,

I think, If you want to bind any field in your database to the Header of the Column in the GridView control. Then you can add a TemplateField in the gridview control and then add a Textbox or Label field in the HeaderTemplate and then bind it to the field in your Database table.

Regards,

Manish

www.ComponentOne.com

 

 


Manish Singhal
www.ComponentOne.com
0
manish
4/15/2008 9:39:25 AM

Try this, 

<asp:TemplateField>

         <asp:HeaderTemplate>

                  <asp:Label ID="lbl" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "column name") %>' />

         </asp:HeaderTemplate>

</asp:TemplateField>
   

If this helps, click "Mark as Answer" 

 


Sivaraman Krishnan
0
Sivaraman
4/15/2008 10:01:17 AM

In the way of an explanation, Manish was concise and correct. Not much I can add there except for an example for illustration purpose

This page has a dropdown loaded with all the customers from Northwind Database.  What it does is filter the output to the Gridview by customerid.  The output is the orders per customer.

Hope it helps

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ddlInGvHdr.aspx.cs" Inherits="ddlInGvHdr" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="OrderID" DataSourceID="SqlDataSource1" >
            <Columns>
                <asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="OrderID" />
                <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" 
                    SortExpression="OrderDate" />
                <asp:TemplateField HeaderText="CustomerID" SortExpression="CustomerID">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("CustomerID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <HeaderTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                            DataSourceID="SqlDataSource2" DataTextField="CompanyName" 
                            DataValueField="CustomerID">
                        </asp:DropDownList>
                        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
                            SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers]">
                        </asp:SqlDataSource>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("CustomerID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT [OrderID], [OrderDate], [CustomerID] FROM [Orders] WHERE ([CustomerID] = @CustomerID)">
            <SelectParameters>
                <asp:SessionParameter Name="CustomerID" SessionField="custid" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>
 

 

Code behind

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class ddlInGvHdr : System.Web.UI.Page
{
    DropDownList ddl;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["custid"] = "ALFKI";

        }
        else
        {
            ddl = (DropDownList)GridView1.HeaderRow.FindControl("DropDownList1"); 
            int i = ddl.SelectedIndex;
            if (i < 0 )
            {
                i=0;
                ddl.SelectedIndex = i;
            }

            Session["custid"] = ddl.SelectedValue;

        }

 
    }
  
}
 
I am not anti social, am just not user friendly
0
aamador
4/15/2008 11:03:43 AM

Thanks Guys ... It hink that solves the  problem.... Great... Keep going...


Red in Heart...
0
kiransdusk
4/21/2008 11:26:33 AM
Reply:

Similar Artilces:

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

using controlS to control a gridview
This question is probably posted but i did not bother to look at 10,000+ posts so i am sorry. I've seen the tutorial where he builds a dataset and uses a gridview and controls the gridview with the drop down list box. What i am trying to do is to control the gridview with 2 drop down list boxes. First select the Country you life in and then the State. I've looked at the grid view properties but couldn't find where i can add another parameter. Is it possible to control a grid view (or any form of displaying the information) with more than 1 controller? Thank You.  ...

What control do I use (or do I not use a control at all...)
Okay so here's the story so far. I am making a blog to be incorporated into a website. Well, I'm doing the blog from scratch. I'm using MySQL to source all the data via MySQL .NET/Connector. Now while developing the script, I used DataReader and Response.Write to show the blog topics. Now that it has come to put it under a design (I'm using a Master Page for that), I've hit the problem. Response.Write shows all the topics before any of the design elements could be incorporated. So that (more or less) is out of question. So next I looked into the data presentation controls. I looked up GridVi...

Freezing the column header row of a GridView control using CSS
I am struggling getting my Gridview in ASP.NET 2.0 Visual Studio 2005 to freeze the column header row and provide for scrolling of the remaining data rows. My specific problems right now are: (IE7) column headers freeze as intended and do not scroll  BUT scroll bar is not initially visible to user (requires scrolling with mouse wheel before scroll bars are exposed in gridview) (Firefox 2.0.10) column header row just scrolls out of sight like any other row  Here is part of my default.css in my default App_Theme folder: /* So the overflow scrolls */ div.coveragesContainer ...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Issue with getting values from child controls in a gridview, to use for the update using a SQLDataSource control
Hi all, I have a gridview bound with a SQLDataSource. I am using the Update feature of the SQLDataSource to update a SQL Server database with values entered into the gridview. However I am not getting it to work. I believe this is due to the controls that contain the user entries are not the gridview itself, but rather child controls within the gridview. I have been using the names of the actual controls but nothing happens. Upon submit, the screen returns blank, and the database is not updated. Here is some code:   <asp:GridView ID="GridEditSettlement" runat="server" AutoGenerate...

can we use gridview control with in repeater control
HI all,     Can i use gridview control with in repeater control, Any one knows plzzz... let me know. Is there any possibility for this?      It is urgent required... plzzzz... help me..  ------------- ThanksBalu Chandaka  yes you can nest a gridview control in the repeater controlJeev~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~If you get the answer to your question, please mark it as the answer. Hi Jeev,    Thank you for your reply.But how can do that give some idea or If you have any example code snippet then plzzz... let m...

How can I control the width of a column in the GridView control.
From searching I see mention of ways to do it before the controls are bound. Is there a simpler way? If not, could someone explain to me how I need to go about it?Here is the code: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"        DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."        OnSelectedIndexChanged="GridView1_SelectedIndexChanged">   &nbs...

Problem Using Slider Control in Gridview Control
Hello to all,   it is urgent.   i want to use slider control in a Gridview Template Column, if someone can help me how i can get this functionality?...

gridview control in another gridview control
Hi ia have a questioni have a gridview control which contains button in one colum.now my question is i need another gridview to be placed in the  first gidviewso that when i click on the button the second gridview will populate the data.now my question is how to place second gridview in first gridview.or is there any approach like first grid itself acting as parent and child.please help me  Hi sirikalavalapalli:                I think nested gridview will help you. Check this link and it will show you how...

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Issue with Template Columns & Bound Columns while updating the data in a GridView control
Hi, I'm facing the below problem (In general, ObjectDataSource is driving me crazy ! ) I have a simple gridview control and an ObjectDataSource is used to populate the grid. I'm trying to update a row and the corresponding tableadapter methods & stored procedure are in place. Each row has 2 checkboxes and 2 textboxes in edit mode. When the change the values in edit mode and hit update, the checkbox values changed are saved into the Database properly, whereas the textbox values are not being saved. If the textbox column is a bound column then the column is updated with <NULL> in...

using a .Net Control as activex control in VCL form
Hi , We have a to integrate a simple .Dot CSharp control which needs to be integrated into bcb2010 VCL form as a control. We will place the control on the VCL form along with other VCL controls's. I tried creating a simple .net control using a lable+textbox and trying to use in vcl form. I have made the assembly comvisible and generated tlb files and .bpl for the component. I could see the component in "ActiveX" tool palette. But when I drag the object onto VCL form and try to access its attributes I run into problems. I am not able to see .net control on the vcl at runti...

ajax.net controls used in custom composite controls?
I am creating an events calendar that uses the accordion to display the events for the selected day.  The problem I'm having is that the accordion is not responding to any client side events.  I would really like to find a good "How to" article for building composite controls that incorporate an ajax.net control like the accordion....

Web resources about - Using Bound Controls in Gridview Column Header - asp.net.presentation-controls

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

WA bushfires: Firefighters work through the night to control several blazes
Firefighters in Western Australia are working through the night to control and contain several blazes.

Alaska Marijuana Control Board makes major residency requirement changes at last minute - Alaska Dispatch ...
Assistant Attorney General Harriet Milks called the shift in Alaska marijuana business residency requirements a “sea change” that could “upend ...

69 percent of users would bypass security controls to win a big deal
When faced with the chance of clinching a major deal people are willing to throw security controls out of the window. This is according to research ...


MAZEL TOV, HILLARY: Clinton Wins Gun Control Award From Major Anti-Bill of Rights Group
... restrictions on the freedoms protected by the Constitution is not a sign of character, but a clear indication of blind adherence to a gun control ...

Obama to Veto Bill Requiring Background Checks On Syrian Refugees; France Demands Tighter Controls, EU ...
... does not call for ending the refugee program or require religious screenings, as some Republicans have demanded . France Demands Tighter Controls, ...

DC Police Chief, “If you’re in a position to try and take the gunman down…it’s the best option for saving ...
... in a position to try and take the gunman down…it’s the best option for saving lives before police can get there,” Yet DC Supports Gun Control ...

USB Volume Control
... computer speakers, they often have a volume knob you can mount somewhere on your desk so you aren’t dependent on the onboard volume control. ...

5 Questions: Punjab’s rightful control over Chandigarh, says Prem Singh Chandumajra
Shiromani Akali Dal (Badal) MP Prem Singh Chandumajra on why he raised the issue of Punjab’s claim over Chandigarh in LS.

Resources last updated: 11/24/2015 10:31:45 AM