DataGridView 实现,折叠的Tree效果

1、效果

DataGridView 实现,折叠的Tree效果

 

 

 2、代码

public void FrmMain(object sender,EventArgs e)

{

//首先生成数据
this.GenerateData();
//然后生成列
this.GenerateClo();
//显示数据
this.ShowData(this.treeList);

}

/// <summary>
/// 生成数据
/// </summary>
public void GenerateData()
{
treeList = new List<TaskCardModel>();
Logic.Query.EisAmount eisAmount = new Logic.Query.EisAmount();
QueryTaskFlowSheet queryTask = new QueryTaskFlowSheet();

//树的加载数据
TaskCardModel treeItem = null;
TaskFlowSheetModel taskItem = new TaskFlowSheetModel();

//车辆的加载数据
List<AmbulanceTaskInfo> cardList = new List<AmbulanceTaskInfo>();
AmbulanceTaskInfo cardItem = null ;

List<TaskFlowSheetModel> ptTaskList = new List<TaskFlowSheetModel>();
var taskList = tasks.Where(n=>n.PROCKEY=="WC").GroupBy(n => n.EID).ToList();
taskList.ForEach(n =>
{
treeItem = new TaskCardModel();
taskItem = n.FirstOrDefault();
treeItem.AmbId = taskItem.EID;
treeItem.AmbNo = taskItem.ENO;
treeItem.IsExpanded = true;
treeItem.Level = 1;
treeItem.Children = new List<TaskCardModel>();


cardItem = new AmbulanceTaskInfo();
cardItem.AmbId = taskItem.EID;
cardItem.AmbNo = taskItem.ENO;
cardItem.Longitude = taskItem?.Longitude;
cardItem.Latitude = taskItem?.Latitude;
cardItem.NurseName = taskItem.nursename;
cardItem.DriverName = taskItem.drivername;
cardItem.DocName = taskItem.RNAME;
cardItem.PtTaskList = new List<PatientTaskInfo>();
treeList.Add(treeItem);
ptTaskList = tasks.Where(k => k.EID == n.Key).ToList();
if (ptTaskList == null)
{
treeList.Add(treeItem);
return;
}
var taskItems = ptTaskList.GroupBy(j => j.TASKID).ToList();
taskItems.ForEach(j =>
{
var sonTask = j.FirstOrDefault();
if (sonTask == null)
return;

var isWanCheng = tasks.FirstOrDefault(w => w.TASKID == sonTask.TASKID && w.PROCKEY == "WC");
if (isWanCheng == null)
return;

var treeChilden = new TaskCardModel();
var cardChilden = new PatientTaskInfo();

treeChilden.AmbId = sonTask.EID;
treeChilden.AmbNo = sonTask.ENO;
treeChilden.TaskId = sonTask.TASKID;
treeChilden.TaskNo = sonTask.TASKNO;
treeChilden.Yhs = sonTask.RNAME + " - " + sonTask.nursename + " - " + sonTask.drivername;
treeChilden.Hzxx = sonTask.NAME;
treeChilden.JLTime = tasks.Where(k => k.EID == n.Key && k.TASKID == sonTask.TASKID && k.PROCKEY == "JL").FirstOrDefault()?.SENDTIME;
treeChilden.Swyy = sonTask.goalsite == "其它" ? sonTask.siteother : sonTask.goalsite;
treeChilden.Level = 2;
treeChilden.Hzxx = sonTask.NAME;
treeChilden.IsExpanded = false;
treeItem.Children.Add(treeChilden);
treeList.Add(treeChilden);

cardChilden.CurrentState = sonTask.PROCKEY; //重症程度
cardChilden.Pk_TaskId = sonTask.TASKID;
cardChilden.Pk_AmbId = sonTask.EID;
cardChilden.BingQing = sonTask.DID;
cardChilden.PatientAge = sonTask.AGESTR;
cardChilden.PatientName = sonTask.NAME;
cardChilden.PatientSex = sonTask.SEX;
cardChilden.FirstDiag = sonTask.FIRSTDIAG;
cardChilden.ProcList = new List<Entity.Eis_taskproc>();
var wc = tasks.Where(k => k.EID == n.Key && k.TASKID == sonTask.TASKID);
if (wc.Count() != 0)
cardChilden.ProcList.AddRange(ConvertProcs(wc.OrderBy(d => d.PROCTIME).ToList()));
cardItem.PtTaskList.Add(cardChilden);
});
cardList.Add(cardItem);
});
#region 不展开的树
//taskList.ForEach(n =>
//{
// treeItem = new TaskCardModel();
// taskItem = n.FirstOrDefault();
// treeItem.AmbId = taskItem.EID;
// treeItem.AmbNo = taskItem.ENO;
// treeItem.IsExpanded = false;
// treeItem.Level = 1;
// treeItem.Children = new List<TaskCardModel>();


// cardItem = new AmbulanceTaskInfo();
// cardItem.AmbId = taskItem.EID;
// cardItem.AmbNo = taskItem.ENO;
// cardItem.Longitude = taskItem?.Longitude;
// cardItem.Latitude = taskItem?.Latitude;
// cardItem.NurseName = taskItem.nursename;
// cardItem.DriverName = taskItem.drivername;
// cardItem.DocName = taskItem.RNAME;
// cardItem.PtTaskList = new List<PatientTaskInfo>();

// ptTaskList = tasks.Where(k => k.EID == n.Key).ToList();
// if (ptTaskList == null)
// {
// treeList.Add(treeItem);
// cardList.Add(cardItem);
// return;
// }
// var taskItems = ptTaskList.GroupBy(j => j.TASKID).ToList();
// taskItems.ForEach(j =>
// {
// var sonTask = j.FirstOrDefault();
// if (sonTask == null)
// return;

// var isWanCheng = tasks.FirstOrDefault(w => w.TASKID == sonTask.TASKID && w.PROCKEY == "WC");
// if (isWanCheng == null)
// return;

// var treeChilden = new TaskCardModel();
// var cardChilden = new PatientTaskInfo();

// treeChilden.AmbId = sonTask.EID;
// treeChilden.AmbNo = sonTask.ENO;
// treeChilden.TaskId = sonTask.TASKID;
// treeChilden.TaskNo = sonTask.TASKNO;
// treeChilden.Yhs = sonTask.RNAME + " - " + sonTask.nursename + " - " + sonTask.drivername;
// treeChilden.Hzxx = sonTask.NAME;
// treeChilden.JLTime = tasks.Where(k => k.EID == n.Key && k.TASKID == sonTask.TASKID && k.PROCKEY == "JL").FirstOrDefault()?.SENDTIME;
// treeChilden.Swyy = sonTask.goalsite=="其它"?sonTask.siteother:sonTask.goalsite ;
// treeChilden.Level = 2;
// treeChilden.Hzxx = sonTask.NAME;
// treeChilden.IsExpanded = false;
// treeItem.Children.Add(treeChilden);

// cardChilden.CurrentState = sonTask.PROCKEY; //重症程度
// cardChilden.Pk_TaskId = sonTask.TASKID;
// cardChilden.Pk_AmbId = sonTask.EID;
// cardChilden.BingQing = sonTask.DID;
// cardChilden.PatientAge = sonTask.AGESTR;
// cardChilden.PatientName = sonTask.NAME;
// cardChilden.PatientSex = sonTask.SEX;
// cardChilden.FirstDiag = sonTask.FIRSTDIAG;
// cardChilden.ProcList = new List<Entity.Eis_taskproc>();
// var wc = tasks.Where(k => k.EID == n.Key && k.TASKID == sonTask.TASKID);
// if(wc.Count() != 0)
// cardChilden.ProcList.AddRange(ConvertProcs(wc.OrderBy(d=>d.PROCTIME).ToList()));
// cardItem.PtTaskList.Add(cardChilden);
// });
// treeList.Add(treeItem);
// cardList.Add(cardItem);
//});

//var daiMing = Logic.LogicVar.AmbulanceTaskList.Where(n => n.PtTaskList == null || n.PtTaskList.Count == 0);
//if (daiMing.Count() != 0)
//cardList.AddRange(daiMing.ToList());
//ucMap.InitData(cardList);
#endregion
ucMap.ambList = cardList;
}

 

