C# bindingNavigator1 綁定後如何重寫按鈕事件
工具條上有6個按鈕、1個文本框、一個標籤,已經關聯了對應的屬性、事件
如何屏蔽默認事件 不要觸發綁定單擊事件 (屏蔽它)
刪除按鈕: 刪除如何提示確認
如果想讓某個按鈕成爲普通按鈕 選中bindingNavigator1 按照圖所示設置它的屬性爲無
bindingNavigator1
將bindingNavigator1綁定到dataGridView上,bindingNavigator1綁定後則上面的按鈕就可以起作用了
比如 添加/刪除/轉到 第一行、是上行、下一行、最後一行,以及按鈕何時可以使用,何時不能使用 這此功能全部自動實現了
下面的5個文本框和BindingSource 綁定源綁定了 它可以得到當前選中的行相關信息,如果修改了文本框中的內容的話則會更新到BindingSource 源中
源文件下載:
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