C# winform分页查询

时间:2024-11-04 09:02:04
using ;
using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;


namespace WindowsFormsApp1
{
    public partial class StudentLateBackPanelRightSelect : Form
    {


        private LaterBackHelper laterBackHelper;


        private string []page_counts = { "5","6","7","8","9","10","15","20"};


        private int total_page, page_step, current_page_index, total_count;


        private DataSet dataSet;


        public StudentLateBackPanelRightSelect()
        {
            laterBackHelper = new LaterBackHelper();
            dataSet = ();
            total_count = [0].;
            page_step = 5;
            total_page = Convert.ToInt16(((total_count) / page_step));
            InitializeComponent();
            
            initView(GetViewDataSet(dataSet,1,5));
            comboBox_page_count.(page_counts);
            comboBox_page_count.SelectedIndex = 0;
            label_all_item_count.Text = total_count.ToString();
            label_page_count.Text = total_page.ToString();
        }
        //每次点击时刷新全局数据
        private void initDataPage(int total_count,int page_step,  int current_page_index)
        {
            this.total_count = total_count;
            this.page_step = page_step;
            this.total_page = Convert.ToInt16(((total_count) / page_step));
            this.current_page_index = current_page_index;
        }
        //初始化界面并加载数据
        private void initView(DataSet ds)
        {
            = false;
            = [0];
            ["dor_id"].DataPropertyName = [0].Columns[0].ToString();
            ["stu_id"].DataPropertyName = [0].Columns[1].ToString();
            ["time"].DataPropertyName = [0].Columns[2].ToString();
            ["reason"].DataPropertyName = [0].Columns[3].ToString();
        }
        
        private void button_select_Click(object sender, EventArgs e)
        {
            string stu_id = textBox_stu_id.Text, dor_id = textBox_dor_id.Text, time = textBox_time.Text, reason = textBox_reason.Text;
            if(stu_id.Equals("")&& dor_id.Equals("") && ("") && (""))
            {
                return;
            }
            if (dor_id.Equals("")) dor_id = "-1";
            if (stu_id.Equals("")) stu_id = "-1";
            LaterBack laterBack = new LaterBack((dor_id), (stu_id), time, reason);
            ();
            dataSet = (laterBack);
            initDataPage([0]., page_step, 1);
            initView(GetViewDataSet(dataSet, 1, page_step));
            label_page_count.Text = total_page.ToString();
            label_page_range.Text = "1-" + page_step.ToString();
            label_all_item_count.Text = total_count.ToString();
        }
        //只能输入数字
        private void only_num_press(object sender, KeyPressEventArgs e)
        {
            if (!(()) && != (char)8)
            {
                = true;
            }
        }
        //对行数据进行删除或修改操作
        private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string action = [].Name;//操作类型
            var cells = [].Cells;
            LaterBack laterBack = new LaterBack((cells[0].()), (cells[1].()), cells[2].(), cells[3].());
            switch (action)
            {
                case "update":
                    //获取相应列的数据ID,弹出加载了该ID数据详细信息的Form,用以修改
                   StudentLateBackPanelRightUpdate studentLateBackPanelRightUpdate =  new StudentLateBackPanelRightUpdate(laterBack);
                    ();
                    break;
                case "delete":
                    if (("确定删除这行数据吗?", "删除提示", ) == )
                    {
                        //获取相应列的数据ID,删除此数据记录      
                        int result = (laterBack);
                        if(result > 0)
                        {
                            //();
                            dataSet = ();
                            initDataPage([0].,page_step,1);
                            initView(GetViewDataSet(dataSet,1,page_step));
                            label_page_count.Text = total_page.ToString();
                            label_page_range.Text = "1-" + page_step.ToString();
                            label_all_item_count.Text = total_count.ToString();
                        }
                    }
                    break;
                default:
                    break;
            }
        }


