• C# bindingNavigator1 绑定后如何重写按钮事件


    C# bindingNavigator1 绑定后如何重写按钮事件

    工具条上有6个按钮、1个文本框、一个标签,已经关联了对应的属性、事件

    clipboard

    如何屏蔽默认事件 不要触发绑定单击事件 (屏蔽它)

    删除按钮: 删除如何提示确认

    如果想让某个按钮成为普通按钮 选中bindingNavigator1 按照图所示设置它的属性为无

    clipboard


    bindingNavigator1

    将bindingNavigator1绑定到dataGridView上,bindingNavigator1绑定后则上面的按钮就可以起作用了

    比如 添加/删除/转到 第一行、是上行、下一行、最后一行,以及按钮何时可以使用,何时不能使用 这此功能全部自动实现了

    下面的5个文本框和BindingSource 绑定源绑定了 它可以得到当前选中的行相关信息,如果修改了文本框中的内容的话则会更新到BindingSource 源中


    clipboard

    源文件下载:

    attachment



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SQLite;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace dgv分页 {
        public partial class Form2 : Form {
            public Form2() {
                InitializeComponent();
            }
            /// <summary>
            ///     bs = new BindingSource();
            /// </summary>
            BindingSource bs = new BindingSource();
            /// <summary>
            ///  dt = new DataTable(); 用于过滤数据用    dgv1.DataSource = dt;
            /// </summary>
            DataTable dt = new DataTable();
            DataSet dataSet;
    
            private void Form2_Load(object sender,EventArgs e) {
                SQLiteHelper sqlite = new SQLiteHelper("Data Source = test.db; Version=3;");
                dataSet = sqlite.DataAdapter($"Select * From Data","Data");
    
                dt = dataSet.Tables[0];
    
                dgv1.DataSource = dt;
    
    
                //bs.DataSource = null;
                //绑定部分 ---------------------------
                bs.DataSource = dt;
                dgv1.DataSource = bs;
                bindingNavigator1.BindingSource = bs;
    
                //绑定之后bindingNavigator1的按钮是不需要写任何代码就能直接调用的
                //控件绑定----------------------------
                //参数1 = textBox2的属性 textBox2.Text
                //参数2 = 绑定源 DataSource
                //参数3 = DataTable列的名字 "单词" 列
                textBox_单词.DataBindings.Add("Text",bs,"单词");//修改控件的属性 会更新表格的属性值的
                textBox_音标.DataBindings.Add("Text",bs,"音标");
                textBox_解释.DataBindings.Add("Text",bs,"解释");
                textBox_id.DataBindings.Add("Text",bs,"id");
                textBox2.DataBindings.Add("Text",bs,"已掌握");
            }
    
            private void textBox_id_TextChanged(object sender,EventArgs e) {
                TextBox t = sender as TextBox;
                if (t == null) return;
                t.Text = SqlString.Restore(t.Text);
                //处理特殊字符串
                //textBox_单词.Text = SqlString.Restore(textBox_单词.Text);
                //textBox_音标.Text = SqlString.Restore(textBox_音标.Text);
                //textBox_解释.Text = SqlString.Restore(textBox_解释.Text);
            }
    
            private void textBox_id_Validated(object sender,EventArgs e) {
                //此方法相当于 LostFocus事件
                //更新到数据库中需要在此写并且要加个判断Modified减少对数据库的操作
            }
    
            private void dgv1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e) {
                //格式化绑定
    
                if (e.Value == null) return;
                //绑定后显示的数值不是显示的数值
                switch (dgv1.Columns[e.ColumnIndex].Name) {
                    case "单词":
                    case "音标":
                    case "解释":
                    case "例句":
                        e.Value = SqlString.Restore(e.Value.ToString());
                        break;
                }
            }
    
            private void bindingNavigatorAddNewItem_Click(object sender,EventArgs e) {
    
            }
    
            //为了确保下面的代码可单独使用 选中bindingNavigator1设置DeleteItem的值为空
            private void bindingNavigatorDeleteItem_Click(object sender,EventArgs e) {
                DialogResult d = MessageBox.Show("确认删除吗?","删除",MessageBoxButtons.OKCancel);
                if ( d== DialogResult.OK) {
                      //删除的是dataSet表中的行,而不是删除dataGridView的行
                    dataSet.Tables[0].Rows[bindingNavigator1.BindingSource.Position].Delete();
                }
    
            }
    
            private void button1_Click(object sender,EventArgs e) {
                //默认选中的dgv1索引项
                bindingNavigator1.BindingSource.Position =  -1; //修改bindingNavigator1.BindingSource.Position的索引来选中dataGridView默认选中项 
            }
    
    
        }
    }
    




    2021年1月5日 17:26:03

  • 相关阅读:
    如何启用apache的gzip压缩?
    Zend Framework配置Nginx的rewrite
    数据库差异比较工具
    心愿王泽 杨颖 乔媛 唐景莲
    在所有存储过程中查找一个关键字
    通用分页存储过程
    JavaScript开发工具 Aptana
    js如何控制select控件(下拉列表)
    Read and write flat file
    Extreme Programming
  • 原文地址:https://www.cnblogs.com/xe2011/p/14237332.html
Copyright © 2020-2023  润新知