Running Total Gridview - c#

any idea how to get a running total (cumulative total ?) for each row in a gridview? Also the gridview shows the records for an apt/villa choosed by dropdown list. Records are ordered by Trans_ID.
This is the code:
<table cellspacing="1" class="auto-style1">
<tr>
<td class="auto-style22">
<strong>
<asp:Label ID="Label1" runat="server" Text="Apt/Villa"
CssClass="auto-style10" BackColor="White" Font-Bold="True" Font-Names="Arial
Narrow"></asp:Label>
</strong>
</td>
<td class="auto-style21">
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="RecordPaymentAptDropDown" DataTextField="Apt"
DataValueField="Apt_ID" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="auto-
style19" BackColor="#CC99FF">
</asp:DropDownList>
<asp:SqlDataSource ID="RecordPaymentAptDropDown" runat="server"
ConnectionString="<%$ ConnectionStrings:db71ConnectionString2 %>"
SelectCommand="SELECT [Apt], [Apt_ID] FROM [Apartments]">
</asp:SqlDataSource>
</td>
<td class="auto-style20"> </td>
</tr>
<tr>
<td class="auto-style22">
<strong>
<asp:Label ID="Label2" runat="server" Text="Date"
CssClass="auto-style10" BackColor="White" Font-Bold="True" Font-Names="Arial
Narrow"></asp:Label>
</strong>
</td>
<td class="auto-style21">
<asp:TextBox ID="TextBox4" runat="server" TextMode="Date"
ToolTip="Enter date in fromat: dd/mm/yyyy" CssClass="auto-style6"
BackColor="#CC99FF"></asp:TextBox>
</td>
<td class="auto-style20"> </td>
</tr>
<tr>
<td class="auto-style22">
<strong>
<asp:Label ID="Label3" runat="server" Text="Description"
CssClass="auto-style10" BackColor="White" Font-Bold="True" Font-Names="Arial
Narrow"></asp:Label>
</strong>
</td>
<td class="auto-style21">
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="RecordPaymentDescriptionDropDown" DataTextField="Description"
DataValueField="Desc_ID" CssClass="auto-style19" BackColor="#CC99FF">
</asp:DropDownList>
<asp:SqlDataSource ID="RecordPaymentDescriptionDropDown"
runat="server" ConnectionString="<%$ ConnectionStrings:db71ConnectionString2
%>" SelectCommand="SELECT [Description], [Desc_ID] FROM [Descr]">
</asp:SqlDataSource>
</td>
<td class="auto-style20"> </td>
</tr>
<tr>
<td class="auto-style24">
Income/Expenses</td>
<td class="auto-style21">
<asp:DropDownList ID="DropDownList3" runat="server"
CssClass="auto-style19" BackColor="#CC99FF">
<asp:ListItem>Please choose</asp:ListItem>
<asp:ListItem Value="1">Income</asp:ListItem>
<asp:ListItem Value="2">Expenses</asp:ListItem>
</asp:DropDownList>
</td>
<td class="auto-style20"> </td>
</tr>
<tr>
<td class="auto-style23">
<asp:Label ID="Label8" runat="server" BackColor="White"
CssClass="auto-style10" Font-Bold="True" Font-Names="Arial Narrow"
Text="Amount"></asp:Label>
</td>
<td class="auto-style11">
<asp:TextBox ID="TextBox1" runat="server" Width="150px"
CssClass="auto-style6" BackColor="#CC99FF"></asp:TextBox>
</td>
<td class="auto-style12"></td>
</tr>
<tr>
<td class="auto-style22">
<strong>
<asp:Label ID="Label6" runat="server" Text="Comment"
CssClass="auto-style10" BackColor="White" Font-Names="Arial Narrow">
</asp:Label>
</strong>
</td>
<td class="auto-style21">
<asp:TextBox ID="TextBox3" runat="server" Height="58px"
TextMode="MultiLine" Width="344px" CssClass="auto-style6"
BackColor="#CC99FF"></asp:TextBox>
</td>
<td class="auto-style20"> </td>
</tr>
<tr>
<td class="auto-style22">
</td>
<td class="auto-style21">
</td>
<td class="auto-style20">
</td>
</tr>
<tr>
<td class="auto-style22"> </td>
<td class="auto-style21">
</td>
<td class="auto-style20"> </td>
</tr>
<tr>
<td class="auto-style22">
<asp:Button ID="Button1" runat="server" Text="Save"
OnClick="Button1_Click" CssClass="auto-style6" BackColor="#000099" Font-
Bold="True" Font-Size="Large" ForeColor="Lime" Height="33px" Width="98px" />
</td>
<td class="auto-style21">
</td>
<td class="auto-style20"> </td>
</tr>
<tr>
<td colspan="3" class="auto-style9">
<asp:SqlDataSource ID="SqlDataSourceRecordPayment1"
runat="server" ConnectionString="<%$ ConnectionStrings:db71ConnectionString2
%>" DeleteCommand="DELETE FROM Trans WHERE Trans_ID = #Trans_ID"
SelectCommand="SELECT Apt, Date, Income, Expenses, Description, Trans_ID,
Memo, OwnerName FROM TransApt WHERE (Apt_ID = #Apt_ID)">
<DeleteParameters>
<asp:Parameter Name="Trans_ID" />
</DeleteParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1"
Name="Apt_ID" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="Trans_ID"
DataSourceID="SqlDataSourceRecordPayment1" GridLines="Horizontal"
CssClass="auto-style6" Width="100%" OnRowDataBound="GridView1_RowDataBound"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" ShowFooter="True">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundField DataField="Trans_ID"
HeaderText="Trans_ID" SortExpression="Trans_ID" ReadOnly="True" />
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="Apt" HeaderText="Apt"
SortExpression="Apt" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="OwnerName"
HeaderText="OwnerName" SortExpression="OwnerName" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Date" HeaderText="Date"
SortExpression="Date" DataFormatString="{0:dd/MM/yyyy}" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Description"
HeaderText="Description" SortExpression="Description" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Income" HeaderText="Income"
SortExpression="Income" DataFormatString="{0:n}" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Expenses"
HeaderText="Expenses" SortExpression="Expenses" DataFormatString="{0:n}" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Memo" HeaderText="Comment"
SortExpression="Memo" >
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True"
ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C"
HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
<br class="auto-style6" />
</td>
</tr>
</table>
This is code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace HHCondominium
{
public partial class RecordPayment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] == null)
Response.Redirect("Login.aspx", true);
}
protected void DropDownList2_SelectedIndexChanged(object sender,
EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection vid = new SqlConnection("Data Source = s1.XXXX.com;
Initial Catalog = db71; Persist Security Info = True; User ID = XXXX;
Password = XXXXX");
if (DropDownList3.SelectedValue == "1")
{
SqlCommand xp = new SqlCommand("Insert into Trans(Apt_ID, Date,
Description_ID, Income, Memo) Values (#Apt_ID, #Date, #Description_ID,
#Income, #Memo)", vid);
xp.Parameters.AddWithValue("#Apt_ID", DropDownList1.Text);
xp.Parameters.AddWithValue("#Date", TextBox4.Text);
xp.Parameters.AddWithValue("#Description_ID",
DropDownList2.Text);
xp.Parameters.AddWithValue("#Income", TextBox1.Text);
xp.Parameters.AddWithValue("#Memo", TextBox3.Text);
vid.Open();
xp.ExecuteNonQuery();
vid.Close();
}
else if (DropDownList3.SelectedValue == "2")
{
SqlCommand xp = new SqlCommand("Insert into Trans(Apt_ID, Date,
Description_ID, Expenses, Memo) Values (#Apt_ID, #Date, #Description_ID,
#Expenses, #Memo)", vid);
xp.Parameters.AddWithValue("#Apt_ID", DropDownList1.Text);
xp.Parameters.AddWithValue("#Date", TextBox4.Text);
xp.Parameters.AddWithValue("#Description_ID",
DropDownList2.Text);
xp.Parameters.AddWithValue("#Expenses", TextBox1.Text);
xp.Parameters.AddWithValue("#Memo", TextBox3.Text);
vid.Open();
xp.ExecuteNonQuery();
vid.Close();
}
{
string msgstring = "Your data has been saved";
string content = "window.onload=function(){ alert('";
content += msgstring;
content += "');";
content += "window.location='";
content += Request.Url.AbsoluteUri;
content += "';}";
ClientScript.RegisterStartupScript(this.GetType(),
"SuccessMessage", content, true);
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{ }
protected void GridView1_SelectedIndexChanged(object sender, EventArgs
e)
{
}
Double totalIncome = 0;
Double totalExpenses = 0;
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
totalIncome += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem,
"Income"));
totalExpenses +=
Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Expenses"));
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].Text = "Grand Total";
e.Row.Cells[5].Font.Bold = true;
e.Row.Cells[6].Text = totalIncome.ToString();
e.Row.Cells[6].Font.Bold = true;
e.Row.Cells[7].Text = totalExpenses.ToString();
e.Row.Cells[7].Font.Bold = true;
e.Row.Cells[8].Text = (totalIncome-totalExpenses).ToString();
e.Row.Cells[8].Font.Bold = true;
}
}
}
}
What I'm trying to achieve is this:
enter image description here
I've tried many things that I've found on a internet but no luck.
Thanks guys,

