DataGridView Control Usage (ii): Add "edit" to the last record for each line

Source: Internet
Author: User
Tags generator
1. The list data is already displayed in the DataGridView Control usage (i), where we need to edit each row of data records and add hyperlinks such as edit, delete, and view. The code is as follows:


View Source
Print?
1//Add edit columns for each row of data.
2//Set column can not be automatically made
3 Userdatagridview.autogeneratecolumns = false;
4//Create a datagridviewlinkcolumn column
5 Datagridviewlinkcolumn DLink = new Datagridviewlinkcolumn ();
6 DLink. Text = "edit"; the display text of this column that is added, that is, the text displayed in the last column of each line.
7 DLink. Name = "Linkedit";
8 DLink. HeaderText = "edit";//Column title
9 DLink. Usecolumntextforlinkvalue = true;//the DLink set above. Text text is displayed in the column
Ten UserdataGridView.Columns.Add (DLink);//Add columns created to Userdatagridview
11
12//Ibid. method creates “ deletes ” hyperlinks for each record
Datagridviewlinkcolumn dlink2 = new Datagridviewlinkcolumn ();
Dlink2. Text = "Delete";
Dlink2. Name = "Linkdelete";
Dlink2. HeaderText = "Delete";
Dlink2. Usecolumntextforlinkvalue = true;
USERDATAGRIDVIEW.COLUMNS.ADD (DLINK2);
19
20//Ibid. method creates “ views ” hyperlinks for each record
Datagridviewlinkcolumn Dlink3 = new Datagridviewlinkcolumn ();
Dlink3. Text = "View";
Dlink3. Name = "Linkview";
Dlink3. HeaderText = "View";
Dlink3. Usecolumntextforlinkvalue = true;
USERDATAGRIDVIEW.COLUMNS.ADD (DLINK3);
The execution effect is as follows:






PS: The above is to write their own code to implement these buttons, you can also use the property bar in the designer to add (by clicking the DataGridView Edit column directly, click "Add", select "Unbound column", select the Type "Datagridviewbuttoncolumn").


Similarly: the cell display button (datagridviewbuttoncolumn), add a drop-down box (Datagridviewcomboboxcolumn), and display the selection box (Datagridviewcheckboxcolumn) method. In fact, there are six derived classes of DataGridViewColumn: Datagridviewbuttoncolumn,datagridviewcheckboxcolumn,datagridviewcomboboxcolumn, Datagridviewimagecolumn


, Datagridviewlinkcolumn and Datagridviewtextboxcolumn, you can choose different types of columns according to your own needs, just like the different classes.


Datagridviewbuttoncolumn usage is as follows:


View Source
Print?
1//Set column can not be automatically made
2 Userdatagridview.autogeneratecolumns = false;
3//Create a datagridviewbuttoncolumn button column
4 Datagridviewbuttoncolumn dbtedit = new Datagridviewbuttoncolumn ();
5 Dbtedit.text = "edit"; the display text of this column added, that is, the text displayed in the last column of each line.
6 dbtedit.name = "Buttonedit";
7 Dbtedit.headertext = "edit";//Column title
8 Dbtedit.usecolumntextforbuttonvalue = true;//the DLink set above. Text text is displayed in the column
9 Dbtedit.width = 66;
Ten UserdataGridView.Columns.Add (Dbtedit);//Add columns created to Userdatagridview
11
12//Create “ Delete ” button
Datagridviewbuttoncolumn dbtdelete = new Datagridviewbuttoncolumn ();
Dbtdelete.text = "Delete";
Dbtdelete.name = "Buttondelete";
Dbtdelete.headertext = "Delete";
Dbtdelete.usecolumntextforbuttonvalue = true;
Dbtdelete.width = 66;
USERDATAGRIDVIEW.COLUMNS.ADD (Dbtdelete);
20
21//Create “ View ” button
Datagridviewbuttoncolumn Dbtview = new Datagridviewbuttoncolumn ();
Dbtview.text = "View";
Dbtview.name = "Buttonview";
Dbtview.headertext = "View";
num Dbtview.usecolumntextforbuttonvalue = true;
Dbtview.width = 66;
USERDATAGRIDVIEW.COLUMNS.ADD (Dbtview);
The effect is as follows:






Cell to add a drop-down box, Datagridviewcomboboxcolumn uses the following:


