c# 中datatable dataview List 在数据绑定时 比较下?
- 提问者网友:黑米和小志
- 2021-02-11 16:39
- 五星知识达人网友:一叶十三刺
- 2021-02-11 18:13
dataTable 看成一个表的话,dataView 就是一张视图;
在视图里 可以排序,筛选,等操作数据比在DataTable 里面方便多.而且可以直接转换成DataTable;
List 集合类 一般做小型控件的绑定;datatable 和 dataview 做表格的绑定
至于代码 在 MSDN上有详细的介绍;
- 1楼网友:一袍清酒付
- 2021-02-11 18:46
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.data.sqlclient;
namespace datagridviewdemo { public partial class form1 : form { public form1() { initializecomponent(); }
private void form1_load(object sender, eventargs e) { string conn = "data source=b13;initial catalog=myschool;user id=student"; sqlconnection connectin = new sqlconnection(conn); connectin.open(); string sql = "select teacherid, loginid,sex from teacher";
//把数据填充到dataset中 dataset ds = new dataset(); sqldataadapter adpater = new sqldataadapter(sql,connectin); adpater.fill(ds,"teacher"); //关联datagridfview的数据源为前面得到的dataset datagridview1.datasource = ds.tables["teacher"];
//下面是像下拉框添加
combobox1.datasource = ds.tables["teacher"]; combobox1.valuemember = "teacherid"; combobox1.displaymember = "loginid";
}
private void button1_click(object sender, eventargs e) { messagebox.show(combobox1.selectedvalue.tostring()); } } }
我这正好有个datagridview例子