connectseekers.com

Monday, 9 February 2015

Detect Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using C#.Net

2/09/2015 - By Pranav Singh 0

This article will show you how you can detect header control and assign value of gridview rowdatabound in asp.netin asp.net using c#.net. In this we will display to bind value to header control at run time in asp.net using c#.net.


So for this article first we will create a new asp.net application and add the below code in the page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!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>Detect Header Control and Assign Value of Gridview RowDataBound In Asp.Net Using C#.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <table width="100%" cellpadding="4" cellspacing="4">
        <tr>
            <td align="left">
                <asp:GridView ID="GridView1" runat="server" Width="100%"
                    AutoGenerateColumns="False"
                    ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                  <asp:Label ID="headerName" runat="server" Text=''></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterStyle Font-Bold="True" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                  <asp:Label ID="headerAddress" runat="server" Text=''></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblNamAddress" runat="server" Text='<%# Eval("Address") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                  <asp:Label ID="headerUserType" runat="server" Text=''></asp:Label>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblUserType" runat="server" Text='<%# Eval("UserType") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

In above code I have use itemtemplate to bind the gridview fields and used label control to display the value using Eval. In HeaderTemplate I have added label control . I will detect this control and assign header value to this label control.

Now check the code to bind and display no of pages.

using System;
using System.Data.SqlClient;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;
namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDistinctValue();
            }
        }
        private void GetDistinctValue()
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ToString());
            try
            {
                DataTable objdt = new DataTable();
                string query = "select * from UserDetail;";
                SqlDataAdapter da = new SqlDataAdapter(query, con);
                con.Open();
                da.Fill(objdt);
                con.Close();
                if (objdt.Rows.Count > 0)
                {
                    GridView1.DataSource = objdt;
                    GridView1.DataBind();
                }
            }
            catch
            {
                con.Close();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                Label headerName = (Label)e.Row.FindControl("headerName");
                Label headerAddress = (Label)e.Row.FindControl("headerAddress");
                Label headerUserType = (Label)e.Row.FindControl("headerUserType");

                headerName.Text = "NAME";
                headerAddress.Text = "ADDRESS";
                headerUserType.Text = "USER TYPE";
            }
        }
    }
}

On above code I have used a function to bind the data. Now I have I have added the. Now check the below code to detect header control and assign value at run time..

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                Label headerName = (Label)e.Row.FindControl("headerName");
                Label headerAddress = (Label)e.Row.FindControl("headerAddress");
                Label headerUserType = (Label)e.Row.FindControl("headerUserType");

                headerName.Text = "NAME";
                headerAddress.Text = "ADDRESS";
                headerUserType.Text = "USER TYPE";
            }
        }

In above code I have validated when the header row is appearing.  Now check the normal output without merging header.

Now we have done run the application to check the output.


DOWNLOAD 

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top