View Source
Print?
1 Datagridviewcomboboxcolumn Dcombo = new Datagridviewcomboboxcolumn ();
2 DCombo.Items.Add ("Administrator");
3 DCombo.Items.Add ("member");
4 DCombo.Items.Add ("User");
5 Dcombo.name = "Dcombo";
6//Where the column is displayed
7//dcombo. DisplayIndex = 1;
8 Dcombo.headertext = "role";
9
10//Bind the value of the database using the following properties
One//dcombo.datasource = DS1//ds1 for data sets, such as Datsset, that are detected from the database tables to be displayed.
//dcombo.datapropertyname = "user_id";//Bound column
13
USERDATAGRIDVIEW.COLUMNS.ADD (Dcombo); 
The effect is as follows:






Note: The Drop-down box to the left of the previous illustration cannot expand the following data because the DataGridView drop-down box pulls the following option when the edit table can be edited, which means that you need to DataGridView the ReadOnly property to False. Click to expand the data below, as shown on the right.


The code above creates a Drop-down list, the default Drop-down box, which you click three times, the first time you select the cell, the second time you enable editing, and the third time you open the Drop-down box. If you need to click to open the Drop-down box at once, you can enable the DataGridView Cellenter () event with the following code:


View Source
Print?
1 private void Userdatagridview_cellenter (object sender, DataGridViewCellEventArgs e)
2 {
3//Realize click once to display the Drop-down list box
4 if (Userdatagridview.columns[e.columnindex] is datagridviewcomboboxcolumn && e.rowindex!=-1)
5 {
6 Sendkeys.send ("{F4}");
7}
8}
PS: You can also dynamically bind a Drop-down box to a database. Use Dcombo.datasource and Dcombo.datapropertynam to set the data source from which the dropdown box data comes from, and the columns that are bound, in particular.


Cell to add check boxes and picture controls, Datagridviewcheckboxcolumn,datagridviewimagecolumn uses the following:


View Source
Print?
1//datagridview display check box
2 Datagridviewcheckboxcolumn CheckBox = new Datagridviewcheckboxcolumn ();
3 Checkbox.headertext = "selection box";
4 Checkbox.name = "CheckBox";
5 Checkbox.autosizemode =
6 datagridviewautosizecolumnmode.displayedcells;
7 Checkbox.flatstyle = Flatstyle.standard;
8//Show the three states of the selection box
9 Checkbox.threestate = true;
USERDATAGRIDVIEW.COLUMNS.ADD (CheckBox);
11
//datagridview Display Image
Datagridviewimagecolumn Dgvi = new Datagridviewimagecolumn ();
Dgvi.name = "Image";
Dgvi.valuesareicons = false;
Dgvi.image = new Bitmap ("f://ashin.jpg");
Dgvi.imagelayout = Datagridviewimagecelllayout.zoom;
dgvi.description = "Test Picture";
USERDATAGRIDVIEW.COLUMNS.ADD (DGVI);
The effect is as follows:






2. The event that the button triggers. These buttons are added in order to click to jump to different pieces of the program to perform different functions, blocking the mouse click triggered event functions as follows:


View Code View Source
Print?
1//Click on the trigger event on the table, different buttons or hyperlinks or picture dropdown boxes and so on are judged here.
2 private void Userdatagridview_cellcontentclick (object sender, DataGridViewCellEventArgs e)
3 {
4 if (Userdatagridview.columns[e.columnindex). Name = = "Buttonedit")
5 {
6 MessageBox.Show ("Trigger Edit button");
7}
8 else if (Userdatagridview.columns[e.columnindex). Name = = "Buttondelete")
9 {
Ten MessageBox.Show ("trigger Delete button");
11}
Or else if (userdatagridview.columns[e.columnindex). Name = = "Buttonview")
13 {
MessageBox.Show ("Trigger the View button");
15}
16}
3. Merge columns in DataGridView. The 3 buttons We added above are in 3 columns, and we want the 3 columns to have a common header that can be called edit or action. However, the DataGridView controls we use in WinForm do not support displaying multiple buttons in a column. What to do. There are many posts on the web for merging columns or rows of a DataGridView control, with roughly 2 ideas, one that tries to get one column to display multiple buttons and one button for each column (as the above article does), and then merges the 3 columns, The merger is the use of DataGridView cellpainting event to redraw the table, netizens said that the method exists when the page is pulling down the drawing error or drawing delay does not refresh and so on, it seems that the latter method used more. In short, either way to achieve a seemingly more cumbersome, time is limited, I just practice practicing (itself is a novice), get a small program just, do not want to spend too much time studying this small details, and so on later, come back to study carefully. Here's an example of a merged column, where a friend implements a two-dimensional header, and the code is as follows:


