Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to expand and contract using DataGridView

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how to use DataGridView to expand and contract, the content is very detailed, interested friends can refer to, hope to be helpful to you.

A lot of data has parent nodes and child nodes, and we want to expand the child node data under the parent node when we click the parent node.

For example, a list of hospital departments, there are father departments and son departments, click on the father department, under the father department can show all the sub-departments under the department.

Let's talk about how to do this in DataGridView.

First, create the sample data:

Sample data SQL

Create table Department (ID int identity (1) not null, DName varchar (20) null, DparentId int null, Dtelphone varchar (20) null, Dhospital varchar (50) null) insert into Department values ('outpatient room', 1 '1111') insert into Department values ('outpatient internal medicine', 1'22'22') insert into Department values ('outpatient surgery', 1'33'33') insert into Department values ('outpatient pediatrics') (1) insert into Department values ('Neurosurgery', 2 '5555') insert into Department values ('Neurosurgery', 2) insert into Department values ('inpatient operation', 2) insert into Department values ('inpatient rehabilitation', 2 '8886' recuperation)

In fact, the idea is very simple, that is, when you expand the parent node and insert a new DataGridViewRow; under the parent node to shrink the parent node, delete the DataGridViewRow of the child node under the parent node.

For simplicity, I hard-coded the data reading in the code directly.

Loading the parent node data, I added two new columns in addition to the columns in the database: IsEx and EX.

Private void DataGridBing (DataTable table) {if (table.Rows.Count > 0) {for (int I = 0; I)

< table.Rows.Count; i++) { int k = this.dataGridView1.Rows.Add(); DataGridViewRow row = this.dataGridView1.Rows[k]; row.Cells["ID"].Value = table.Rows[i]["ID"]; row.Cells["DName"].Value = table.Rows[i]["DName"]; row.Cells["Daddress"].Value = table.Rows[i]["Daddress"]; row.Cells["Dtelphone"].Value = table.Rows[i]["Dtelphone"]; //用于显示该行是否已经展开 row.Cells["IsEx"].Value = "false"; //用于显示展开或收缩符号,为了简单我就直接用字符串了,其实用图片比较美观 row.Cells["EX"].Value = "+"; } } } 下面就是Cell的单击事件了,分别在事件中写展开的插入与收缩的删除. 插入子节点: string isEx=this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value.ToString(); if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx=="false") { string id = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString(); DataTable table = GetDataTable("select * from Department where DparentId="+id); if (table.Rows.Count >

0) {/ / insert row this.dataGridView1.Rows.Insert (e.RowIndex+1, table.Rows.Count); for (int I = 0; I

< table.Rows.Count; i++) { DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex + i+1]; row.DefaultCellStyle.BackColor = Color.CadetBlue; row.Cells["ID"].Value = table.Rows[i]["ID"]; row.Cells["DName"].Value = table.Rows[i]["DName"]; row.Cells["Daddress"].Value = table.Rows[i]["Daddress"]; row.Cells["Dtelphone"].Value = table.Rows[i]["Dtelphone"]; } } //将IsEx设置为true,标明该节点已经展开 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "true"; this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "-"; 删除子节点: if (this.dataGridView1.Columns[e.ColumnIndex].Name == "EX" && isEx == "true") { string id = this.dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString(); DataTable table = GetDataTable("select * from Department where DparentId=" + id); if (table.Rows.Count >

0) {/ / utilize Remove for (int I = 0; I

< table.Rows.Count; i++) { foreach (DataGridViewRow row in this.dataGridView1.Rows) { if (row.Cells["ID"].Value.Equals(table.Rows[i]["ID"])) { this.dataGridView1.Rows.Remove(row); } } } } ////将IsEx设置为false,标明该节点已经收缩 this.dataGridView1.Rows[e.RowIndex].Cells["IsEx"].Value = "false"; this.dataGridView1.Rows[e.RowIndex].Cells["EX"].Value = "+"; } 这里面通过比较ID来***确定一行,循环比较多,因为子节点是紧接着父节点的,我们可以确定子节点所在的行数,所以用RemoveAt()方法更好。 //利用RemoveAt for (int i = table.Rows.Count; i >

0; iMel -) {/ / delete the line this.dataGridView1.Rows.RemoveAt (I + e.RowIndex);}

The above approach is achieved through constant insertions and deletions, but this interaction with the database becomes frequent. It would be better to insert once and then achieve our effect by hiding or showing rows.

To do this, we need to add two new columns to grid:

IsInsert: used to determine whether the row has inserted child node data

RowCount: used to hold the number of child nodes inserted under the row.

In the method DataGridBing, when you bind data, you should add another column:

/ / whether to insert row.Cells ["IsInsert"] .Value = "false"

When adding nodes, we need to make one more judgment: insert data if IsInsert is false, and display data if true.

Expand Lin

If (this.dataGridView1.Columns [e.ColumnIndex] .Name = "EX" & & isEx== "false") {if (this.dataGridView1.Rows [e.RowIndex] .cells ["IsInsert"]. Value.ToString () = "false") {string id = this.dataGridView1. Rows [e.RowIndex] .cells ["ID"]. Value.ToString () DataTable table = GetDataTable ("select * from Department where DparentId=" + id); if (table.Rows.Count > 0) {/ / insert row this.dataGridView1.Rows.Insert (e.RowIndex + 1, table.Rows.Count) For (int I = 0; I < table.Rows.Count; iTunes +) {DataGridViewRow row = this.dataGridView1.Rows [e.RowIndex + I + 1]; row.DefaultCellStyle.BackColor = Color.CadetBlue Row.Cells ["ID"]. Value = table.Rows [I] ["ID"]; row.Cells ["DName"]. Value = table.Rows [I] ["DName"]; row.Cells ["Daddress"]. Value = table.Rows [I] ["Daddress"] Row.Cells ["Dtelphone"]. Value = table.Rows [I] ["Dtelphone"];} this.dataGridView1.Rows [e.RowIndex] .cells ["IsInsert"]. Value = "true"; this.dataGridView1.Rows [e.RowIndex] .cells ["RowCount"]. Value = table.Rows.Count }} else {/ / display data int RowCount = Convert.ToInt32 (this.dataGridView1.Rows [e.RowIndex] .cells ["RowCount"] .value); for (int I = 1; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report