You can do this in the RowDataBound event. First, add the event to the GridView and add a Label to display the total value.
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Balance">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then in code behind
int total = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the dataitem back to a row
DataRowView row = e.Row.DataItem as DataRowView;
//add the row value to the total
total += Convert.ToInt32(row["Balance"]);
//use findcontrol to locate the label in the row
Label lbl = e.Row.FindControl("Label1") as Label;
//display the totals in the row
lbl.Text = string.Format("{0:N0}", total);
}
}

Related

Add generated attendance details to Database asp.net c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class UserAttendance : System.Web.UI.Page
{
SqlConnection vid = new SqlConnection("Data Source=DESKTOP-430E2O0\\SQLEXPRESS;Initial Catalog=mca;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Calendar1.Visible = true;
TextBox1.Visible = true;
String str = "select * from Student where (Course like '%' + #search + '%')";
String str1 = "select * from Student where (Semester like '%' + #search + '%')";
SqlCommand xp = new SqlCommand(str, vid);
SqlCommand xp1 = new SqlCommand(str1, vid);
xp.Parameters.Add("#search", SqlDbType.NVarChar).Value = DropDownList1.SelectedItem.Text;
xp1.Parameters.Add("#search", SqlDbType.NVarChar).Value = DropDownList2.SelectedItem.Text;
vid.Open();
xp.ExecuteNonQuery();
xp1.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = xp;
da.SelectCommand = xp1;
DataSet ds = new DataSet();
da.Fill(ds, "Course");
da.Fill(ds, "Semester");
GridView1.DataSource = ds;
GridView1.DataBind();
vid.Close();
Button2.Visible = true;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox1.Text = Calendar1.SelectedDate.ToShortDateString();
Calendar1.Visible = false;
}
protected void Button2_Click(object sender, EventArgs e)
{
//I wish to use this button to save details to the database
}
}
<%# Page Title="" Language="C#" MasterPageFile="~/User.master" AutoEventWireup="true" CodeFile="UserAttendance.aspx.cs" Inherits="UserAttendance" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 179px;
}
.auto-style3 {
width: 6px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<form id="form1" runat="server">
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:studentConnectionString %>" SelectCommand="SELECT [Course] FROM [Course]"></asp:SqlDataSource>
</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Course" DataValueField="Course">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Semester" DataValueField="Semester">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource3" DataTextField="Subject" DataValueField="Subject">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="OK" Width="49px" />
</td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2">
</td>
<td>
<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged" Visible="False"></asp:Calendar>
</td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2">
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Visible="False"></asp:TextBox>
</td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2">
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:studentConnectionString %>" SelectCommand="SELECT [Semester] FROM [Semester]"></asp:SqlDataSource>
</td>
<td>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server">
<asp:ListItem>Present</asp:ListItem>
<asp:ListItem>Absent</asp:ListItem>
<asp:ListItem>Holiday</asp:ListItem>
</asp:DropDownList>
</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>
</td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2">
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:studentConnectionString %>" SelectCommand="SELECT [Subject] FROM [Subject]"></asp:SqlDataSource>
</td>
<td style="text-align: justify">
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Save" Visible="False" Width="58px" />
</td>
<td class="auto-style3"> </td>
</tr>
</table>
</form>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
</asp:Content>
I have a textbox which contains date(out side the Gridview)
I have a dropdownlist in the gridview as a templatefield(Absent/Present)
I have a button called "save" outside of the grid view
When I press the save button the details should go to database and should be saved in a table Attendance.
Columns of Attendance Table
1.ID
2.Name
3.Course
4.Semester
5.Date
6.Status(Absent/Present)

Display GridView Edited row data in TextBox

This is my screenshot-output
When i click edit button, then then the edited row data need to display in the above text boxes(attached screenshot).
Here is the aspx file:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<center><div><h4>Student Details</h4></div></center>
<table style="width: 100%;">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="Textusername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Class"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="Textclass" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Section"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="Textsection" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Address"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="Textaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnsub" runat="server" Text="Submit" OnClick="btnsub_Click" OnClientClick="return register();" />
<asp:Button ID="btnrst" runat="server" Text="Reset" OnClick="btnrst_Click" />
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Column1,Column2,Column3,Column4" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
<asp:BoundField DataField="Section" HeaderText="Section"
SortExpression="Section" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:ButtonField ButtonType="Button" CommandName="EditRow" HeaderText="Edit"
ShowHeader="True" Text="Edit" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete"
ShowHeader="True" Text="Delete" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="StoredProcedure2"
SelectCommandType="StoredProcedure">
<DeleteParameters>
<asp:Parameter Name="ID" />
<asp:Parameter Name="Name" />
<asp:Parameter Name="Class" />
<asp:Parameter Name="Section" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ID" />
<asp:Parameter Name="Name" />
<asp:Parameter Name="Class" />
<asp:Parameter Name="Section" />
</UpdateParameters>
</asp:SqlDataSource>
</asp:Content>
Can anybody help me, how to achieve this one?
Any help would be highly appreciated,
Thanks.,
To achieve the bind value in Form you need to set the TextBox value as GridView rows value within RowCommandEvent:
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName =="EditRow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
string id = gr.Cells[0].Text;
txtname.Text =gr.Cells[1].Text;
txtclass.Text=gr.Cells[2].Text;
txtsection.Text =gr.Cells[3].Text;
txtaddress.Text=gr.Cells[4].Text;
}
}
GridView :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Column1,Column2,Column3,Column4" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
<asp:BoundField DataField="Section" HeaderText="Section"
SortExpression="Section" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button runat="server" ID="btnedit" Text="Edit" CommandName="EditRow"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete"
ShowHeader="True" Text="Delete" />
</Columns>
</asp:GridView>
Or Fetch the row as :
protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)
{
if(e.CommandName =="EditRow")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow gr = GridView1.Rows[index];
string id = gr.Cells[0].Text;
txtname.Text =gr.Cells[1].Text;
txtclass.Text=gr.Cells[2].Text;
txtsection.Text =gr.Cells[3].Text;
txtaddress.Text=gr.Cells[4].Text;
}
}
For more Reference :
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield.commandname(v=vs.110).aspx
First, add your GridView and Display Text boxes to Update Panel
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="updt" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="False" >
<ContentTemplate>
<center><div><h4>Student Details</h4></div></center>
<table style="width: 100%;">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="Textusername" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Class"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="Textclass" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Section"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="Textsection" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="Address"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="Textaddress" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnsub" runat="server" Text="Submit" OnClick="btnsub_Click" OnClientClick="return register();" />
<asp:Button ID="btnrst" runat="server" Text="Reset" OnClick="btnrst_Click" />
</td>
<td>
</td>
<td>
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Column1,Column2,Column3,Column4" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
<asp:BoundField DataField="Section" HeaderText="Section"
SortExpression="Section" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:ButtonField ButtonType="Button" CommandName="EditRow" HeaderText="Edit"
ShowHeader="True" Text="Edit" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Delete"
ShowHeader="True" Text="Delete" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="StoredProcedure2"
SelectCommandType="StoredProcedure">
<DeleteParameters>
<asp:Parameter Name="ID" />
<asp:Parameter Name="Name" />
<asp:Parameter Name="Class" />
<asp:Parameter Name="Section" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ID" />
<asp:Parameter Name="Name" />
<asp:Parameter Name="Class" />
<asp:Parameter Name="Section" />
</UpdateParameters>
</asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridView1" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>
This is how to get cell value of edit row on RowCommandEvent
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRow")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
string _name = row.Cells[1].Text;
string _class = row.Cells[2].Text;
string _section = row.Cells[3].Text;
string _Address = row.Cells[4].Text;
//Add this value to your text box here //
}
}

