Disabling button in DataGridViewButtonColumn - c#

I pull results from Oracle database and store it in DataTable before setting it as source in DataGridView. The reason for this is I remove some columns before I show it to user. Using DataGridViewButtonColumn I have added column that has enables user to "Edit" specific row. My problem is I'm trying to disable that button based on value in that current row. Leave the rest of the row buttons enabled.
var table = OracleConnection.Results(myquery);
table.Columns.Remove("Comments");
var editButton = new DataGridViewButtonColumn;
editButton.Value = "Edit";
editButton.Text = "Edit";
editButton.UseColumnTextForButtonValue = true;
_dtgvResults.Source = table;
_dtgvResults.Columns.Add(editButton);

Take a look in OnRowDataBound event in your DataGridView
MS Reference

Related

DataGridView not displaying changes to checkbox when application is launched

I have the following code that will create a checkbox column, insert as first column to the main data grid, and then loop through the rows to set the checkbox to checked. Basically, what I'm trying to do is add checkboxes that are checked by default when the application launches.
The issue is that when the application is started, the checkboxes remain untouched. I've added the ToolTip text below to see whether that takes effect, but no luck there.
I also added an event that will trigger the same code below (calling the same method), and it will refresh the grid with the checkboxes CHECKED.
DataGridViewCheckBoxColumn importSelectionColumn = new DataGridViewCheckBoxColumn();
importSelectionColumn.Name = "dataSelection";
importSelectionColumn.DisplayIndex = 0;
importSelectionColumn.HeaderText = "\u2611";
importSelectionColumn.Width = 35;
importSelectionColumn.Visible = true;
importSelectionColumn.FalseValue = false;
importSelectionColumn.TrueValue = true;
importSelectionColumn.HeaderCell.Style.Font = new Font(FontFamily.GenericSansSerif, 16f);
// Add column to grid:
mainDataGrid.Columns.Insert(0, importSelectionColumn);
// Set checkbox to true for all rows:
foreach (DataGridViewRow row in this.mainDataGrid.Rows)
{
row.Cells["dataSelection"].Value = true;
// Adding this just to see whether it's set when application starts.
row.Cells["dataSelection"].ToolTipText = "Testing";
}
mainDataGrid.RefreshEdit();
mainDataGrid.Refresh();
Make sure that the code that changes state is not being executed too early.
It should be executed after Loaded event of the container form, when all controls are loaded and ready for work.
Adding a check box control to your mainDataGrid can be done in DataBound event instead of on page load event.
Try to use below code on your page and check:
protected void mainDataGrid_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow objRow in mainDataGrid.Rows)
{
TableCell tcCheckCell = new TableCell();
CheckBox chkCheckBox = new CheckBox();
tcCheckCell.Controls.Add(chkCheckBox);
objRow.Cells.AddAt(0, tcCheckCell);
}
}

RadGrid GridTemplateColumn dynamically added insert mode

I've RadGrid where I want to add a column dynamically so I've done something like this in page load
GridTemplateColumn gtc = new GridTemplateColumn();
gtc.DataField = "chqNumber";
gtc.HeaderText = "Cheque Number";
gtc.UniqueName = "chqNumber";
RadGrid1.MasterTableView.Columns.Add(gtc);
It work fine, now I want to add textbox in this column as the user click "+add new record" on grid here is my code of itemCreated event
GridEditableItem gdit = (GridEditableItem)e.Item;
RadTextBox txtBox = new RadTextBox();
txtBox.ID = "someIDWhatEver";
gdit["chqNumber"].Controls.Add(txtBox);
but this textbox gets added to another new column what I mean is this textbox does not added to same chqNumber column you can view my attached image which will illustrate better
as you can see my dynamic column gets added at the last even after insert and cancel button and dynamic textbox doesn't gets in this column please avoid my english grammatical mistakes :P

DatagridviewButtonColumn is visible to every row when click on a cell of row/column

I have a Datagridview that has some text box columns.In this Datagridview I have added a DataGridViewButtoncolumn after the third column and I have set it as visible = false as shown in below code.
DataGridViewButtonColumn btnEllipse = new DataGridViewButtonColumn();
btnEllipse.Text = "...";
btnEllipse.FillWeight = 6;
btnEllipse.MinimumWidth = 20;
btnEllipse.Width = 20;
btnEllipse.DividerWidth = 0;
//btnCompanyProperty.HeaderText = "To Company/Property";
btnEllipse.UseColumnTextForButtonValue = true;
dgvForecast.Columns.Insert(4, btnEllipse);
dgvForecast.Columns[4].Visible = false;
dgvForecast.Columns[dgvForecast.Columns["BaseValue"].Index + 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dgvForecast.Columns[dgvForecast.Columns["BaseValue"].Index + 1].Width = 20;
dgvForecast.Columns["BaseValue"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
dgvForecast.Columns["BaseValue"].CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
Now My problem is that I have to show only this button cell when i click on index 3 cell.Now
this is showing full Button column when i click on index 3 cell as shown in below image.I only want to show selected row button cell not full Button Column.
I want that when i click on first row value cell only first row Button cell should be visible and when i click on second row value cell first row button should be invisible and current row button should be visible.
You can't hide specific cells in the DataGridView. There is no direct support for this in the DataGridView.
One way of doing this is to create custom DataGridViewButtonColumn to control the visibility using a custom property and override the paint method to draw a rectangle with the background color when this property is set. It is similar to that available in the msdn forum question removehide-button-on-datagridview.
Using the above custom column you could make the cell visible in CellEnter/RowEnter event and hide it in CellLeave/RowLeave event.

.net Datagrid WinForm - Adding a DataGridViewButtonColumn Automatically Adds a Row

When adding a new DataGridViewButtonColumn to a DataGrid, a new row of buttons is added.
Is there a way to prevent this from happening?
I'd prefer not to have to clear all the rows after adding columns.
Also, can't use WPF. This is a Winform project I inherited.
DataGridViewButtonColumn bcol = new DataGridViewButtonColumn();
bcol.HeaderText = "Button Column";
bcol.Text = "Click Me";
bcol.Name = "btnClickMe";
The above code snippet creates a new row with a button.
Try disallowing adding new rows:
dataGridView1.AllowUserToAddRows = false;
I think what you're seeing is the new row at the bottom of your grid.
You can see the difference here: (enabled in the top pic, disabled in the bottom pic)

Add buttons to datagridview in windows form

I want to add two buttons onto datagridview. Now they are on the right side. But I want to locate them on the left.
The other thing is I want to add an update event for "Edit" button. Is it
private void Edit_Click(object sender, EventArgs e)
{
}
Form code:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'qDataSet.Metric' table.
// You can move, or remove it, as needed.
this.metricTableAdapter.Fill(this.qDataSet.Metric);
DataGridViewButtonColumn EditColumn = new DataGridViewButtonColumn();
EditColumn.Text = "Edit";
EditColumn.Name = "Edit";
EditColumn.DataPropertyName = "Edit";
dataGridView1.Columns.Add(EditColumn);
DataGridViewButtonColumn DelColumn = new DataGridViewButtonColumn();
DelColumn.Text = "Delete";
DelColumn.Name = "Delete";
DelColumn.DataPropertyName = "Delete";
dataGridView1.Columns.Add(DelColumn);
}
The image likes:
Thank you.
You seems to have design the datagridview from the designer.
You can use the wizard which allow to edit columns. There you'll add two columns (one for editing and the other for deleting). You can choose where you want to set those columns.
The text of the button is defined using the property "Text", you can also set the property "UseColumnTextForButton".
You can manage easily in the CellContentClickEvent the column and row which is clicked and then do the job.
If you want to manage access right (like allowing someone to editing and someone else to not editing) you can play with show/hide of this column.
See DisplayIndex
The zero-based position of the column as it is displayed in the associated DataGridView, or -1 if the band is not contained within a control.
EditColumn.DisplayIndex = 0;
DelColumn.DisplayIndex = 1;
You can't subscribe to the Edit button events directly.
So, the decision to subscribe to the CellClick event and check which are pressed
And yes, for columns position set DisplayIndex property
DataGridViewButtonColumn ButtonColumn = new DataGridViewButtonColumn();
ButtonColumn.Name = "Print";
ButtonColumn.HeaderText = "Print";
ButtonColumn.FlatStyle = FlatStyle.Popup;
ButtonColumn.DefaultCellStyle.ForeColor = Color.White;
ButtonColumn.DefaultCellStyle.BackColor = Color.CadetBlue;
ButtonColumn.Text = "Print";
ButtonColumn.UseColumnTextForButtonValue = true;
int columnIndex = 12; /*your column index number*/
if (dtGridTicket.Columns["Print"] == null)
{
dtGridTicket.Columns.Insert(columnIndex, ButtonColumn);
}
If you load the page column index hide so you use
dtGridTicket.Columns.RemoveAt(12);

Categories