2014年7月13日 星期日

[RESOLVED] Gridview Paging




I am using Visual studio 2010 C# language and SQL SERVER 2008 for a web application...


I have a Gridview control, a Button control, and a Label control on a web page...The button control is placed on the web page and not inside the Gridview


control...The page size of the Gridview control is 1 so it displays one row per each page...


My aim is when I click on the Button control then the Gridview pager will move from one page to another page and the text on the cell (2) of the present row


in the Gridview control will be displayed on the label control and textbox control. Then the text in the textbox control will automaticaly be added to a


DATABASE after adding the data to the DATABASE then the program will automaticaly move to the next page and repeat the same process until it gets to the last


page then it will stop adding data to the DATABASE...


I have the code below and when I executed it...I clicked on the btnnext button and the program executed successfuly...

but when I checked the DATABASE I discovered that it was adding only the data on the first page of the Gridview...

For instant, if the total page on the Gridview is five(5), then it will add the text on the cell (2) five times in the DATABASE (e.g ken, ken, ken, ken, ken)


In summary, it automaticaly moves from page 1 to page 5 and add only the data on the text on the cell (2) five times in the DATABASE...That means it did not


add data on the text on the cell (2) of page 2, page 3, page 4, and page 5...?


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



public partial class testing : System.Web.UI.Page
{

public void adddata(Object sender, EventArgs e)
{
//ADD DATA TO DATABASE

SqlConnection connection = new SqlConnection(@"Data Source=KENNETH-PC;Initial Catalog=libdb;Integrated Security=True");
connection.Open();


try
{

SqlCommand cmd3 = new SqlCommand("insert into testing (cusid, fname) " +
"Values (@cusid, @fname)", connection);


cmd3.Parameters.AddWithValue("@cusid", txtid.Text);
cmd3.Parameters.AddWithValue("@fname", txtfname.Text);

cmd3.ExecuteNonQuery();
lbladd.Text = "ADDED................";
txtid.Text = "";
txtfname.Text = "";
}
finally
{

connection.Close();
}

//AUTOMATICALLY MOVE TO THE NEXT PAGE OF THE GRIDVIEW

nextdata(sender as object, e as EventArgs);

}

public void nextdata(object sender, EventArgs e)
{
if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
{

}
else
{

CustomersGridView.PageIndex = CustomersGridView.PageIndex + 1;


lblnxt.Text = CustomersGridView.Rows[0].Cells[2].Text + ".";
txtid.Text = CustomersGridView.Rows[0].Cells[1].Text;
txtfname.Text = CustomersGridView.Rows[0].Cells[2].Text;

//ADD DATA TO DATABASE
adddata(sender as object, e as EventArgs);

if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
{
lblmsge.Text = "THIS IS THE LAST PAGE!!!.......";

}
else
{
lblmsge.Text = "THIS IS PAGE " + CustomersGridView.PageIndex + "of " + CustomersGridView.PageCount + ".";
}
}
}

public void CustomersGridView_PageIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
//lblmsg.Text = CustomersGridView.SelectedRow.Cells[2].ToString();
lblmsg.Text = CustomersGridView.Rows[0].Cells[2].Text + ".";
}
public void CustomersGridView_PageIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;
lblmov.Text = "You selected " + row.Cells[2].Text + ".";
}

public void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{

// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;

// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";

}

public void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{

// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];


// You can cancel the select operation by using the Cancel
// property. For this example, if the user selects a customer with
// the ID "ANATR", the select operation is canceled and an error message
// is displayed.
if (row.Cells[1].Text == "ANATR")
{

e.Cancel = true;
MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";

}

}


protected void btnnext_Click(object sender, EventArgs e)
{
if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
{

}
else
{

CustomersGridView.PageIndex = CustomersGridView.PageIndex + 1;


lblnxt.Text = CustomersGridView.Rows[0].Cells[2].Text + ".";
txtid.Text = CustomersGridView.Rows[0].Cells[1].Text;
txtfname.Text = CustomersGridView.Rows[0].Cells[2].Text;

//ADD DATA To DATABASE
adddata(sender as object, e as EventArgs);

if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
{
lblmsge.Text = "THIS IS THE LAST PAGE!!!.......";

}
else
{
lblmsge.Text = "THIS IS PAGE " + CustomersGridView.PageIndex + "of " + CustomersGridView.PageCount + ".";
}
}


}


}

I NEED YOUR CONTRIBUTION...THANKS





Hi ken4aspnet,


According to your description and my understanding,  I rewrite you code  and it works fine in my lab machine. You can refer to bellow:


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