Printing an aspx file in Asp.net

I have used the following code to print an HTML DIV Contents & it works fine, but when I am using an Ajax Control in an Aspx page then it gives me an error message, i.e:
"Extender control 'CalendarExtender2' is not a registered extender
control. Extender controls must be registered using
RegisterExtenderControl() before calling RegisterScriptDescriptors().
Parameter name: extenderControl"
Here is my C# code
protected void BtnPrint_Click(object sender, EventArgs e)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
Page pg = new Page();
pg.EnableEventValidation = false;
HtmlForm frm = new HtmlForm();
pg.EnableEventValidation = false;
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(divContent);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
Here is my Aspx code
<%# Page Title="" Language="C#" MasterPageFile="~/Masters/TSAMaster.master" AutoEventWireup="true"
EnableEventValidation="false" Theme="skinFiles" CodeFile="AdminHome.aspx.cs"
Inherits="Masters_Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<script type="text/javascript">
function ValidateDate() {
isValidDate = ValidateDate();
if (!isValidDate) {
return false;
}
else {
return true;
}
}
function ValidateDate() {
var Fdate = document.getElementById('ctl00_ContentPlaceHolder1_txtFDate').value;
var Tdate = document.getElementById('ctl00_ContentPlaceHolder1_txtTDate').value;
var todaysDate = formatDate(new Date(), 'dd/MM/yyyy');
var isValidDate = true;
var flag;
if (trim(Fdate) != 0 || trim(Tdate) != 0) {
if (!isDate(Fdate, 'dd/MM/yyyy')) {
alert("Please select valid From Date enter into (dd/MM/yyyy) format");
document.getElementById('ctl00_ContentPlaceHolder1_txtFDate').focus();
return false;
}
if (!isDate(Tdate, 'dd/MM/yyyy')) {
alert("Please select valid To Date enter into (dd/MM/yyyy) format ");
document.getElementById('ctl00_ContentPlaceHolder1_txtFDate').focus();
return false;
}
if (((compareDates(Fdate, 'dd/MM/yyyy', Tdate, 'dd/MM/yyyy')) == 1)) {
alert("To date cannot be less than From Date ");
return false;
}
return true;
}
}
function trim(str) {
return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
}
</script>
<ajaxToolkit:ToolkitScriptManager runat="Server" EnableScriptGlobalization="true"
EnableScriptLocalization="true" ID="ScriptManager1" />
<center>
<div class="divContainPage" id="divContent" runat="server">
<table id="tblHead" runat="server">
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="DueDate Report" Font-Bold="True" Font-Names="Verdana"
Font-Size="Large" ForeColor="#C80000"></asp:Label>
<br />
<br />
<table id="tbl1" runat="server" style="width: 823px">
<tr>
<td>
Select Payment Mode:
</td>
<td>
<asp:DropDownList ID="ddlPayMode" runat="server" CssClass="DropDown" Width="85px">
<asp:ListItem>--Select--</asp:ListItem>
<asp:ListItem>Cash</asp:ListItem>
<asp:ListItem>Cheque</asp:ListItem>
</asp:DropDownList>
</td>
<td>
From Date:
</td>
<td>
<asp:TextBox ID="txtFDate" runat="server" Width="110px"></asp:TextBox>
<asp:ImageButton ID="imgCalendar" runat="server" ImageUrl="~/Images/calendar.png"
TabIndex="1" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="txtFDate"
Format="dd/MM/yyyy" PopupPosition="BottomRight" PopupButtonID="imgCalendar" />
</td>
<td>
To Date:
</td>
<td>
<asp:TextBox ID="txtTDate" runat="server" Width="110px"></asp:TextBox>
<asp:ImageButton ID="imgCalendar1" runat="server" ImageUrl="~/Images/calendar.png"
TabIndex="2" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender3" runat="server" TargetControlID="txtTDate"
Format="dd/MM/yyyy" PopupPosition="BottomRight" PopupButtonID="imgCalendar1" />
</td>
<td>
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" OnClientClick="return ValidateDate();" />
</td>
<td>
<asp:Button ID="btnReset" runat="server" Text="Reset" OnClick="btnReset_Click" Width="47px" />
</td>
</tr>
</table>
<table style="width: 600px" runat="server" id="tblone">
<tr>
<td>
<asp:GridView ID="grdStudentInfo" runat="server" SkinID="Professional" DataKeyNames="StudentID"
Width="800px" AllowSorting="True" AutoGenerateColumns="False" BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
OnSorting="grdStudentInfo_sorting" AllowPaging="True" OnPageIndexChanging="grdStudentInfo_PageIndexChanging"
OnRowCommand="grdStudent_RowCommand">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:BoundField HeaderText="Name" DataField="FirstandLastName" SortExpression="FirstandLastName">
<ItemStyle HorizontalAlign="left" Width="100px" />
<HeaderStyle HorizontalAlign="left" Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="Amount Due" DataField="FirstInstallmentFee" SortExpression="FirstInstallmentFee">
<ItemStyle HorizontalAlign="left" Width="100px" />
<HeaderStyle HorizontalAlign="left" Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="Payment Mode" DataField="FirstInsMode" SortExpression="FirstInsMode">
<ItemStyle HorizontalAlign="left" Width="100px" />
<HeaderStyle HorizontalAlign="left" Width="100px" />
</asp:BoundField>
<asp:BoundField HeaderText="Bank Name" DataField="FirstBankName" SortExpression="FirstBankName">
<ItemStyle HorizontalAlign="left" Width="200px" />
<HeaderStyle HorizontalAlign="left" Width="200px" />
</asp:BoundField>
<asp:BoundField HeaderText="Cheque Number" DataField="FirstChequeNumber" SortExpression="FirstChequeNumber">
<ItemStyle HorizontalAlign="left" Width="200px" />
<HeaderStyle HorizontalAlign="left" Width="200px" />
</asp:BoundField>
<asp:BoundField HeaderText="Due Date" DataField="FirstInstallmentDate" SortExpression="FirstInstallmentDate">
<ItemStyle HorizontalAlign="left" Width="200px" />
<HeaderStyle HorizontalAlign="left" Width="200px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="editStudent" Text="Edit"
CommandArgument='<%#Eval("StudentID")%>'></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="200px" />
<HeaderStyle HorizontalAlign="Left" Width="200px" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</td>
</tr>
</table>
<table id="tbl3" runat="server">
<tr style="height: 30px;">
<td>
<asp:Button ID="btnExport2PDF" runat="server" Text="Export To PDF" OnClick="btnExport2PDF_Click" />
</td>
<td>
<asp:Button ID="btnExport2XLS" runat="server" Text="Export To XLS" OnClick="btnExport2XLS_Click" />
<asp:HiddenField ID="hidSort" runat="server" Value="1" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</center>
If I use frm.Controls.Add(tblOne) instead of frm.Controls.Add(divContent) then it works fine because tblone doesn't have an Ajax Control. But divContent has both the table as well as Ajax Controls, and it throws the above exception. I have tried many options like overriding OnInit and OnPreRender but doesn't work for me.
It could be due to the placement of ToolkitScriptManager in your code. See if this resolves your problem:
Error: Extender controls may not be registered before PreRender
Finally i have resolved this problem by using JavaScript code & it works better for me, i would like to thanks for all who gives their valuable time & feedback.
The code is here :
<script>
function Panel1() {
var panel = document.getElementById("<%=printablediv.ClientID %>");
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title>newTable</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(panel.innerHTML);
printWindow.document.write('</body></html>');
printWindow.document.close();
setTimeout(function() {
printWindow.print();
printWindow.close();
}, 1000);
return false;
}
</script>

