如何在datagrid视图中使用datatable [英] how to use datatable with datagrid view
本文介绍了如何在datagrid视图中使用datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有预定义的网格视图,其中包含两列(colEmpCode,colEmpName),这些列名称由对象属性分配
这是我的代码
I have predefined grid view it''s contain two column (colEmpCode,colEmpName) those column name are assign by object properties
this is my code
OdbcCommand OpenEmp = new OdbcCommand("SELECT EMPLOYEE_GROUP_ASSIGN.EmpNo, EMPLOYEE_PERSONAL.FullName FROM " +
"EMPLOYEE_GROUP_ASSIGN INNER JOIN EMPLOYEE_PERSONAL ON EMPLOYEE_GROUP_ASSIGN.EmpNo = EMPLOYEE_PERSONAL.EmpId " +
"WHERE (EMPLOYEE_GROUP_ASSIGN.EmpGroupCode = '" + cmbEmpGrup.Text.Trim().ToString() + "')", MainMod.Conn);
OdbcDataReader readEmp = OpenEmp.ExecuteReader();
DataTable table = new DataTable();
table.Columns.Add("EmpNo", typeof(string));
table.Columns.Add("FullName", typeof(string));
while (readEmp.Read())
{
table.Rows.Add(MainMod.NullableString(readEmp.GetValue(0).ToString()),MainMod.NullableString(readEmp.GetValue(1).ToString()));
}
readEmp.Dispose();
gvEmployee.DataSource = table;
当我调用(gvEmployee.DataSource = table)时,它将给我记录到新列中
在数据网格视图中
但我想将记录添加到这样的预定义列中
when i call (gvEmployee.DataSource = table) it give me record to new columns
in data grid view
but i want to add the records to my predefined columns like this
gvEmployee.columns[0].value = table.columns[0].value // this is what i expting
gvEmployee.columns[1].value = table.columns[1].value
如何实现目标?
请帮助我
how to achieve the target ?
Please help me
推荐答案
与datatable绑定后,您需要重命名data-grid的列名
You need to rename the column name of data-grid after bind with datatable
gvEmployee.columns[0].HeaderText= "column name"
这是不对的,
it is not right,
gvEmployee.columns[0].value = table.columns[0].value
应该是
it should be,
gvEmployee.Columns[0].Datapropertyname = "EmpId";
// or
//gvEmployee.Columns[0].Datapropertyname = table.columns[0].Name.ToString();
祝您编程愉快!
:)
Happy coding!
:)
If you want to manually fill datagridview I suggest to use strong typed datatables and rows from Dataset. In this case you don't have to worry about column names.
dapfor. com
dapfor. com
这篇关于如何在datagrid视图中使用datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文