ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

用BindingSource将Windows窗体控件绑定到DBNull数据库值

2021-05-22 10:05:27  阅读:167  来源: 互联网

标签:控件 bindingSource1 Windows System button1 窗体 new using Add


https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-bind-windows-forms-controls-to-dbnull-database-values?view=netframeworkdesktop-4.8

当您将Windows Forms控件绑定到数据源并且数据源返回DBNull值时,您可以替换适当的值而无需处理,格式化或解析事件。格式化或解析数据源值时,NullValue属性会将DBNull转换为指定的对象。

例子

下面的示例演示如何在两种不同的情况下绑定DBNull值。第一个演示了如何为字符串属性设置NullValue;第二个示例演示如何为图像属性设置NullValue

 

 

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            this.Load += new EventHandler(Form1_Load);
        }

        // The controls and components we need for the form.
        private Button button1;
        private PictureBox pictureBox1;
        private BindingSource bindingSource1;
        private TextBox textBox1;
        private TextBox textBox2;

        DataTable employeeTable = new DataTable();//保存数据

        void Form1_Load(object sender, EventArgs e)
        {
            // Basic form setup.
            this.pictureBox1 = new PictureBox();
            this.bindingSource1 = new BindingSource();
            this.textBox1 = new TextBox();
            this.textBox2 = new TextBox();
            this.button1 = new Button();
            this.pictureBox1.Location = new System.Drawing.Point(20, 20);
            this.pictureBox1.Size = new System.Drawing.Size(174, 179);
            this.textBox1.Location = new System.Drawing.Point(25, 215);
            this.textBox1.ReadOnly = true;
            this.textBox2.Location = new System.Drawing.Point(25, 241);
            this.textBox2.ReadOnly = true;
            this.button1.Location = new System.Drawing.Point(200, 103);
            this.button1.Text = "Move Next";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.pictureBox1);
            this.ResumeLayout(false);
            this.PerformLayout();

            // Create the connection string and populate the data table
            // with data.
            string connectionString = "Integrated Security=SSPI;" +
                "Persist Security Info = False;Initial Catalog=Northwind;" +
                "Data Source = localhost";
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString = connectionString;
            SqlDataAdapter employeeAdapter =
                new SqlDataAdapter(new SqlCommand("Select * from Employees", connection));
            connection.Open();
            employeeAdapter.Fill(employeeTable);

            // Set the DataSource property of the BindingSource to the employee table.
            bindingSource1.DataSource = employeeTable;

            // Set up the binding to the ReportsTo column.
            Binding reportsToBinding = textBox2.DataBindings.Add("Text", bindingSource1,
                "ReportsTo", true);

            // Set the NullValue property for this binding.
            reportsToBinding.NullValue = "No Manager";

            // Set up the binding for the PictureBox using the Add method, setting
            // the null value in method call.
            pictureBox1.DataBindings.Add("Image", bindingSource1, "Photo", true,
                DataSourceUpdateMode.Never, new Bitmap(typeof(Button), "Button.bmp"));

            // Set up the remaining binding.
            textBox1.DataBindings.Add("Text", bindingSource1, "LastName", true);
        }

        // Move through the data when the button is clicked.
        private void button1_Click(object sender, EventArgs e)
        {
            bindingSource1.MoveNext();
        }
    }
}

 

标签:控件,bindingSource1,Windows,System,button1,窗体,new,using,Add
来源: https://www.cnblogs.com/wfy680/p/14798153.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有