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;
                    }
                }
            }
        }

猜你喜欢

转载自www.cnblogs.com/ljhandsomeblog/p/11246094.html