datagridviewcombocell和accdb [英] datagridviewcombocell and accdb

查看:70
本文介绍了datagridviewcombocell和accdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview..where有七列..其中两个是r组合单元格...其他是文本单元格...我被要求从accdb表填充网格..但是我不能这样做...请帮助我

解决方案

您可以编写类似下面的内容来从accdb和绑定datagridview中检索数据。



  string  connectionString =  @  Provider = Microsoft.ACE.OLEDB.12.0; Data Source = F:\YourDB.accdb; 
string sql = SELECT * FROM TableName ;
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(sql,con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
YourDataGridviewId.DataSource = dt;





现在为了向组合单元添加值,你可以写一些类似的东西 -

 DataGridViewComboBoxCell cbCell =(DataGridViewComboBoxCell)myDataGrid.Rows [ 0 ]。单元格[ 0 ]; 
cbCell.DataSource = dataSourceForCombobox; // 在此之前获取组合框的数据源
cbCell.ValueMember = ValueField;
cbCell.DisplayMember = DisplayField;



您可以使用 OnItemDataBound ,将其应用于所有行。



希望,它有助于:)


i have a datagridview..where there are seven columns ..in which two of them r combo cell ...and others are text cell...i have been asked to populate the grid from an accdb table ..but i cannot do so ...kindly help me out

解决方案

You can code something like following to retrive data from accdb and binding datagridview.

string connectionString= @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\YourDB.accdb";
string sql = "SELECT * FROM TableName";
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt= new DataTable();
da.Fill(dt);
YourDataGridviewId.DataSource = dt;



Now for adding values to comboboxcells you can write something like-

DataGridViewComboBoxCell cbCell = (DataGridViewComboBoxCell)myDataGrid.Rows[0].Cells[0];
cbCell.DataSource = dataSourceForCombobox; //get the datasource for combobox prior to this
cbCell.ValueMember = "ValueField";
cbCell.DisplayMember = "DisplayField";


You can make use of OnItemDataBound, to apply this for all the rows.

Hope, it helps :)


这篇关于datagridviewcombocell和accdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