的System.Data.DataRow'不包含'项目'的定义

时间:2014-05-28 18:50:16

标签: c# vb.net datagridview treeview

我正在尝试从TreeView控件控制datagridview,并且还要从VB.Net转换为C#并且在这两行上遇到错误:

sRef = lWADataBaseDataSet.orderTBL.Rows[I].Item[0];
sOrder = lWADataBaseDataSet.orderTBL.Rows[I].Item[1];

来自this tutorial的代码(包含我的数据库信息):

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));

        }

2 个答案:

答案 0 :(得分:1)

您可以简单地调用这些项目:

sRef = lWADataBaseDataSet.orderTBL.Rows[I][0];
sOrder = lWADataBaseDataSet.orderTBL.Rows[I][1];

无需在VB中声明项目,因为C#DataRow.Items本身就是索引器。

答案 1 :(得分:0)

使用字段扩展方法:

sRef = lWADataBaseDataSet.orderTBL.Rows[I].Field<string>(0);
sOrder = lWADataBaseDataSet.orderTBL.Rows[I].Field<string>(1);