/// <summary>
/// 生成列
/// </summary>
public void GenerateClo()
{
this.dataGridView1.ReadOnly = false;
this.dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.AllowUserToOrderColumns = true;
this.dataGridView1.AllowUserToResizeRows = false;
this.dataGridView1.AllowUserToResizeColumns = true;

#region 影响性能
this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;
//this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;

#endregion

this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
this.dataGridView1.RowHeadersVisible = false;
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.dataGridView1.MultiSelect = false;
this.dataGridView1.VirtualMode = true;

this.dataGridView1.CellValueNeeded -= dataGridView1_CellValueNeeded;
this.dataGridView1.CellValueNeeded += dataGridView1_CellValueNeeded;

this.dataGridView1.Columns.Clear();

int colWidth = 50;

DataGridViewTextBoxColumn colId = new DataGridViewTextBoxColumn();
colId.Name = "colId";
colId.HeaderText = "图标";
colId.Width =40;

DataGridViewTextBoxColumn ambNo = new DataGridViewTextBoxColumn();
ambNo.Name = "AmbNo";
ambNo.HeaderText = "车辆号";
//cardId.FillWeight = 1;

DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
name.Name = "Hzxx";
name.HeaderText = "患者姓名";

DataGridViewTextBoxColumn taskNo = new DataGridViewTextBoxColumn();
taskNo.Name = "TaskNo";
taskNo.HeaderText = "任务号";
//taskId.FillWeight = 4;


DataGridViewTextBoxColumn yhs = new DataGridViewTextBoxColumn();
yhs.Name = "Yhs";
yhs.HeaderText = "医护司";
//yhs.FillWeight = 1;


DataGridViewTextBoxColumn jlTime = new DataGridViewTextBoxColumn();
jlTime.Name = "JLTime";
jlTime.HeaderText = "接令时间";
//jlTime.FillWeight = 1;


DataGridViewTextBoxColumn swyy = new DataGridViewTextBoxColumn();
swyy.Name = "Swyy";
swyy.HeaderText = "送往医院";
//swyy.FillWeight = 1;


//name.FillWeight = 1;

DataGridViewTextBoxColumn amdId = new DataGridViewTextBoxColumn();
amdId.Name = "AmbId";
amdId.HeaderText = "车辆Id";
amdId.MinimumWidth = colWidth;

DataGridViewTextBoxColumn taskId = new DataGridViewTextBoxColumn();
taskId.Name = "TaskId";
taskId.HeaderText = "任务Id";
taskId.MinimumWidth = colWidth;

DataGridViewTextBoxColumn level = new DataGridViewTextBoxColumn();
level.Name = "Level";
level.HeaderText = "层级";
level.MinimumWidth = colWidth;

this.dataGridView1.Columns.Add(colId);
this.dataGridView1.Columns.Add(ambNo);
this.dataGridView1.Columns.Add(name);
this.dataGridView1.Columns.Add(taskNo);
this.dataGridView1.Columns.Add(yhs);
this.dataGridView1.Columns.Add(jlTime);
this.dataGridView1.Columns.Add(swyy);
this.dataGridView1.Columns.Add(amdId);
this.dataGridView1.Columns.Add(taskId);
this.dataGridView1.Columns.Add(level);

this.dataGridView1.Columns[7].Visible = false;
this.dataGridView1.Columns[8].Visible = false;
this.dataGridView1.Columns[9].Visible = false;

foreach (DataGridViewColumn col in this.dataGridView1.Columns)
{
col.ReadOnly = true;
}

dataGridView1.Font = new Font("宋体", 12, FontStyle.Regular);
this.dataGridView1.CellMouseClick += dataGridView1_CellMouseClick;
}

 

