小白请教一个问题,请大神帮我来看看,谢谢
我现在做个查询,结果分页显示,如果没有查到结果,则给提示,没有查到任何记录,但为啥MESSAGEBOX会弹出两次,谢谢
public partial class Form2 : Form
{
DataView mydv=new DataView();
public Form2()
{
InitializeComponent();
}
public int pageSize = 10; //每页记录数
public int recordCount = 0; //总记录数
public int pageCount = 0; //总页数
public int currentPage = 0; //当前页
DataTable dtSource = new DataTable();
///LoadPage方法
/// <summary>
/// loaddpage方法
/// </summary>
private void LoadPage()
{
for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
this.dataGridView1.Columns[i].MinimumWidth = 60;
}
for (int i = 1; i < this.dataGridView1.Columns.Count; i++)
{
this.dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}
if (currentPage < 1) currentPage = 1;
if (currentPage > pageCount) currentPage = pageCount;
int beginRecord;
int endRecord;
DataTable dtTemp;
dtTemp = dtSource.Clone();
beginRecord = pageSize * (currentPage - 1);
if (currentPage == 1) beginRecord = 0;
endRecord = pageSize * currentPage;
if (currentPage == pageCount) endRecord = recordCount;
if (recordCount == 0)
MessageBox.Show("查不到符合条件的记录");
else
{
for (int i = beginRecord; i < endRecord; i++)
{
dtTemp.ImportRow(dtSource.Rows[i]);
}
}
dataGridView1.ScrollBars = ScrollBars.Vertical;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.Single;
dataGridView1.DataSource = dtTemp; //datagridview控件名是tf_dgv1
toolStripLabel1.Text = "当前位于第 "+currentPage.ToString()+" 页";//当前页
toolStripLabel4.Text = "共 " + pageCount.ToString()+" 页";//总页数
toolStripLabel6.Text = "查询到 "+recordCount.ToString()+" 条符合条件的记录";//总记录数
}
/// <summary>
/// 分页的方法
/// </summary>
/// <param name="str"></param>
private void fenye(string str) //str是sql语句
{
SqlConnection myconn = new SqlConnection();
myconn.ConnectionString = Form1.mystr;
myconn.Open();
DataSet myds = new DataSet();
SqlDataAdapter myadp = new SqlDataAdapter(str, myconn);
myadp.Fill(myds, "ksb");
dataGridView1.DataSource = myds.Tables["ksb"];
mydv = myds.Tables["ksb"].DefaultView;
dtSource = myds.Tables[0];
recordCount = dtSource.Rows.Count;
pageCount = (recordCount / pageSize);
if ((recordCount % pageSize) > 0)
{
pageCount++;
}
//默认第一页
currentPage = 1;
LoadPage();//调用加载数据的方法
}
private void form2_load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (txt1.Text != "")
{
string str = "select * from ksb where 姓名 like'%" + txt1.Text + "%'or 身份证号 like'%" + txt1.Text + "%'";
fenye(str);//分页
LoadPage();//加载数据
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
currentPage = 1;
LoadPage();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
currentPage = pageCount;
LoadPage();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
currentPage++;
LoadPage();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
currentPage--;
LoadPage();
}