有没有办法以树形视图的形式显示datagridview行?

时间:2014-02-18 15:46:45

标签: c# winforms datagridview treeview

我正在使用一个C#app加载文本文件,解析字符串并从解析后的值中填充DataGridView。我想在另一个面板中显示DataGridView上的选定行,其格式类似于treeview控件。这甚至可能吗?如果不是树视图控件,我可以使用什么?

编辑:所以有这个代码,但它正在抛出一个NRE:

public void dataGridViewParse_SelectionChanged(object sender, EventArgs e)
    {           

        if (dataGridViewParse.SelectedRows.Count > 0)
        {
            int id = Convert.ToInt32(dataGridViewParse.SelectedRows[0].Cells["Number"].Value.ToString());
            TreeNode passFail = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["PassFail"].Value.ToString());
            TreeNode cluster = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["ClusterNum"].Value.ToString());
            TreeNode manufacturerSpecific = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["ManufacturerSpecific"].Value.ToString());
            TreeNode direction = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["Direction"].Value.ToString());
            TreeNode disableDefaultResponse = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["DisableDefaultResponse"].Value.ToString());
            TreeNode reserved = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["Reserved"].Value.ToString());
            TreeNode manufacturerCode = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["ManufacturerCode"].Value.ToString());
            TreeNode transactionSequenceNum = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["TransactionSequenceNum"].Value.ToString());
            TreeNode commandID = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["CommandID"].Value.ToString());
            TreeNode framePayload = new TreeNode(dataGridViewParse.SelectedRows[0].Cells["FramePayload"].Value.ToString());


            treeView1.Nodes["Number"].Nodes.Add(id.ToString());
            treeView1.Nodes["Pass/Fail"].Nodes.Add(passFail); // throws exception here
            treeView1.Nodes["Cluster"].Nodes.Add(cluster);
            treeView1.Nodes["Manufacturer Specific"].Nodes.Add(manufacturerSpecific);
            treeView1.Nodes["Direction"].Nodes.Add(direction);
            treeView1.Nodes["Default Response"].Nodes.Add(disableDefaultResponse);
            treeView1.Nodes["Reserved"].Nodes.Add(reserved);
            treeView1.Nodes["Manufacturer Code"].Nodes.Add(manufacturerCode);
            treeView1.Nodes["Transaction Sequence Number"].Nodes.Add(transactionSequenceNum);
            treeView1.Nodes["Command Identifier"].Nodes.Add(commandID);
            treeView1.Nodes["Frame Payload"].Nodes.Add(framePayload);
        }
    }

1 个答案:

答案 0 :(得分:0)

是的,有可能。设置广告TreeView,然后当用户点击DataGridViewRow时,通过枚举数据网格行的列来填充树。