Delete Individual row of gridview without deleting from database

I am using Linq-to-SQL in ASP.NET & C#. I want to delete individual row of gridview by click on delete button (i.e. CommandField or TemplateField) with gridTrainingNeed_RowDeleting event.
Here is my code for loading gridview on page load.
int departmentId;
int branchId;
int positionId;
double month;
int levelId;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Initialize();
}
}
public void Initialize()
{
ddlBranch.DataSource = TrainingManager.GetBranchName();
ddlBranch.DataBind();
ddlDepartment.DataSource = DepartmentManager.GetAllDepartments();
ddlDepartment.DataBind();
ddlPosition.DataSource = TrainingManager.GetDesignationName();
ddlPosition.DataBind();
ddlLevel.DataSource = TrainingManager.GetLevelName();
ddlLevel.DataBind();
gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
gridTrainingNeed.DataBind();
}
Now I am going to keep my design code:
<div class="innerLR">
<div class="seperator bottom" />
<div class="widget">
<div class="widget-head">
<h4 class="heading">
Send Circulation To
</h4>
</div>
<div class="widget-body">
<table class="tableShow" cellpadding="3" cellspacing="0">
<tr>
<td>
<asp:DropDownList ID="ddlBranch" runat="server" DataTextField="Name"
DataValueField="BranchId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Branch---"/>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlDepartment" runat="server" AppendDataBoundItems="True"
DataTextField="Name" Width="170px" DataValueField="DepartmentId">
<asp:ListItem Text="---Select Department---"/>
</asp:DropDownList></td>
<td>
<asp:DropDownList ID="ddlLevel" runat="server" DataTextField="Name"
DataValueField="LevelId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Level---"/>
</asp:DropDownList></td>
<td>
<asp:DropDownList ID="ddlPosition" runat="server" DataTextField="Name"
DataValueField="DesignationId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Position---"/>
</asp:DropDownList></td>
</tr>
<tr>
<td>
<br />
Send Circulation To</td>
<td>
<br />
Month</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlCirculation" runat="server" DataTextField="Name"
DataValueField="PositionId" Width="170px" AutoPostBack="true" AppendDataBoundItems="True"
onselectedindexchanged="ddlCirculation_SelectedIndexChanged">
<asp:ListItem Text="" Value="-1" />
<asp:ListItem Text="All" Value="0" />
<asp:ListItem Text="Must Have Worked For" Value="1" />
</asp:DropDownList></td>
<td>
<asp:TextBox ID="txtMonth" Width="150px" runat="server" Enabled="false"></asp:TextBox></td>
<td>
<asp:Button ID="btnShow" runat="server" Text="Show" onclick="btnShow_Click" /></td>
<td>
</td>
</tr>
</table>
</div>
</div>
<div>
<asp:GridView ID="gridTrainingNeed" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" CellPadding="4" ForeColor="#333333"
GridLines="None" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="EID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Branch" HeaderText="Branch" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="Level" HeaderText="Level" />
<asp:BoundField DataField="Position" HeaderText="Position" />
<asp:CommandField HeaderText="Delete" ShowCancelButton="False"
ShowDeleteButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</div>
</div>
Thus, I want to delete row one by one with RowDeleting Event from displaying data on gridview not from database.
protected void gridTrainingNeed_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//Row delete code
}
Thank You.
Instead of assigning the DataSource directly like you are doing:
gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
Store the result in Page level List. Define a page level List like:
//Define List at page/class level
List<Employee> gridList = new List<Employee>();
Then in your method Initialize
gridList = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
gridTrainingNeed.DataSource = gridList;
gridTrainingNeed.DataBind();
You need to persist the List<Employee> in memory on PostBack, Use Session/ViewState etc. (See ASP.Net State Management).
Later in your gridTrainingNeed_RowDeleting, get the ID from row or any field that uniquely identify an employee. Query your list and remove that object from the List in memory. Re-assign the DataSource to the modified list and call DataBind.

