Hi tex,
I have created a sample please take its refrence and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:GridView ID="gvOrders" runat="server" EmptyDataText="No visits to display" PageSize="5"
ShowHeaderWhenEmpty="true" OnPageIndexChanging="gvOrders_PageIndexChanging" AutoGenerateColumns="False"
AllowPaging="true" OnRowCommand="gvOrders_RowCommand">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="Order ID"></asp:BoundField>
<asp:BoundField DataField="OrderDate" HeaderText="Date" DataFormatString="{0:dd/MM/yyyy}"></asp:BoundField>
<asp:BoundField DataField="CustomerID" HeaderText="Customer Name"></asp:BoundField>
<asp:TemplateField HeaderText="Ready" HeaderStyle-Width="5%" ItemStyle-Width="5%"
ControlStyle-BackColor="Transparent" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:CheckBox ID="chReady" runat="server" Visible="true" OnClick="return false;">
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnEdit" Text="Edit" runat="server" CommandName="Edit_Pat" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnPrintPDF" Text="Print" runat="server" CommandName="pdf" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings PageButtonCount="10" />
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT OrderID,CustomerID,ShipName,OrderDate FROM Orders", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvOrders.DataSource = dt;
this.gvOrders.DataBind();
}
}
}
}
protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gvOrders.PageIndex = e.NewPageIndex;
this.BindGrid();
}
protected void gvOrders_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit_Pat" || e.CommandName == "pdf")
{
int index = Convert.ToInt32(e.CommandArgument);
gvOrders.AllowPaging = false;
this.BindGrid();
GridViewRow gvRow = gvOrders.Rows[index];
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('" + gvRow.Cells[0].Text + "')", true);
gvOrders.AllowPaging = true;
this.BindGrid();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT OrderID,CustomerID,ShipName,OrderDate FROM Orders", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvOrders.DataSource = dt
Me.gvOrders.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub gvOrders_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
Me.gvOrders.PageIndex = e.NewPageIndex
Me.BindGrid()
End Sub
Protected Sub gvOrders_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "Edit_Pat" OrElse e.CommandName = "pdf" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
gvOrders.AllowPaging = False
Me.BindGrid()
Dim gvRow As GridViewRow = gvOrders.Rows(index)
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('" & gvRow.Cells(0).Text & "')", True)
gvOrders.AllowPaging = True
Me.BindGrid()
End If
End Sub
Screenshot