zoukankan      html  css  js  c++  java
  • WinForm控件之【BindingNavigator】【DataSet】【BindingSource】【DataGridView】

    基本介绍

    数据类控件,数据加载绑定便捷应用相当广泛,具体看例子自行扩展吧;

    常设置属性

    BindingNavigator--BindingSource:数据来源,绑定后默认项会根据相应的操作按钮执行操作;

    BindingNavigator--Items:显示项的集合;

    DataSet--Tables:数据集内数据表的集合;

    BindingSource--DataSource: 数据源绑定数据集;

    BindingSource--Filter、sort:对数据源的筛选、排序;

    DataGridView--DataSource:数据源绑定数据集;

    DataGridView--Columns、Rows:数据源中列、行的对象集合;

    事例举例

    相关代码

            //控件数据绑定
            private void btn_serachData_Click(object sender, EventArgs e)
            {
                string strValue = txt_sql.Text.Trim();
                if (!string.IsNullOrWhiteSpace(strValue))
                {
                    //获取数据源绑定DataSet
                    dataSet1 = Helpers.DBHelper.GetDataBySql(strValue);
                    if (Helpers.UtilityHelper.GetRowCount(dataSet1) > 0)
                    {
                        //绑定DataGridView
                        dataGridView1.DataSource = dataSet1.Tables[0];
                        
                        //绑定BindingSource
                        bindingSource1.DataSource = dataSet1;
                        bindingSource1.DataMember = dataSet1.Tables[0].TableName;
    
                        //指定数据列绑定TextBox
                        txt_code.DataBindings.Add("Text", bindingSource1, "SITE_CODE");
                        txt_name.DataBindings.Add("Text", bindingSource1, "SITE_NAME");
    
                        //绑定BindingNavigator
                        bindingNavigator1.BindingSource = bindingSource1;
                    }
                }
            }
    
            //dataGridView数据选择联动
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (dataGridView1 != null && !string.IsNullOrWhiteSpace(txt_code.Text))
                {
                    //切换数据时,定位选中列表行
                    dataGridView1.ClearSelection();
                    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        string str = row.Cells["SITE_CODE"].Value.ToString();
                        if (str.Equals(txt_code.Text))
                        {
                            row.Selected = true;
                            return;
                        }
                    }
                }
            }
  • 相关阅读:
    Jvm年轻代复制到Survivor To区时,对象存放不下会发生什么?
    Jvm内存布局和Java对象内存布局
    ArrayList的removeIf和iterator.remove性能比较
    闲着没事做,用js做了一个冒泡排序的动画
    对象与this
    idea 简记
    线程按序交替
    大数阶乘
    序列化 与 反序列化
    人月神话
  • 原文地址:https://www.cnblogs.com/ljhandsomeblog/p/11246094.html
Copyright © 2011-2022 走看看