formbindingnavigator.cs

来自「csharp课本的源代码」· CS 代码 · 共 101 行

CS
101
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BindingNavigatorExample
{
    public partial class FormBindingNavigator : Form
    {
        SqlDataAdapter adapter;
        DataTable selectedTable;
        public FormBindingNavigator()
        {
            InitializeComponent();
        }
        private void FormBindingNavigator_Load(object sender, EventArgs e)
        {
            bindingNavigator1.BindingSource = bindingSource1;
            //将MyDatabase.mdf中的表名称添加到dataTableListBox中
            for (int i = 0; i < myDatabaseDataSet.Tables.Count; i++)
            {
                listBoxMyTables.Items.Add(myDatabaseDataSet.Tables[i].TableName);
            }
            listBoxMyTables.SelectedIndex = 0;
            //不允许用户直接在最下面的行添加新行
            dataGridView1.AllowUserToAddRows = false;
            //不允许用户直接按Delete键删除行
            dataGridView1.AllowUserToDeleteRows = false;
        }
        private void listBoxMyTables_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = listBoxMyTables.SelectedIndex;
            selectedTable = myDatabaseDataSet.Tables[index];
            string queryString = "select * from " + selectedTable.TableName;
            adapter = new SqlDataAdapter(
                queryString, Properties.Settings.Default.MyDatabaseConnectionString);
            SqlCommandBuilder builer = new SqlCommandBuilder(adapter);
            adapter.InsertCommand = builer.GetInsertCommand();
            adapter.DeleteCommand = builer.GetDeleteCommand();
            adapter.UpdateCommand = builer.GetUpdateCommand();
            adapter.Fill(selectedTable);
            bindingSource1.DataSource = selectedTable;
            dataGridView1.DataSource = bindingSource1;
        }
        private void buttonAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                bindingSource1.AddNew();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show(
                    "请先单击最左边的空白列选择要删除的行,可以按住<Ctrl>同时选中多行");
            }
            else
            {
                if (MessageBox.Show(
                    "确实要删除选定的行吗?", "小心",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    for (int i = dataGridView1.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        bindingSource1.RemoveAt(dataGridView1.SelectedRows[i].Index);
                    }
                }
            }
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                bindingSource1.EndEdit();
                adapter.Update(myDatabaseDataSet.Tables[listBoxMyTables.SelectedIndex]);
                MessageBox.Show("保存成功");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "保存失败");
            }
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
        }
           
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?