        //获取要显示的数据源
        public DataSet GetViewDataSet(DataSet a_ds,int from_index,int end_index)
        {
            //首先先声明一个DataSet对象和一个DataTable对象      
            DataSet l_ds = new DataSet();
            DataTable l_dt = new DataTable();
            //构建DataTable对象的列值
            l_dt.("dor_id");//这些列名就是返回的DataSet的列名,可以随意添加
            l_dt.("stu_id");
            l_dt.("time");
            l_dt.("reason");
            
            //遍历传进来的DataSet的值,并对DataTable进行赋值操作
            for (int i = from_index - 1; i < end_index ; i++)
            {
                DataRow dr = l_dt.NewRow();//首先新增一行,然后对其进行赋值
                dr["dor_id"] = a_ds.Tables[0].Rows[i][0].ToString().Trim();
                dr["stu_id"] = a_ds.Tables[0].Rows[i][1].ToString().Trim();
                dr["time"] = a_ds.Tables[0].Rows[i][2].ToString().Trim();
                dr["reason"] = a_ds.Tables[0].Rows[i][3].ToString().Trim();
                
                l_dt.(dr);//这里一定要add进去
            }
            l_ds.(l_dt);//这里也不能忘记
            return l_ds;
        }


        //combobox更改触发事件
        private void combobox_selected_listen(object sender, EventArgs e)
        {
            page_step = (comboBox_page_count.());
            textBox_page_count.Text = ();
            label_page_range.Text = "1-" + page_step.ToString();
            total_page = Convert.ToInt16( ((total_count) / page_step));
            label_page_count.Text = total_page.ToString();
            initView(GetViewDataSet(dataSet,  1,  page_step));
        }


        //输入页数变化
        private void page_count_change(object sender, EventArgs e)
        {
            if (textBox_page_count.("")) return;
            if ((textBox_page_count.Text) < 1)
            {
                ("输入页数不能小于1", "系统提示");
                return;
            }
            if ((textBox_page_count.Text) > total_page)
            {
                ("输入页数超过总页数", "系统提示");
                return;
            }
            current_page_index = (textBox_page_count.Text);
            var view_range = (current_page_index - 1)* page_step ;
            if (view_range + page_step < total_count) {
                initView(GetViewDataSet(dataSet, view_range + 1, view_range + page_step));
                label_page_range.Text = (view_range + 1).ToString() + "-" + (view_range + page_step).ToString();
            }
            else {
                initView(GetViewDataSet(dataSet, view_range + 1, total_count));
                label_page_range.Text = (view_range + 1).ToString() + "-" + total_count.ToString();
            }

        }


        private void button_first_page_Click(object sender, EventArgs e)
        {
            current_page_index = 1;
            var view_range = (current_page_index - 1) * page_step;
            initView(GetViewDataSet(dataSet, view_range + 1, view_range + page_step));
            label_page_range.Text = (view_range + 1).ToString() + "-" + page_step.ToString();
            textBox_page_count.Text = current_page_index.ToString();
        }


        private void button_previous_page_Click(object sender, EventArgs e)
        {
            if (current_page_index == 1) return;
            current_page_index -= 1;
            var view_range = (current_page_index - 1) * page_step;
            initView(GetViewDataSet(dataSet, view_range + 1, view_range + page_step));
            label_page_range.Text = (view_range + 1).ToString() + "-" + (view_range + page_step).ToString();
            textBox_page_count.Text = current_page_index.ToString();

        }

        private void button_next_page_Click(object sender, EventArgs e)
        {
            if (current_page_index == total_page) return;
            current_page_index += 1;
            var view_range = (current_page_index - 1) * page_step;
            if (view_range + page_step < total_count)
            {
                initView(GetViewDataSet(dataSet, view_range + 1, view_range + page_step));
                label_page_range.Text = (view_range + 1).ToString() + "-" + (view_range + page_step).ToString();
            }
            else
            {
                initView(GetViewDataSet(dataSet, view_range + 1, total_count));
                label_page_range.Text = (view_range + 1).ToString() + "-" + total_count.ToString();
            }
            textBox_page_count.Text = current_page_index.ToString();
        }


        private void button_last_page_Click(object sender, EventArgs e)
        {
            current_page_index = total_page;
            var view_range = (current_page_index - 1) * page_step;
            initView(GetViewDataSet(dataSet, view_range + 1, total_count));
            label_page_range.Text = (view_range + 1).ToString() + "-" + total_count.ToString();
            textBox_page_count.Text = current_page_index.ToString();
        }
    }
}