C# Learning Summary (4) - DataGridView and ContextMenuStrip

I'm tired of memorizing English, so I can do C# homework. The general function is to display the list of students, and then use the function of the right mouse button menu to maintain this list. Some simple things will not be repeated. If C# has no foundation, it is normal to not understand this blog. The
general functions are as follows, and the database can also be changed. I will not focus on the database here:
write picture description here


DataGridView

Display list of controls
1. Add controls and drag them
directly from the toolbox

2. Add data to the control

dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = StudentManage.getStudentList();
//StudentManage.getStudentList()得到一个学生类的集合

3. Display data

Go to the View Designer, right-click on the control - Edit Columns, as shown in the figure below,
write picture description here
add four columns as shown in the figure above, and the DataPropertyName property of each column corresponds to the property name of the student class.

  • It should be noted here that set and get methods must be added to each attribute in the student class, otherwise it will not be displayed! ! ! ! !

ContextMenuStrip

Right mouse button
1. Add a control, drag the ContextMenuStrip to the bottom of the view designer or double-click the ContextMenuStrip in the toolbox to add
write picture description here


2. Add events Double-click
on each item to add a click event.


Summary: It looks complicated, but it's actually very simple. In terms of the difficulty of using properties alone, it is much easier to determine than Java's swing. After the right click triggers the click event, get the data of which student is currently selected in the student list (note the property of CurrentRow), and then you can operate separately.


There are many files, and the code is not passed on. If you are interested, you can leave me a message.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325987373&siteId=291194637