我试图从TreeView控件控制一个datagridview,也从VB转换。在这两行中遇到了一个错误:
sRef = lWADataBaseDataSet.orderTBL.Rows[I].Item[0];
sOrder = lWADataBaseDataSet.orderTBL.Rows[I].Item[1];
本教程中的代码是(包含我的数据库信息):
if treeView1.SelectedNode is nothing then
orderTBLDataGridView.DataSource = nothing
exit sub
end if
if treeview1.SelectedNod.Name = "root" then
orderTBLDataGridView.DataSource = nothing
exit sub
end if
dim sRef as string
sRef = treeview1.SelectedNode.name
orderTBLDataGridView.DataSource = lWADataBaseDataSet.orderTBL("id=" & id);
所有代码:
public void ordersForm_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'lWADataBaseDataSet.orderTBL' table. You can move, or remove it, as needed.
this.orderTBLTableAdapter.Fill(this.lWADataBaseDataSet.orderTBL);
string sRef = null;
string sOrder = null;
int I = 0;
for (I = 0; (I
<= (lWADataBaseDataSet.orderTBL.Count - 1)); I++)
{
sRef = lWADataBaseDataSet.orderTBL.Rows[I].Item[0];
sOrder = lWADataBaseDataSet.orderTBL.Rows[I].Item[1];
treeView1.Nodes[0].Nodes.Add(sRef, sOrder);
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
if ((treeView1.SelectedNode == null)) {
orderTBLDataGridView.DataSource = null;
return;
}
if ((treeView1.SelectedNode.Name == "root")) {
orderTBLDataGridView.DataSource = null;
return;
}
string sRef;
sRef = treeView1.SelectedNode.Name;
orderTBLDataGridView.DataSource = lWADataBaseDataSet.orderTBL.Select(("Reference=" + sRef));
}
您可以简单地这样调用这些项:
sRef = lWADataBaseDataSet.orderTBL.Rows[I][0];
sOrder = lWADataBaseDataSet.orderTBL.Rows[I][1];
不需要在VB中声明项,因为在c# DataRow中。Items本身就是一个索引器
使用字段扩展方法:
sRef = lWADataBaseDataSet.orderTBL.Rows[I].Field<string>(0);
sOrder = lWADataBaseDataSet.orderTBL.Rows[I].Field<string>(1);