WinForm control of BindingNavigator [DataSet] [] [] [DataGridView BindingSource]

basic introduction

Type of control data, data load bindings convenient wide range of applications, specifically to see examples of their own extended it;

Chang set properties

The BindingNavigator - the BindingSource : Data sources, based on the binding default entry button to execute the corresponding operation;

The BindingNavigator - the Items : displaying a set of items;

The DataSet - the Tables : aggregate data set within the data table;

BindingSource - DataSource:  string;

The BindingSource - the Filter, Sort : filtering, sorting of the data source;

The DataGridView - the DataSource : Calendar bottom of the control indicating whether today's date display;

The DataGridView - the Columns, the Rows : column data source, set the target line;

 

Case example

 

The relevant code

        //控件数据绑定
        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;
                    }
                }
            }
        }

 

Guess you like

Origin www.cnblogs.com/ljhandsomeblog/p/11246094.html