/// <summary>
/// 显示数据
/// </summary>
public void ShowData(List<TaskCardModel> list)
{
int recordCount = list.Count;
this.dataGridView1.Rows.Clear();
this.dataGridView1.RowCount = recordCount;
}

/// <summary>
/// 列表的点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex < 0)
return;
if (dataGridView1.Columns[e.ColumnIndex].Name == "colId")
{
if (this.treeList[e.RowIndex].Children != null && this.treeList[e.RowIndex].Children.Count > 0)
{
if (this.treeList[e.RowIndex].IsExpanded)
{
this.RecursiveRemove(this.treeList, e.RowIndex);
this.treeList[e.RowIndex].IsExpanded = false;
this.ShowData(this.treeList);
}
else
{
this.treeList.InsertRange(e.RowIndex + 1, this.treeList[e.RowIndex].Children);
this.treeList[e.RowIndex].IsExpanded = true;
this.ShowData(this.treeList);
}

this.dataGridView1.Rows[0].Selected = false;
this.dataGridView1.Rows[e.RowIndex].Selected = true;
}
}
//消除上一次选种的车的轨迹


//点击的是任务列表,动态播放车辆行驶轨迹
if (dataGridView1.Rows[e.RowIndex].Cells["TaskNo"].Value !=null)
{
var ambId = dataGridView1.Rows[e.RowIndex].Cells["AmbId"].Value.ToString();
var taskId = dataGridView1.Rows[e.RowIndex].Cells["TaskId"].Value.ToString();
//var line = new PatientTaskInfo();
//line.Pk_AmbId = "1912101621010001ASYM";
//line.BillTime = "2020-01-17 17:34:51";
ucMap.showGL(true);
ucMap.mapMainAmbId = ambId;
ucMap.mapMainTaskId = taskId;
ucMap.DrawAmbLine(null);
ucMap.UpdateCardImg();
}
else
{
ucMap.showGL(false);
ucMap.clearRoute();
ucMap.mapMainAmbId = "";
ucMap.mapMainTaskId = "";
}
}

/// <summary>
/// 递归移除
/// </summary>
/// <param name="lst"></param>
/// <param name="index"></param>
/// <param name="count"></param>
public void RecursiveRemove(List<TaskCardModel> lst, int index)
{
int count = GetAllExpandChildCount(lst[index]);
lst.RemoveRange(index + 1, count);
}

public int GetAllExpandChildCount(TaskCardModel r)
{
if (r.Children != null && r.Children.Count > 0)
{
int cnt = 0;
if (r.IsExpanded)
{
cnt = r.Children.Count;
for (int i = 0; i < r.Children.Count; i++)
{
cnt += GetAllExpandChildCount(r.Children[i]);
}
}
r.IsExpanded = false;
return cnt;
}
else
{
return 0;
}
}
private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
if (e.RowIndex >= this.dataGridView1.RowCount || e.RowIndex > this.treeList.Count)
return;
try
{
//任务号、车辆号、医护司、接令时间、送往医院、患者姓名
TaskCardModel dict = this.treeList[e.RowIndex];
object[] values = new object[] { null, dict.AmbNo, dict.Hzxx, dict.TaskNo,dict.Yhs,dict.JLTime,dict.Swyy,dict.AmbId,dict.TaskId, dict.Level };
e.Value = values[e.ColumnIndex];
dataGridView1.Rows[e.RowIndex].Tag = dict;
if (e.ColumnIndex == 0)
{
if (dict.Children != null && dict.Children.Count > 0)
{
if(dict.IsExpanded)
e.Value = "[-]";
else
e.Value = "[+]";
}
else
{
e.Value = "";
}
}
if (e.ColumnIndex == 1)
{
if (dict.Children == null && dict.Children?.Count == 0)
e.Value = " " + values[e.ColumnIndex];
}
}
catch
{

}
}

 

DataGridView 实现,折叠的Tree效果

上一篇:C#高级编程第11版 - 附赠第二章


下一篇:在AAD 注册 API permission