View Source
Print?
1 int top = 0;
2 int left = 0;
3 int height = 0;
4 int width1 = 0;
5 private void Datagridview1_cellpainting (object sender, DataGridViewCellPaintingEventArgs e)
6 {
7 #region Redraw the DataGridView table header
8 DataGridView DGV = (DataGridView) (sender);
9 if (E.rowindex = 1 && (e.columnindex = 3 | | e.columnindex = 4))
10 {
One//e.cellstyle.font = new Font (DataGridView1.DefaultCellStyle.Font, fontstyle.bold);
//e.cellstyle.wrapmode = Datagridviewtristate.true;
if (E.columnindex = 3)
14 {
top = E.cellbounds.top;
left = E.cellbounds.left;
Height = e.cellbounds.height;
Width1 = E.cellbounds.width;
19}
20
int width2 = this.datagridview1.columns[4]. Width;
22
Rectangle rect = new Rectangle (left, top, width1 + width2, e.cellbounds.height);
using (Brush BackColorBrush = new SolidBrush (E.cellstyle.backcolor))
25 {
26//Erase the original cell background
E.graphics.fillrectangle (BackColorBrush, rect);
28}
29
using (Pen Gridlinepen = new Pen (DGV). Gridcolor))
31 {
E.graphics.drawline (Gridlinepen, left, top, left + width1 + width2, top);
E.graphics.drawline (Gridlinepen, left, top + HEIGHT/2, left + width1 + width2, top + HEIGHT/2);
E.graphics.drawline (Gridlinepen, left + width1, top + HEIGHT/2, left + width1, top + height);
35
36//Calculate the position of the drawing string
Panax Notoginseng String columnvalue = year;
SizeF SF = e.graphics.measurestring (Columnvalue, E.cellstyle.font);
float Lstr = (width1 + width2-sf. Width)/2;
float RSTR = (HEIGHT/2-SF. Height)/2;
41//Draw a text box
if (Columnvalue!= "")
43 {
E.graphics.drawstring (Columnvalue, E.cellstyle.font, New SolidBrush (E.cellstyle.forecolor),
Left + LSTR,
Top + RSTR,
Stringformat.genericdefault);
48}
49
50//Calculate the position of the drawing string
Wuyi Columnvalue = "The amount of assets of the Bureau net Table";
SF = e.graphics.measurestring (Columnvalue, E.cellstyle.font);
Lstr = (width1-sf. Width)/2;
RSTR = (HEIGHT/2-SF. Height)/2;
55//Draw a text box
if (Columnvalue!= "")
57 {
E.graphics.drawstring (Columnvalue, E.cellstyle.font, New SolidBrush (E.cellstyle.forecolor),
Left + LSTR,
Top + HEIGHT/2 + RSTR,
Stringformat.genericdefault);
62}
63
64//Calculate the position of the drawing string
Columnvalue = "Network assets amount";
SF = e.graphics.measurestring (Columnvalue, E.cellstyle.font);
Lstr = (width2-sf. Width)/2;
RSTR = (HEIGHT/2-SF. Height)/2;
69//Draw a text box
if (Columnvalue!= "")
71 {
E.graphics.drawstring (Columnvalue, E.cellstyle.font, New SolidBrush (E.cellstyle.forecolor),
Left + width1 + lstr,
Top + HEIGHT/2 + RSTR,
Stringformat.genericdefault);
76}
77}
E.handled = true;
79}
#endregion
81}
However, although I did not study the merging of the columns carefully, I still found a way to replace the merge problem to some extent. Because I need to merge the "delete", "edit" and "view" These three columns, I would like to do not show the table header, anyway is the Action column, do not show the table head does not notice what to see. Coincidentally, I found Eines lying one months ago published a blog specifically introduced Easycode generator generated changes, delete Information list interface, with the Easycode 2.0 version, the resulting page first feeling is very fresh, is really a bright, The original Easycode code generator is so powerful. What's even more tempting is the ability to merge columns like I want, why is it similar, because it looks the same, but he is putting 1 columns (a cell) inside the 2 buttons, rather than the common kind of a column put a button and then merge the cells of the table header. The following is the appearance of the custom button column for the DataGridView Control in the WinForm of the Easycode 2.0.50727.4963 version of the Eines team, see:






For more information about how to design a custom button column for a DataGridView control in WinForm, refer to this blog:


Http://www.cnblogs.com/BudEasyCode/archive/2012/02/29/2373702.html


Easycode's official website: http://www.budeasycode.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.