bindingnavigator1,C#中的bindingNavigator控件
本文目录索引
- 1,C#中的bindingNavigator控件
- 2,C#中的bindingNavigator控件
- 3,c#中bindingNavigator的用法是什么,求示范……
- 4,关于C#中BindingNavigatorSaveItem_Click事件中的代码。
- 5,C#怎么直接导入datagridview数据到access数据库?
1,C#中的bindingNavigator控件
BindingSource的使用
BindingSource bs = new BindingSource();//声明绑定类
using (SqlConnection conn = new SqlConnection(strConnecion))
{
try
{
conn.Open();//打开联结
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "select * 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.Close();
}
2,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
所以其他无关控件基本都不支持了。
3,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;User ID=xxx;Password=xxx");
private void Form1_Load(object sender, EventArgs e)
{
string sql3 = "select * 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 sender, EventArgs e)
{
myBindingSource.MoveNext();
}
private void button2_Click(object sender, EventArgs e)
{
myBindingSource.MovePrevious();
}
4,关于C#中BindingNavigatorSaveItem_Click事件中的代码。
首先bt.Parent = f1;最好改成f1.Controls.Add(bt);
然后,事件注册的通用写法是
产生事件的对象.事件名+=方法名;
其中方法的定义要类似如下:
方法名(object sender, XXXEventArgs e)
如果你不清楚具体的XXXEventArgs应该是怎么写,比如,上面的button你不知道怎么写,那就拖一个button到界面,双击看看人家是怎么写的。
5,C#怎么直接导入datagridview数据到access数据库?
简单说明:
1 建立连接,因为是access数据库,所以建立的是OleDbConnection。
2 建立适配器OleDbDataAdapter da,目的是将数据库里的表fill到dt中,这就不用fill了。
3 dt,说白了就是一张表,当然有dt.Rows.Count行数啊。
4 dataGridView的成员DataSource就是一个DataTable,指明一下就ok了。
5 记得关闭连接Close、Dispose和关闭适配器da.Dispose();
using System.Data.OleDb;
using System.Data.SqlClient;
string strFilePath = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" + Application.StartupPath + "\\111.accdb";
string sql = "select * from 111";
//声明一个数据连接
System.Data.OleDb.OleDbConnection con = new OleDbConnection(strFilePath);
System.Data.OleDb.OleDbDataAdapter da = new OleDbDataAdapter(sql, con);
DataTable dt = new DataTable();
try
{
da.Fill(dt);
if (dt.Rows.Count >= 1)
{
dataGridView1.DataSource = dt;
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
con.Close();
con.Dispose();
da.Dispose();
}
简单总结:
涉及到的类:
1 Application,它的StartupPath即debug文件
2 OleDbConnection
3 OleDbDataAdapter:肯定会需要的con和sql语句参数都用到了。
4 DataTable,表格了,它的行数.Rows.Count
5 DataGridView,这里只做显示用,应该还能在其上进行操作
6 Exception,常用的方法是:ex.ToString());