I put the button Column into DGV control. Additionally, when the
button is pressed, all the cells are enables (read to edit) and the text
into row bolds. You can disable the editing on two ways:
- or clicking another row (the new row will be enabled, and the previous one disabled)
- or clicking the button again (so for the 2nd time)
I did the code, and it was tested -works perfect - I hope this is what you have wanted!
In my text code the dgv only has 3 columns (its an example). So you only have to modify it to your needs, and here we go:
- or clicking another row (the new row will be enabled, and the previous one disabled)
- or clicking the button again (so for the 2nd time)
I did the code, and it was tested -works perfect - I hope this is what you have wanted!
In my text code the dgv only has 3 columns (its an example). So you only have to modify it to your needs, and here we go:
namespace EditCells { public partial class Form1 : Form { List<Customer> list; public Form1() { InitializeComponent(); InitializingBS(); CreatingDGV(); //create a click event for the dgv`s buttons: dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); } private void CreatingDGV() { //setting datasurce and stuff: dataGridView1.AutoGenerateColumns = false; dataGridView1.AllowUserToAddRows = false; dataGridView1.RowHeadersVisible = false; dataGridView1.DataSource = list; //creating columns: dataGridView1.Columns.Add("col1", "Id"); dataGridView1.Columns.Add("col2", "Customer name"); DataGridViewButtonColumn columnButton = CreatingButtonColumn(); dataGridView1.Columns.Add(columnButton); dataGridView1.Columns[0].DataPropertyName = "id"; dataGridView1.Columns[1].DataPropertyName = "name"; //disabling columns: dataGridView1.Columns[0].ReadOnly = true; dataGridView1.Columns[1].ReadOnly = true; } private DataGridViewButtonColumn CreatingButtonColumn() { DataGridViewButtonColumn column = new DataGridViewButtonColumn(); { column.HeaderText = ""; column.Name = "GetRowId"; column.Text = "Enable row"; column.UseColumnTextForButtonValue = true; column.ReadOnly = true; } return column; } private void InitializingBS() { list = new List<Customer>(); Customer customer1 = new Customer(1, "Mitja Bonca"); Customer customer2 = new Customer(2, "Sara McRae"); Customer customer3 = new Customer(3, "John Dollan"); Customer[] array = new Customer[3] { customer1, customer2, customer3 }; for (int i = 0; i < array.Length; i++) list.Add(array[i]); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex != dataGridView1.Columns["GetRowId"].Index) return; int row = e.RowIndex; for (int i = 0; i < dataGridView1.Rows.Count; i++) { foreach (DataGridViewCell cell in dataGridView1.Rows[i].Cells) { if (i == row) { if (cell.ReadOnly == false) { //if the button is pressed for 2nd time, the row will be disabled and text to regular: cell.Style.Font = new Font(cell.InheritedStyle.Font, FontStyle.Regular); cell.ReadOnly = true; } else { //the enabled rows will get a bold text: cell.Style.Font = new Font(cell.InheritedStyle.Font, FontStyle.Bold); cell.ReadOnly = false; } } else { //all the other rows will be disables and fort changes back to rwgular: cell.Style.Font = new Font(cell.InheritedStyle.Font, FontStyle.Regular); cell.ReadOnly = true; } } } } } public class Customer { public int id { get; set; } public string name { get; set; } public Customer(int _id, string _name) { id = _id; name = _name; } } }
No comments:
Post a Comment