How DataGridView cells are embedded in multiple button controls

Source: Internet
Author: User

Some time ago I had a friend interview the company encountered this interview problem, he also gave me the original question to see see, and there is no special point, about this kind of problem, how to embed the cell on the grid multiple controls (such as buttons, hyperlinks, etc.), I searched the following questions on the Internet, found a lot of answers but all messy, This article helps you understand how to deal with this kind of problem.

The Datagirdview grid control provided by Microsoft can embed the required controls in cells by means of the Getcelldisplayrectangle () method, such as:

This. Load + = Form1_Load;

void Form1_Load (object sender, EventArgs e)
{
Mulautobtnedit ();
}

private void Mulautobtnedit ()
{
THIS.DATAGRIDVIEW1.COLUMNS.ADD ("Colbtnedit", "Embed Operation Button");
this.datagridview1.columns["Colbtnedit"]. Width = 150;
int index = this.datagridview1.columns["Colbtnedit"]. Index;
this.datagridview1.columns["Colbtnedit"]. resizable = Datagridviewtristate.false;
Button Btnadd = Getbtnbytype ("Btnadd", "new");
Button Btnedit = Getbtnbytype ("Btnedit", "Modify");
Button Btndel = Getbtnbytype ("Btndel", "delete");
THIS.DATAGRIDVIEW1.CONTROLS.ADD (Btnadd);
THIS.DATAGRIDVIEW1.CONTROLS.ADD (Btnedit);
THIS.DATAGRIDVIEW1.CONTROLS.ADD (Btndel);
Rectangle Rectangle = this.dataGridView1.GetCellDisplayRectangle (index, 0, true);//Gets the rectangular area of the current cell
Btnadd.size = Btnedit.size = Btndel.size = new Size (rectangle. WIDTH/3 + 1, rectangle. Height);
Btnadd.location = new Point (rectangle. Left, rectangle. TOP);
Btnedit.location = new Point (rectangle. Left + btnadd.width, rectangle. TOP);
Btndel.location = new Point (rectangle. Left + btnadd.width + btndel.width, rectangle. TOP);
}

Private Button Getbtnbytype (string strbtnname,string strbtntext)
{
Button btn = New button ();
Btn. Name = Strbtnname;
Btn. Text = Strbtntext;
Btn. Click + = Btn_click;
return BTN;
}

private void Btn_click (object sender, EventArgs e)
{
If (Sender is Button)
{
Button btn = (button) sender;
MessageBox.Show (String. Format ("click button: {0}", btn. Text));
}
}

Through the above operation can be a number of buttons embedded in the cell, regardless of maintainability, efficiency issues, only to give everyone the idea, for "HYPERLINK" control, etc. can be processed in a similar way, similar to the following URL:

https://bbs.csdn.net/topics/340208660

In the online distribution of another way, the idea is similar, address I also posted out:

52595863

About the third party devexpress components on the grid, such as the GridView, I did not find a good solution to the above problems, there are shortcomings, insider things can be on the message board.

A young idler ~ an old beggar!

How DataGridView cells are embedded in multiple button controls

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.