connectseekers.com

Friday, 22 July 2016

Retrieve Boundfield Column Values On Button Click Of Gridview In Asp.Net

7/22/2016 - By Pranav Singh 0

This article will show you how you can Retrieve Boundfield Column Values on button click Of Gridview In Asp.Net using c#.net.

So for this article first we will create a new asp.net application and add a button, gridview and label controls. After adding all the controls your page code will look as shown below.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Retrieve Boundfield Column Values Of Gridview In Asp.Net</title>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Click To Get Colum Value" OnClick="Button1_Click" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CssClass="tablesorter" HeaderStyle-BackColor="Yellow">
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" />
                    <asp:BoundField DataField="CountryName" HeaderText="Country Name" />
                    <asp:BoundField DataField="Population" HeaderText="Population" />
                    <asp:BoundField DataField="Code" HeaderText="Code" />
                    <asp:BoundField DataField="Status" HeaderText="Status" />
                </Columns>
            </asp:GridView>
            Colum 1 Values:
            <asp:Label ID="Label1" runat="server" Text="Label" Style="color: #FF0000"></asp:Label><br />
            Colum 2 Values:
            <asp:Label ID="Label2" runat="server" Text="Label" Style="color: #FF0000"></asp:Label><br />
            Colum 3 Values:
            <asp:Label ID="Label3" runat="server" Text="Label" Style="color: #FF0000"></asp:Label><br />
            Colum 4 Values:
            <asp:Label ID="Label4" runat="server" Text="Label" Style="color: #FF0000"></asp:Label>
        </div>
    </form>
</body>
</html>

After this we will check the code. For this first we will create the click event of the button. Now we will add code to get the boundfiels value. In this I have identified the value of all the columns.

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = GetCountryData();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        private DataTable GetCountryData()
        {
            DataTable dt = new DataTable();
            dt.Clear();
            dt.Columns.Add("Id");
            dt.Columns.Add("CountryName");
            dt.Columns.Add("Population");
            dt.Columns.Add("Code");
            dt.Columns.Add("Status");

            DataRow dataRow1 = dt.NewRow();
            dataRow1["Id"] = 1;
            dataRow1["CountryName"] = "India";
            dataRow1["Population"] = "125 Cr";
            dataRow1["Code"] = "IN";
            dataRow1["Status"] = "1";
            dt.Rows.Add(dataRow1);

            DataRow dataRow2 = dt.NewRow();
            dataRow2["Id"] = 2;
            dataRow2["CountryName"] = "Pakistan";
            dataRow2["Population"] = "50 Cr";
            dataRow2["Code"] = "PK";
            dataRow2["Status"] = "0";
            dt.Rows.Add(dataRow2);

            DataRow dataRow3 = dt.NewRow();
            dataRow3["Id"] = 3;
            dataRow3["CountryName"] = "United States";
            dataRow3["Population"] = "25 Cr";
            dataRow3["Code"] = "US";
            dataRow3["Status"] = "1";
            dt.Rows.Add(dataRow3);

            DataRow dataRow4 = dt.NewRow();
            dataRow4["Id"] = 4;
            dataRow4["CountryName"] = "Japan";
            dataRow4["Population"] = "20 Cr";
            dataRow4["Code"] = "JP";
            dataRow4["Status"] = "0";
            dt.Rows.Add(dataRow4);

            return dt;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string colum1 = "";
            string colum2 = "";
            string colum3 = "";
            string colum4 = "";
            foreach (GridViewRow grrows in GridView1.Rows)
            {
                colum1 = colum1 + GridView1.Rows[grrows.RowIndex].Cells[0].Text + ",";
                colum2 = colum2 + GridView1.Rows[grrows.RowIndex].Cells[1].Text + ",";
                colum3 = colum3 + GridView1.Rows[grrows.RowIndex].Cells[2].Text + ",";
                colum4 = colum4 + GridView1.Rows[grrows.RowIndex].Cells[3].Text + ",";
            }
            Label1.Text = colum1;
            Label2.Text = colum2;
            Label3.Text = colum3;
            Label4.Text = colum4;
        }
    }
}

In  above code I have bind the grid view on page load and then on button click I have detected value for each and every column one by one and then assign it to the string variable, and shown on the label control.

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

  


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