可視化怎么移動bindingnavigator控件
/步驟
在winform工程中,打開工具箱,找到bindingnavigator控件,并拖入winform窗體中,這時窗體上出現bindingnavigator控件,窗體下自動生成bindingnavigator控件實例。
依次在窗體上雙擊bindingnavigator控件的按鈕可以切換到按鈕點擊事件的后臺,如果只是做記錄的導航,就不必再另外寫事件代碼。原因嘛,先賣個關子。
在工具箱中,找到DataGridView控件到窗體,這個控件我們是要拿來展示數據的。
調整窗體和DataGridView控件大小
在窗體空白處雙擊,進入form load事件代碼,寫下如下代碼。代碼含義如下:
bs = new BindingSource();//BindingSource對象,用來轉換datatable數據源的
bs.DataSource = t.SearchDb;//t.SearchDb是一個有數據的datatable,把/t.SearchDb綁定到bs上
bindingNavigator1.BindingSource = bs;//把數據源綁定在bindingNavigator1上
dataGridView1.DataSource = bs;//把數據源綁定在dataGridView1上
運行工程,查看效果。可以看到bindingNavigator1顯示了記錄總數和當前記錄索引號,dataGridView1顯示了綁定的數據
點擊向后按鈕,可以看到當前記錄立刻向后移動到下一條記錄
點擊移動到最后按鈕,可以看到當前記錄立刻向后移動到最后一條記錄
點擊bindingNavigator1上的加號,在dataGridView1最后一行出現編輯框,并且可以編輯數據
選中最后一行,點擊bindingNavigator1上的叉號,就刪除了選中的行。
現在要解釋剛才的賣的關子:雖然我們并沒有在bindingNavigator1的按鈕事件上添加代碼,但是我們把bindingNavigator 和 BindingSource一起使用, bindingNavigator 的這些按鈕事件都與BindingSource的方法對應。
C#中的bindingNavigator控件
BindingSource的使用
BindingSource bs = new BindingSource();//聲明綁定類
using (SqlConnection conn = new SqlConnection(strConnecion))
{
try
{
conn.Open();//打開聯結
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "lect * from Customers";//選擇要查找的表
SqlDataAdapter da = new SqlDataAdapter();//聲明適配器
DataSet ds = new DataSet();//聲明數據集
da.SelectCommand = comm;//將適配器與數據集關連
da.Fill(ds);//將表中數據填入數據集中
bs.DataSource = ds.Tables[0];//將綁定數據源中的數據連接到數據集中去.
dataGridView1.DataSource = bs;//綁定控件dataGridView
bindingNavigator1.BindingSource = bs;//綁定控件bindingNavigator
textBox1.DataBindings.Add("Text", bs, "CompanyName", true);//綁定控件textBox
comboBox1.DataSource = bs;////綁定控件comboBox
comboBox1.DisplayMember = "City";//comboBox前臺顯示的值
comboBox1.ValueMember = "City";//comboBox后臺實際綁定的值
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Clo();
}
c#中bindingNavigator的用法是什么,求示范……
在界面上放入bindingNavigator1,dataGridView1,button1,button2,textBox1控件,加入引用
using System.Data.OracleClient; 沒有就自己添加引用
private BindingSource myBindingSource = new BindingSource();
OracleConnection conn = new OracleConnection("Data Source=xxx;Persist Security Info=True;Ur ID=xxx;Password=xxx");
private void Form1_Load(object nder, EventArgs e)
{
string sql3 = "lect * from xxxx ";
OracleCommand cmd3 = new OracleCommand(sql3, conn);
OracleDataAdapter oda = new OracleDataAdapter(cmd3);
DataSet ds = new DataSet();
oda.Fill(ds);
myBindingSource.DataSource = ds.Tables[0]; //綁定數據源
this.bindingNavigator1.BindingSource = myBindingSource; //數據源跟bindingNavigator1控件相連
this.dataGridView1.DataSource = myBindingSource; //數據源跟dataGridView1控件相連
this.textBox1.DataBindings.Add("Text", myBindingSource, "IP");//數據源跟textBox1控件相連
}
private void button1_Click(object nder, EventArgs e)
{
myBindingSource.MoveNext();
}
private void button2_Click(object nder, EventArgs e)
{
myBindingSource.MovePrevious();
}
C#中的bindingNavigator控件
表示窗體上綁定到數據的控件的導航和操作用戶界面 (UI)。
命名空間:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)
將 BindingNavigator 控件添加到窗體并綁定到數據源(例如 BindingSource)時,將自動在此表中建立關系。能使用的控件
移到最前
MoveFirstItem
MoveFirst
前移一步
MovePreviousItem
MovePrevious
當前位置
PositionItem
Current
計數
CountItem
Count
移到下一條記錄
MoveNextItem
MoveNext
移到最后
MoveLastItem
MoveLast
新添
AddNewItem
AddNew
刪除
DeleteItem
RemoveCurrent
所以其他無關控件基本都不支持了。
VB.net中,BindingNavigator的定位問題
你的思路完全正確
Dim curpos As Integer
curpos = BindingSource1.Position
BindingSource1.Position = curpos