public partial class DataViewAdd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
protected void BindData()
{
string strConnection = "Data Source=PC-20130410DVTE;Initial Catalog=testDB;Integrated Security=True";
SqlConnection con = new SqlConnection(strConnection);
con.Open();
SqlCommand cmd = new SqlCommand("select * from testing", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
CustomersGridView.DataSource = ds;
CustomersGridView.DataBind();
con.Close();
}
//public void adddata(Object sender, EventArgs e)
public void adddata(int cusid, int fname)
{
//ADD DATA TO DATABASE

SqlConnection connection = new SqlConnection(@"Data Source=PC-20130410DVTE;Initial Catalog=testDB;Integrated Security=True");
connection.Open();
try
{

SqlCommand cmd3 = new SqlCommand("insert into testing (cusid, fname) " +
"Values (@cusid, @fname)", connection);

cmd3.Parameters.AddWithValue("@cusid", cusid);
cmd3.Parameters.AddWithValue("@fname", fname);

cmd3.ExecuteNonQuery();
lbladd.Text = "ADDED................";
txtid.Text = "";
txtfname.Text = "";
}
finally
{
connection.Close();
}

//AUTOMATICALLY MOVE TO THE NEXT PAGE OF THE GRIDVIEW

// nextdata();

}

//public void nextdata()
//{
// if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
// {

// }
// else
// {

// CustomersGridView.PageIndex = CustomersGridView.PageIndex + 1;
// // int i = CustomersGridView.PageIndex;

// lblnxt.Text = CustomersGridView.Rows[0].Cells[2].Text + ".";
// txtid.Text = CustomersGridView.Rows[0].Cells[1].Text;
// txtfname.Text = CustomersGridView.Rows[0].Cells[2].Text;

// //ADD DATA TO DATABASE
// adddata(txtid.Text, txtfname.Text);

// if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
// {
// lblmsge.Text = "THIS IS THE LAST PAGE!!!.......";

// }
// else
// {
// lblmsge.Text = "THIS IS PAGE " + CustomersGridView.PageIndex + "of " + CustomersGridView.PageCount + ".";
// }
// }
//}

public void CustomersGridView_PageIndexChanging(Object sender, GridViewSelectEventArgs e)
{
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];
//lblmsg.Text = CustomersGridView.SelectedRow.Cells[2].ToString();
lblmsg.Text = CustomersGridView.Rows[0].Cells[2].Text + ".";
}
public void CustomersGridView_PageIndexChanged(Object sender, EventArgs e)
{
// Get the currently selected row using the SelectedRow property.
string s = CustomersGridView.Rows[0].Cells[1].Text;
// GridViewRow row = CustomersGridView.SelectedRow;
//lblmov.Text = "You selected " + row.Cells[2].Text + ".";
}

public void CustomersGridView_SelectedIndexChanged(Object sender, EventArgs e)
{

// Get the currently selected row using the SelectedRow property.
GridViewRow row = CustomersGridView.SelectedRow;

// Display the company name from the selected row.
// In this example, the third column (index 2) contains
// the company name.
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";

}

public void CustomersGridView_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
{

// Get the currently selected row. Because the SelectedIndexChanging event
// occurs before the select operation in the GridView control, the
// SelectedRow property cannot be used. Instead, use the Rows collection
// and the NewSelectedIndex property of the e argument passed to this
// event handler.
GridViewRow row = CustomersGridView.Rows[e.NewSelectedIndex];


// You can cancel the select operation by using the Cancel
// property. For this example, if the user selects a customer with
// the ID "ANATR", the select operation is canceled and an error message
// is displayed.
if (row.Cells[1].Text == "ANATR")
{
e.Cancel = true;
MessageLabel.Text = "You cannot select " + row.Cells[2].Text + ".";

}

}

protected void btnnext_Click(object sender, EventArgs e)
{
//if (CustomersGridView.PageIndex != CustomersGridView.PageCount)
//{

//}
//else
//{

// CustomersGridView.PageIndex = CustomersGridView.PageIndex + 1;


// lblnxt.Text = CustomersGridView.Rows[0].Cells[2].Text + ".";
// txtid.Text = CustomersGridView.Rows[0].Cells[1].Text;
// txtfname.Text = CustomersGridView.Rows[0].Cells[2].Text;

// ADD DATA To DATABASE
// adddata(txtid.Text, txtfname.Text);

// if (CustomersGridView.PageIndex == CustomersGridView.PageCount)
// {
// lblmsge.Text = "THIS IS THE LAST PAGE!!!.......";

// }
// else
// {
// lblmsge.Text = "THIS IS PAGE " + CustomersGridView.PageIndex + "of " + CustomersGridView.PageCount + ".";
// }
////}
//while (CustomersGridView.PageIndex != CustomersGridView.PageCount)
//{
// txtid.Text = CustomersGridView.Rows[0].Cells[1].Text;
// txtfname.Text = CustomersGridView.Rows[0].Cells[2].Text;
// total_id += Convert.ToInt32(txtid.Text);
// total_name += Convert.ToInt32(txtfname.Text);
// CustomersGridView.PageIndex++;

// CustomersGridView_PageIndexChanged( sender, e);

//}

int total_id = 0;
int total_name = 0;
CustomersGridView.AllowPaging = false;
CustomersGridView.DataBind();
foreach (GridViewRow row in CustomersGridView.Rows)
{
txtid.Text = row.Cells[1].Text;
txtfname.Text =row.Cells[2].Text;
total_id += Convert.ToInt32(txtid.Text);
total_name += Convert.ToInt32(txtfname.Text);
}

CustomersGridView.AllowPaging = true;
// do your databind here again
CustomersGridView.DataBind();
adddata(total_id, total_name);

}

}

You can alter on your demand. Hope it can help. 


Best Regards.


Fannie


沒有留言:

張貼留言