Wednesday, 4 December 2013

How to read Grid View Cell value in Asp.Net with Template Field and without Template Field

How to read Grid View Cell value in Asp.Net with Template Field and without Template Field

//Execute this query in Sql Server………………………..

create database GridViewProblem
use GridViewProblem
create table Student(RollNo int primary key ,Name nvarchar(50),Course nvarchar(50),Dob DateTime)
insert into Student values(1,'Ashish Upadhyay','Mca','09/09/1987')
select * from Student

How to add Template Field in GridView……………………………….


How drag LinkButton in selected  Template Field………………………………….



Code For Default.aspx …………………………………
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<table class="style1">
<tr>
<td><center><b>How to read GridView Cell value in Asp.Net </b></center> </td>
</tr>
<tr>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
<center>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" onselectedindexchanged="GridView1_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Read">
<ItemTemplate>
<asp:LinkButton ID="lbRead" runat="server" CommandName="Select">Read</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</center>
</td>
</tr>
<tr>
<td>
<center><asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></center>
</td>
</tr>
<tr>
<td>
<center>
<b>How to read Grid View Cell value in Asp.Net with Template Field</b>
</center>
</td>
</tr>
<tr>
<td><center>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
onselectedindexchanged="GridView2_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="RollNo">
<ItemTemplate>
<asp:Label ID="lblRollNo" runat="server" Text='<%# Eval("RollNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course">
<ItemTemplate>
<asp:Label ID="lblCourse" runat="server" Text='<%# Eval("Course") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Dob">
<ItemTemplate>
<asp:Label ID="lbldob" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbRead" runat="server" CommandName="Select">Read</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</center>
</td>
</tr>
<tr>

<td><center><asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></center></td>
</tr>
</table>

</div>
</form>
</body>
</html>

Code For Default.aspx.cs …………………………………
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
bindGridView();
}
//create method for bind GridView1 with table......
private void bindGridView()
{
SqlConnection con=new SqlConnection(@"Data Source=KUSH-PC\KUSH;Initial Catalog=GridViewProblem;User ID=sa;Password=tiwari");
SqlCommand cmd = new SqlCommand("Select * from Student", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
//bind GridView1 with table......
GridView1.DataSource = ds;
GridView1.DataBind();
//bind GridView2 with Template field
GridView2.DataSource = ds;
GridView2.DataBind();

}

//code for read GridView1 Cell ................................
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

Label1.Text ="Name"+"  "+ GridView1.SelectedRow.Cells[2].Text;
//code for go to Next Page with Query String......
Response.Redirect("NextPage.aspx?RollNo=" + GridView1.SelectedRow.Cells[1].Text);

}
//code for read GridView2 Cell with Template Filed because GridView2 bind with table with Template Field ...............................
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
Label2.Text = "Name"+(GridView2.SelectedRow.FindControl("lblName") as Label).Text;
//code for go to Next Page with Query String......
string rollno = (GridView2.SelectedRow.FindControl("lblRollNo") as Label).Text;
Response.Redirect("NextPage.aspx?RollNo=" + rollno);
}
}


5 comments:

  1. Can u Read this value.......?



    <%# Eval("RollNo") %>

    ReplyDelete
  2. Very informative. i like blog

    ReplyDelete
  3. How to bold or apply color for particular cell in Grid View
    protected void GridView2_DataBound(object sender, EventArgs e)
    {
    foreach (GridViewRow grv in GridView2.Rows)
    {

    grv.Cells[1].Style["font-weight"] = "bold";
    grv.Cells[1].Style["Color"] = "Red";
    }
    }

    ReplyDelete