paging in gridview get same data for all pages

I had gridview which get data from database according to two event .The first from method ,the second when selected ddl.it worked well ,I made paging and when I go through paging pages I must click two click on number of page to go to this page also all pages get same data for firt page .So plaes any one help me.
<aspx>
<div class="div_open_CS m10 ad_mun_font">
<table width="560" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td class="pag_bor">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="Pag_toparow">
</td>
<td class="Pag_hedfont">
Dealers list
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="pag_bor">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="ad_mun_font_h" width="100">
Select City
</td>
<td>
<asp:SqlDataSource ID="SDSGetDealerArea" runat="server" ConnectionString="<%$ ConnectionStrings:ElarabyGroup %>"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT [AREA_ID], [AREA_ENG] FROM [DealerArea]">
</asp:SqlDataSource>
<%-- <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>--%>
<asp:DropDownList CssClass="ad_mun_font_h" ID="DDlCity" runat="server" AutoPostBack="True"
DataSourceID="SDSGetDealerArea" DataTextField="AREA_ENG" DataValueField="AREA_ID"
OnDataBound="DDlCity_DataBound" OnSelectedIndexChanged="DDlCity_SelectedIndexChanged">
</asp:DropDownList>
<%-- </ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DDlCity" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>--%>
</td>
<td>
<asp:Label ID="LblResult" runat="server" Text="Result" Font-Bold="True" ForeColor="#006699"
Visible="false"></asp:Label>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top" class="tm10 rm10">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None"
CellPadding="3" DataKeyNames="DEALER_ID,LOC_ID,CAT_ID,AREA_ID" CellSpacing="3"
GridLines="Horizontal" Width="510px" BorderWidth="1px" OnPageIndexChanging="GridView1_PageIndexChanging">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="NAME_ENG">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("NAME_ENG") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("NAME_ENG") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" SortExpression="ADR_ENG">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ADR_ENG") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ADR_ENG") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Telephone" SortExpression="TEL_ENG">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("TEL_ENG") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("TEL_ENG") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile" SortExpression="TEL_ARA">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("TEL_ARA") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("TEL_ARA") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="LOC_ENG">
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("LOC_ENG") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("LOC_ENG") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="CAT_NAME">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("CAT_NAME") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("CAT_NAME") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Area" SortExpression="AREA_ENG">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("AREA_ENG") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("AREA_ENG") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
<asp:Panel ID="pnlModal" runat="server" Style="display: none" CssClass="modalPopup">
Our Valued Customer You Are Not Registered Are You Want To Register?
<br />
<br />
<br />
<br />
<table id="LoginTL" runat="server" width="300" border="0" align="center" cellpadding="0"
cellspacing="2" visible="true" style="border-color: #666666">
<tr>
<td width="80px" class="bod_d_reg_txt_p lm7">
Username :
</td>
<td>
<label>
<asp:TextBox ID="TxtUser" runat="server" Width="125px"></asp:TextBox>
</label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="TxtUser" Display="Dynamic" SetFocusOnError="True">*</asp:RequiredFieldValidator>
</td>
<td>
<table width="110" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<label>
<asp:CheckBox ID="CBRemeber" runat="server" class="tx_s" OnCheckedChanged="CBRemeber_CheckedChanged" />
</label>
</td>
<td class="bod_d_reg_txt_w">
Remember me
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="80" class="bod_d_reg_txt_p lm7">
Password :
</td>
<td>
<label>
<asp:TextBox ID="TXTPassword" runat="server" TextMode="Password" Width="125px"></asp:TextBox>
</label>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*"
ControlToValidate="TXTPassword" Display="Dynamic" SetFocusOnError="True">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:ImageButton ID="IBLogin" runat="server" ImageUrl="images/home-d_login.jpg" OnClick="IBLogin_Click" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label8" runat="server" CssClass="ad_mun_font" Text="result" Visible="false"></asp:Label>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
Close
</td>
<td>
</td>
<td>
Register
</td>
</tr>
</table>
<br />
</asp:Panel>
<cc1:ModalPopupExtender TargetControlID="btnYes" ID="pnlModal_ModalPopupExtender"
runat="server" Enabled="True" BackgroundCssClass="modalBackground" PopupControlID="pnlModal"
CancelControlID="btnYes" DropShadow="true">
</cc1:ModalPopupExtender>
<asp:HiddenField ID="btnYes" runat="server" />
</td>
</tr>
</table>
</div>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridFunction();
}
}
private void BindGridFunction()
{
DataTable DT = new DataTable();
using (SqlConnection con = Connection.GetConnection())
{
SqlCommand Com = new SqlCommand();
if (DDlCity.SelectedIndex < 0)
{
Com = new SqlCommand("GetDealers", con);
Com.CommandType = CommandType.StoredProcedure;
SqlDataAdapter DA = new SqlDataAdapter(Com);
DA.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
else
{
Com = new SqlCommand("GetDealersByArea", con);
Com.CommandType = CommandType.StoredProcedure;
Com.Parameters.Add(Parameter.NewNVarChar("#AREA_ENG", DDlCity.SelectedItem.Text));
SqlDataAdapter DA = new SqlDataAdapter(Com);
DA.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
}
}
protected void DDlCity_SelectedIndexChanged(object sender, EventArgs e)
{
BindGridFunction();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
}
Try rebinding after GridView1.PageIndex = e.NewPageIndex; call GridView1.DataBind();
.
--EDIT--
DataSet and then DataBind(), try it like:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridFunction();
}

Categories

Resources