Datagridview拖放数据到TreeView
通過Datagridview控件可以顯示數據庫記錄
sender:表示支持.net framework類層次結構中所有類的基類對象.
e:表示為mousedown 事件提供數據.
private void treeview1_mouseenter(object sender,eventargs e)
拖放可以選定單行或者多行.
1.選定一條在控件中默認狀態下行標題選定.按ctrl 選定記錄,放在控件上.
2.選多條記錄匯總任意一個端點記錄,按shift選中另一端記錄.拖放到控件上.
代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace DateToTreeView
{
? ? public partial class Frm_Main : Form
? ? {
? ? ? ? public Frm_Main()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? //聲明本程序需要的變量
? ? ? ? public static string[,] recordInfo;
? ? ? ? //窗體加載時,顯示原有的數據
? ? ? ? private void Form1_Load(object sender,EventArgs e)
? ? ? ? {
? ? ? ? ? ? string P_Connection = string.Format(//創建數據庫連接字符串
? ? ? ? ? ? ?"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;User Id=Admin");
? ? ? ? ? ? OleDbDataAdapter P_OLeDbDataAdapter = new OleDbDataAdapter(
? ? ? ? ? ? ? ? "select au_id as 用戶編號,au_lname as 用戶名,phone as 聯系電話 ?from authors",
? ? ? ? ? ? ? ? P_Connection);
? ? ? ? ? ? DataSet ds = new DataSet();
? ? ? ? ? ? P_OLeDbDataAdapter.Fill(ds,"UserInfo");
? ? ? ? ? ? dataGridView1.DataSource = ds.Tables["UserInfo"].DefaultView;
? ? ? ? ? ? TreeNode treeNode = new TreeNode("用戶信息",0,0);
? ? ? ? ? ? treeView1.Nodes.Add(treeNode);
? ? ? ? ? ? //默認情況下追加節點
? ? ? ? ? ? 追加節點ToolStripMenuItem.Checked = true;
? ? ? ? }
? ? ? ? //DataGridView的按下鼠標事件
? ? ? ? private void dataGridView1_MouseDown(object sender,MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? if(dataGridView1.SelectedCells.Count != 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //定義一個二維數組,數組中的每一行代表DataGridView中的一條記錄
? ? ? ? ? ? ? ? recordInfo = new string[dataGridView1.Rows.Count,dataGridView1.Columns.Count];
? ? ? ? ? ? ? ? //當按下鼠標左鍵時,首先獲取選定行,記錄每一行對應的信息
? ? ? ? ? ? ? ? for(int i = 0; i < dataGridView1.Rows.Count; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if(dataGridView1.Rows[i].Selected)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? for(int j = 0; j < dataGridView1.Columns.Count; j++)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? recordInfo[i,j] = dataGridView1.Rows[i].Cells[j].Value.ToString();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //當鼠標進入TreeView控件時,觸發的操作
? ? ? ? private void treeView1_MouseEnter(object sender,EventArgs e)
? ? ? ? {
? ? ? ? ? ? if(追加節點ToolStripMenuItem.Checked == true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? #region 代碼區域
? ? ? ? ? ? ? ? if(recordInfo != null && recordInfo.Length != 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? //用雙重for循環遍歷數組recordInfo中的內容
? ? ? ? ? ? ? ? ? ? for(int i = 0; i < recordInfo.GetLength(0); i++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? for(int j = 0; j < recordInfo.GetLength(1); j++)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? //判斷數組中的值是否為空
? ? ? ? ? ? ? ? ? ? ? ? ? ? if(recordInfo[i,j] != null)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(j == 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //向TreeView中加入節點
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TreeNode Node1 = new TreeNode(recordInfo[i,j].ToString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode.Nodes.Add(Node1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode = Node1;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加子級節點下的子節點
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TreeNode Node2 = new TreeNode(recordInfo[i,j].ToString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode.Nodes.Add(Node2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode = treeView1.Nodes[0];
? ? ? ? ? ? ? ? ? ? ? ? treeView1.ExpandAll();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //清空recordInfo中的記錄
? ? ? ? ? ? ? ? ? ? for(int m = 0; m < recordInfo.GetLength(0); m++)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? for(int n = 0; n < recordInfo.GetLength(1); n++)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? recordInfo[m,n] = null;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? #endregion
? ? ? ? ? ? }
? ? ? ? ? ? if(清空內容ToolStripMenuItem.Checked == true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(treeView1.SelectedNode.Nodes.Count != 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode.Remove();
? ? ? ? ? ? ? ? ? ? TreeNode treeNode = new TreeNode("用戶信息",0,0);
? ? ? ? ? ? ? ? ? ? treeView1.Nodes.Add(treeNode);
? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode = treeNode;
? ? ? ? ? ? ? ? ? ? #region 代碼區域
? ? ? ? ? ? ? ? ? ? if(recordInfo != null && recordInfo.Length != 0)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? //用雙重for循環遍歷數組recordInfo中的內容
? ? ? ? ? ? ? ? ? ? ? ? for(int i = 0; i < recordInfo.GetLength(0); i++)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int j = 0; j < recordInfo.GetLength(1); j++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //判斷數組中的值是否為空
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(recordInfo[i,j] != null)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if(j == 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //向TreeView中加入節點
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TreeNode Node1 = new TreeNode(recordInfo[i,j].ToString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode.Nodes.Add(Node1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode = Node1;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //添加子級節點下的子節點
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TreeNode Node2 = new TreeNode(recordInfo[i,j].ToString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode.Nodes.Add(Node2);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? treeView1.SelectedNode = treeView1.Nodes[0];
? ? ? ? ? ? ? ? ? ? ? ? ? ? treeView1.ExpandAll();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? //清空recordInfo中的記錄
? ? ? ? ? ? ? ? ? ? ? ? for(int m = 0; m < recordInfo.GetLength(0); m++)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for(int n = 0; n < recordInfo.GetLength(1); n++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? recordInfo[m,n] = null;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? #endregion
? ? ? ? ? ? ? ? ? ? 追加節點ToolStripMenuItem.Checked = true;
? ? ? ? ? ? ? ? ? ? 清空內容ToolStripMenuItem.Checked = false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? #region 默認項的設置?codego.net/tags/1/1/
? ? ? ? private void 清空內容ToolStripMenuItem_Click(object sender,EventArgs e)
? ? ? ? {
? ? ? ? ? ? if(追加節點ToolStripMenuItem.Checked == true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? 清空內容ToolStripMenuItem.Checked = true;
? ? ? ? ? ? ? ? 追加節點ToolStripMenuItem.Checked = false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? private void 追加節點ToolStripMenuItem_Click(object sender,EventArgs e)
? ? ? ? {
? ? ? ? ? ? if(清空內容ToolStripMenuItem.Checked == true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? 追加節點ToolStripMenuItem.Checked = true;
? ? ? ? ? ? ? ? 清空內容ToolStripMenuItem.Checked = false;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? #endregion
? ? }
}
轉載于:https://blog.51cto.com/9521583/1571918
總結
以上是生活随笔為你收集整理的Datagridview拖放数据到TreeView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c/c++如何获取数组的长度
- 下一篇: centos6中三台物理机配置nginx