我想用DataGridView中的數據填充TreeView,但我不能。從c中的DataGridView填充TreeView與數據#
DataGridView的是以下信息:
TreeView控件必須象下面這樣:
OBS:我創建了一個TreeView手動試圖解釋我是如何希望它是。
我試着這樣做:
public void CarregaTreeView() {
string pai = "";
string filho = "";
string noPrincipal = "";
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>();
noPrincipal = dgv.Rows[0].Cells["COD_PAI"].Value.ToString();
foreach (DataGridViewRow row in dgv.Rows) {
pai = (string)row.Cells[0].Value;
filho = (string)row.Cells[3].Value;
if (dict.ContainsKey(pai)) {
dict[pai].Add(filho);
}
else {
dict.Add(pai, new List<string>());
dict[pai].Add(filho);
}
}
//adicionar nó principal
TreeNode objTopNode = new TreeNode(noPrincipal + " - DESCRICAO - [QTDE]");
tv.Nodes.Add(objTopNode);
objTopNode.Tag = noPrincipal;
MontaTreeView(dict, objTopNode);
}
private void MontaTreeView(Dictionary<string, List<string>> dict, TreeNode objparentNode) {
foreach (var kvp in dict) {
objcurrentNode = new TreeNode(kvp.Key + " - DESCRICAO - [QTDE]");
objparentNode.Nodes.Add(objcurrentNode);
objcurrentNode.Tag = kvp.Key;
objcurrentNode.Expand();
if (kvp.Key.Contains("T")) {
Dictionary<string, List<string>> dict1 = new Dictionary<string, List<string>>();
dict1.Add(kvp.Key, new List<string>());
dict1[kvp.Key].Add(dict[kvp.Key][0]);
MontaTreeView(dict1, objcurrentNode);
}
}
}
這是贏的形式嗎? –
@Marcelo對不起,但在目前的形式中,這個問題很難回答。您是否可以移除不相關代碼的額外部分,將您的數據顯示爲文本,而不是圖像,並向我們展示您從代碼輸出的結果,以及哪些部分在那裏不起作用? – trailmax
@Marcelo很多時候,當你試圖簡化問題時,你自己實際得到你的答案。所以這是值得的 – trailmax