C#中获取datagridview单元格和分页显示以及支持下拉数据输入

添加控件:

bindingSource1和bindingNavigator1

支持datagridview下拉输入:

 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            string titleText=dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText;
            
            if(titleText.Equals("name"))
            {
                TextBox autoText = e.Control as TextBox;
                System.Diagnostics.Debug.Write(titleText + "\r");
                if(autoText!=null)
                {
                    System.Diagnostics.Debug.Write(autoText + "\r");
                    autoText.AutoCompleteMode = AutoCompleteMode.Suggest;
                    autoText.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
                    DataCollection.Add("开发部");
                    DataCollection.Add("财务部");
                    autoText.AutoCompleteCustomSource = DataCollection;
                }
            }
        }

升序排列:

 private void btnAscend_Click(object sender, EventArgs e)
        {
            dataGridView1.Sort(dataGridView1.Columns[2], ListSortDirection.Ascending);

        }

加载事件:

      dataGridView1.RowsDefaultCellStyle.BackColor = Color.IndianRed;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Blue;
            //总页数计算
            totalPageNumber = (totalValNumber % valPage == 0) ? (totalValNumber / valPage) : (totalValNumber / valPage + 1);
            toolStripTextBox1.Text = "1/"+totalPageNumber;

查询语句:

private void query_CX(DataGridView dt)//查询语句
        {
            OleDbConnection myCon = Getconnection();
            try
            {
                myCon.Open();
                string sql = "select * from 表1";
                OleDbDataAdapter myda = new OleDbDataAdapter(sql, myCon);
                DataSet myds = new DataSet();
                myda.Fill(myds, "表1");
                dt.DataSource = myds.Tables[0];
                totalValNumber = myds.Tables[0].Rows.Count;

                currentPage = StartVal / valPage + 1;
                toolStripTextBox1.Text = currentPage.ToString() + "/" + totalPageNumber;
                myds.Clear();
                myda.Fill(myds,StartVal,valPage,"表1");
                bindingSource1.DataSource = myds.Tables[0];
                bindingNavigator1.BindingSource = bindingSource1;
                dt.DataSource = bindingSource1;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
            finally
            {
                myCon.Close();
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

品尚公益团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值