How To Select DataGridView Values Into TreeView Nodes Using C#
In this tutorial, we will learn How To Select DataGridView Values Into TreeView Nodes Using C# and TreeView Node Using For Loop And Datatable On Button Click Event.
Source code
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { DataTable table = new DataTable(); // add columns to datatable table.Columns.Add("Id", typeof(int)); table.Columns.Add("First Name", typeof(string)); table.Columns.Add("Last Name", typeof(string)); table.Columns.Add("Age", typeof(int)); // add rows to datatable table.Rows.Add(1, "Abhishek ", "kumar", 10); table.Rows.Add(2, "Sima", "Kumari", 20); table.Rows.Add(3, "Soni ", "kumari", 30); table.Rows.Add(4, "Puja", "kumari", 40); table.Rows.Add(5, "Rima", "kumari", 50); table.Rows.Add(6, "Sona", "kumari", 60); table.Rows.Add(7, "Soni", "kumari", 70); table.Rows.Add(8, "Akash", "kumari", 80); dataGridView1.DataSource = table; } private void button1_Click(object sender, EventArgs e) { for (int i = 0; i < dataGridView1.Rows.Count; i++) { int rowIndex = i + 1; TreeNode node = new TreeNode("Row_" + rowIndex); for (int j = 0; j < dataGridView1.Columns.Count; j++) { node.Nodes.Add(dataGridView1.Rows[i].Cells[j].Value.ToString()); } treeView1.Nodes.Add(node); } } } }
Post A Comment:
0 comments: