实现效果
在年级列表里面有个单元格,里面是一个学生列表按钮,点击按钮之后弹出一个列表,显示对应的学生列表!
步骤1:
创建弹出的学生列表,学生列表是一个UserControl
添加一个GridControl,并设置表格的表头和数据,完成后的页面如下
代码:
public void InitGrid(int classId)
{
List<Student> studentList = new List<Student>();
if (classId == 1)
{
Student student1 = new Student()
{
Id = 1,
Name = "一号",
Age = 10,
ClassId = 1,
Address = "China",
PhoneNumber = "13667656789"
};
Student student2 = new Student()
{
Id = 2,
Name = "二号",
Age = 10,
ClassId = 1,
Address = "China",
PhoneNumber = "13667656780"
};
Student student3 = new Student()
{
Id = 3,
Name = "三号",
Age = 10,
ClassId = 1,
Address = "China",
PhoneNumber = "13667656089"
};
studentList.Add(student1);
studentList.Add(student2);
studentList.Add(student3);
}
if (classId == 2)
{
Student student4 = new Student()
{
Id = 4,
Name = "四号",
Age = 10,
ClassId = 1,
Address = "China",
PhoneNumber = "13367656789"
};
Student student5 = new Student()
{
Id = 5,
Name = "五号",
Age = 10,
ClassId = 1,
Address = "China",
PhoneNumber = "13647656780"
};
Student student6 = new Student()
{
Id = 6,
Name = "六号",
Age = 10,
ClassId = 1,
Address = "China",
PhoneNumber = "18667656089"
};
studentList.Add(student4);
studentList.Add(student5);
studentList.Add(student6);
}
this.gridControl1.DataSource = studentList;
}
步骤2:创建主页面
添加GridControl,并设置表头和数据
DataTable tblDatas = new DataTable("ClassesInfo");
DataColumn dc = null;
dc = tblDatas.Columns.Add("Id", Type.GetType("System.Int32"));
dc = tblDatas.Columns.Add("ClassName", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("Floor", Type.GetType("System.String"));
dc = tblDatas.Columns.Add("TeacherName", Type.GetType("System.String"));
DataRow newRow1;
newRow1 = tblDatas.NewRow();
newRow1["Id"] = 1;
newRow1["ClassName"] = "班级1";
newRow1["Floor"] = "一楼";
newRow1["TeacherName"] = "王老师";
tblDatas.Rows.Add(newRow1);
DataRow newRow2;
newRow2 = tblDatas.NewRow();
newRow2["Id"] = 2;
newRow2["ClassName"] = "班级2";
newRow2["Floor"] = "二楼";
newRow2["TeacherName"] = "李老师";
tblDatas.Rows.Add(newRow2);
this.gridControl1.DataSource = tblDatas;
步骤3:添加学生列表列的按钮
将PopupContainerEdit的Name改为StudentListPopupContainer
步骤4:将StudentListUserControl控件添加到主页面
重新编译类库,在工具箱中可以找到
添加一个PopupContainerControl到主页面中,再将StudentListUserControl添加到PopupContainerControl中,
并将StudentListUserControl的Dock设置为Fill
显示效果如下
步骤5:将PopupContainerControl绑定到PopupContainerEdit中
并将TextEditStyle设置为HideTextEditor
设置Button的样式和文字
步骤6:给PopupContainerEdit设置点击事件,给StudentListUserControl传值
private void StudentListPopupContainer_Click(object sender, EventArgs e)
{
try
{
DataRow dr = this.gridView1.GetFocusedDataRow();
int classId = dr["Id"]!=null?int.Parse(dr["Id"].ToString()):0;
if (classId != 0)
{
studentListUserControl1.InitGrid(classId);
}
}
catch (Exception ex)
{
Logger.Error(ex);
XtraMessageBox.Show(ex.Message);
}
}
步骤7:也是最最重要的一步:
将GridView的Editable属性设置为True,将GridView的Editable属性设置为True,将GridView的Editable属性设置为True
否则点击Button会没有任何反应