简体   繁体   中英

DataGridView add buttons and text to Header Row

I have a datagridview that I populate from a loop. If I do the following:

public Form(Dictionary<String, String> headers)
{
    InitializeComponent();
    foreach (var key in headers.Keys) {
        datagridview1.Columns.Add(key, key);
    }
}

I get a header row containing text.

How do I add buttons and text to the header row of datagridview1 instead of just text?

I am using winforms and .net 4.5 . (I guess I could switch to 4.5.1 if it makes things easier).

As far as I know this is not possible. Here are ways you could get close:

  • Set the first row's Frozen property and put the buttons in the first row.
  • Add a right-click menu to expose the functions you want to add.

May thats helping you

 DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
 dataGridView1.Columns.Add(btn);
 btn.HeaderText = "YourText";
 btn.Text = "YourText";
 btn.Name = "DGVbutton1";
 btn.UseColumnTextForButtonValue = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM