简体   繁体   中英

Insert a button into a specific data grid view column

Can I put the button wherever the row I wanted? For example, I want to put the button in row[2], so the button will be appear in that row at every columns.

I tried to made something like this to made the button in the right, but I failed :

DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
        btn.HeaderText = "Delete";
        btn.Text = "   ";
        btn.UseColumnTextForButtonValue = true;
        tbl_incomingrawmaterialform.ColumnCount = 5; 

        tbl_incomingrawmaterialform.Columns[0].HeaderText = "Header1";
        tbl_incomingrawmaterialform.Columns[1].HeaderText = "Header2";
        tbl_incomingrawmaterialform.Columns[2].HeaderText = "Header3";
        tbl_incomingrawmaterialform.Columns[3].HeaderText = "Header4";
        tbl_incomingrawmaterialform.Columns.Add(btn);

And this is for the looping data :

tbl_incomingrawmaterialform.Rows[i].Cells[0].Value = (i + 1);
            tbl_incomingrawmaterialform.Rows[i].Cells[1].Value = Value1;
            tbl_incomingrawmaterialform.Rows[i].Cells[2].Value = Value2;
            tbl_incomingrawmaterialform.Rows[i].Cells[3].Value = Value3;
            DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
            tbl_incomingrawmaterialform.Rows.Add(btn);

            i++;

But this doesn't work. I thought if I empty one columns, the button will automatically be there. I'm sorry, I'm still beginner.

You should use gridView.Columns.Insert() instead of gridView.Rows.Add(). Please take a look:

        int columnIndex = 4;

        if (tbl_incomingrawmaterialform.Columns["Delete"] == null)
        {
            tbl_incomingrawmaterialform.Columns.Insert(columnIndex, btn);
        }

Happy coding!

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