Hide some datagridview checkbox cell - c#

I have a datagridview showing installments of a loan. I created a datagridviewcheckbox column so then I can select all the installments i want to pay for.
This is a screen of the datagrid:
My issue is that I need to disable the checkboxes of the paid intallments. In this case, when "Restante" (what´s left to pay) is = 0.
I read some posts where they used the paint event to not show the checkbox cell, but i didnt like that solution. I thought of hiding the checkbox cell, but i don´t know if it is possible to do that.
Thats what i tried:
foreach (DataGridViewRow row in dgv_Cuotas.Rows)
{
if (Convert.ToDecimal(dgv_Cuotas.Rows[row.Index].Cells[17].Value) == 0)
{
dgv_Cuotas.Rows[row.Index].Cells[16].Visible = false;
}
}
Obviously this does not works, I get a compiler error message saying that the property is read only.
Does somebody knows how to set the checkbox cell to invisible?
Just in case, I attach the DataGridViewCheckboxColumn creation code:
DataGridViewCheckBoxColumn chbox = new DataGridViewCheckBoxColumn();
{
chbox.CellTemplate = new DataGridViewCheckBoxCell();
chbox.HeaderText = "";
chbox.Name = "Seleccionar";
chbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
chbox.FlatStyle = FlatStyle.Standard;
}
dgv_Cuotas.Columns.Insert(16, chbox);
dgv_Cuotas.Columns[16].DisplayIndex = 0;
EDIT:
Some considerations:
I use the cell content click event to handle the checkboxes, so readonly wont work. What I want is to hide the checkbox:
private void dgv_Cuotas_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
if (dgv_Cuotas.Columns[e.ColumnIndex].Name == "Seleccionar")
{
DataGridViewRow row = dgv_Cuotas.Rows[e.RowIndex];
DataGridViewCheckBoxCell cellSeleccion = row.Cells["Seleccionar"] as DataGridViewCheckBoxCell;
int n_cuota = Convert.ToInt32(dgv_Cuotas[2, dgv_Cuotas.CurrentRow.Index].Value);
Cuota cuota_seleccionada = new Cuota();
cuota_seleccionada = Lista_cuotas.Where(x => x.num_cuota == n_cuota).First();
if (cellSeleccion != null && Convert.ToBoolean(cellSeleccion.Value) == true)
{
cellSeleccion.Value = false;
Actualizar_cuotas_seleccionadas(false, cuota_seleccionada);
}
else
{
if (cellSeleccion != null && Convert.ToBoolean(cellSeleccion.Value) == false)
{
cellSeleccion.Value = true;
Actualizar_cuotas_seleccionadas(true, cuota_seleccionada);
}
}
}
In the other hand, I´m already using the Onpaint event. Its inherited, thats why I´m trying to avoid using it.

Assign a value to the checkbox cell. Then Convert it to a TextBox with a new value.
Worked for me.
dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";

Yes, you can do this by Converting the DataGridViewCheckBoxCell to DataGridViewTextBoxCell
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (dataGridView1.Rows[row.Index].Cells[17].EditedFormattedValue.ToString().Length == 0) // if (string.IsNullOrWhiteSpace(dataGridView1.Rows[row.Index].Cells[4].EditedFormattedValue.ToString()))
break;
if (Convert.ToDecimal(dataGridView1.Rows[row.Index].Cells[17].EditedFormattedValue) == 0)
{
dataGridView1.Rows[row.Index].Cells[16].Value = null;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
}
else
{
//dgv_Cuotas.Rows[row.Index].Cells[16] = new DataGridViewCheckBoxCell();
}
}

Use the cell's ReadOnly attribute to disable any modification.
If you want to turn it to hidden, you need to override the painting code for the cells.

Try to hide it and the value remains, which should prevent runtime errors.
dataGridView1.Rows[row.Index].Cells[16].Style.Padding =
new Padding(dataGridView1.Rows[row.Index].Cells[16].OwningColumn.Width, 0, 0, 0);

I took spajce's answer and tweaked it a bit to make it work for me.
for (var i = 0; i < datagridview1.Count; i++)
{
if ((bool)datagridview1[0, i])
{
datagridview1[0, i] = new DataGridViewTextBoxCell
{
Style = { ForeColor = Color.Transparent,
SelectionForeColor = Color.Transparent }
};
}
}
We're basically iterating through the rows and looking for a 'true' checked box. If it's checked then we're converting the box to a text box and setting its text color to Transparent so the cell looks empty. I hope this helps everyone who had this problem, I spent hours trying to find a workable answer.

There are several ways to accomplish what you want.
For example, you can use the Readonly property of the cell to avoid the user to change the values and change the appeareance of the control to look grayed:
C# DataGridViewCheckBoxColumn Hide/Gray-Out

A simple and effective alternative is to use the chkbox ThreeState property. In code below, if my object does not have an email address, then I don't want to allow the user to tick the checkbox for sending email. To do this, the check box value is set to Unknown and read-only, which means it is displayed as "unselectable" and user cannot modify.
chk = DirectCast(row.Cells(m_idxEmailChecked), DataGridViewCheckBoxCell)
If String.IsNullOrEmpty(contact.EmailAddress) Then
chk.Value = enumTristate.Unknown
chk.ReadOnly = True
Else
chk.ThreeState = False
End If
Note this also requires several associated ThreeState properties to be set on the checkbox.
.ValueType = GetType(enumTristate)
.TrueValue = enumTristate.True
.FalseValue = enumTristate.False
.IndeterminateValue = enumTristate.Unknown
.ThreeState = True
Good luck.

I tried Charlie's way:
dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";
I got fewer errors, but still kept getting them. So I tried to instantiate the new DataGridViewTextBoxCell separately, and that did the trick for me (I didn't need to set the checkbox cell value either):
DataGridViewTextBoxCell c = new DataGridViewTextBoxCell();
c.Value = "";
dataGridView1.Rows[row.Index].Cells[16] = c;
Hope that helps someone!

Related

Text.Replace in gridview replacing entire cell

I have a gridview that is bound to a sql database. As the user enters data they must indicate whether that cell's information is complete or not. To do this they enter /end/ at the end of their statement and it will automatically change the cell color. If nothing is entered then nothing happens.
Here is the code:
if (dataItem != null)
{
var label = dataItem["Status"].FindControl("Statuslbl") as Label;
if (label != null)
{
var item = dataItem;
var text = label.Text;
if (text.Contains("/end/"))
{
item["Status"].BackColor = Color.Lime;
item["Status"].Text = item["Status"].Text.Replace(#"/end/", #"");
}
else
{
item["Status"].BackColor = Color.Salmon;
}
}
}
Instead of just hiding the '/end/' like I need it to, it hides the entire cells contents.
How can I go about fixing this?
Discovered all I would need to do would be the following to achieve my result:
if (text.Contains("/end/"))
{
item["Test"].BackColor = Color.Lime;
item["Test"].Text = label.Text.Replace("/end/", " ");
}
Really simple, I just needed to use label.Text.

c# How do i change the color of a specific textbox if it is empty?

I am trying to change the color of an empty textbox, I have more than one textbox on this form and i wish to highlight the empty one when a user clicks submit. I have written the loop below which is in my btnSubmit function after checking if all the textbox have a value. Can anyone help in completing this loop for me??
foreach (Control txtbxs in this.Controls)
{
if (txtbxs is TextBox)
{
var TBox = (TextBox)txtbxs;
if (TBox.Text == string.Empty)
{
TBox.ForeColor = Color.Red;
}
}
}
lblTopError.Text = "Please fill in the missing billing information";
pnlTopError.Visible = true;
When your string is empty, changing the ForeColor will do nothing since you don't have Text to display in red. Consider using BackColor and remember to have an event when text is entered to switch it back to the appropriate BackColor.
If this is what you are trying to do, have you considered using the error provider? this would help you to signal the user and prompt them to put in the information.
errorProvider= new System.Windows.Forms.ErrorProvider();
errorProvider.BlinkRate = 1000;
errorProvider.BlinkStyle = System.Windows.Forms.ErrorBlinkStyle.AlwaysBlink;
private void TextValidated(object sender, System.EventArgs e)
{
var txtbox = Sender as TextBox;
if(IsTextValid(txt))
{
// Clear the error, if any, in the error provider.
errorProvider.SetError(txtbox, String.Empty);
}
else
{
// Set the error if the name is not valid.
errorProvider.SetError(txtbox, "Please fill in the missing billing information.");
}
}
You can apply any CSS you want like this:
TBox.Attributes.Add("style", "color: red; border: solid 1px #FC3000")
I would use this instead of:
TBox.ForeColor = Color.Red;
Well since there aren't much textboxes in this form, i went the easy route and it worked, code bellow:
List<TextBox> boxes = new List<TextBox>();
if (string.IsNullOrWhiteSpace(txtFname.Text))
{
//highlightTextBox= txtFname;
boxes.Add(txtFname);
}
if (string.IsNullOrWhiteSpace(txtLname.Text))
{
//highlightTextBox = txtLname;
boxes.Add(txtLname);
}
if (string.IsNullOrWhiteSpace(txtAddOne.Text))
{
//highlightTextBox = txtAddOne;
boxes.Add(txtAddOne);
}
if (string.IsNullOrWhiteSpace(txtTown.Text))
{
//highlightTextBox = txtTown;
boxes.Add(txtTown);
}
if (string.IsNullOrWhiteSpace(txtPostCode.Text))
{
//highlightTextBox = txtPostCode;
boxes.Add(txtPostCode);
}
foreach (var item in boxes)
{
if (string.IsNullOrWhiteSpace(item.Text))
{
item.BackColor = Color.Azure;
}
}
lblTopError.Text = "Please fill in the missing billing information highlighted below";
pnlTopError.Visible = true;

InvalidArgument=Value of '0' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex

I am getting the above error when i am trying this code. I tried giving just my code but no use. (It was default)
Here is my XML file
The error is in cmbProduct_SelectedIndexChanged event.
cmbProduct --> combobox
cmbBrand --> combobox
Global
DataSet dsUpdate = new DataSet();
Form_load
dsUpdate.ReadXml(#"...\..\stock.xml");
cmbProduct.DataSource = dsUpdate.Tables[0]
.DefaultView.ToTable(true, "productname");//.DefaultView;
cmbProduct.DisplayMember = "productname";
cmbProduct.SelectedIndex = 0;
cmbProduct_SelectedIndexChanged
cmbBrand.Items.Clear();
foreach (DataRow Row in dsUpdate.Tables[0].Select("productname='" + cmbProduct.Text + "'"))
{
//cmbBrand.SelectedIndex = i;
cmbBrand.Items.Add(Row["brandname"].ToString());
//i++;
}
cmbBrand.SelectedIndex = 0; /*ERROR*/
Please help
Thanks in Advance.
Problem is:
when you start application, you do not have items in cmbBrand, but cmbProduct fires SelectedIndexChanged.
Try this:
remove SelectedIndexChanged event initialization from Form1.Designer.cs. Try to find following line:
this.cmbProduct.SelectedIndexChanged += new System.EventHandler(this.cmbProduct_SelectedIndexChanged);
After that, when you populate DataSet with data from xml file, initialize SelectedIndexChanged event:
dsUpdate.ReadXml(#"...\..\stock.xml");
cmbProduct.DataSource = dsUpdate.Tables[0].DefaultView.ToTable(true, "productname");//.DefaultView;
cmbProduct.DisplayMember = "productname";
this.cmbProduct.SelectedIndexChanged += new System.EventHandler(this.cmbProduct_SelectedIndexChanged);
cmbProduct.SelectedIndex = 0;
i had same error. i think this error have a some reasons.
so my error is related to "set DataSource in another thread is not working"
example
//Run in another thread
myComboBox.DataSource = myDataSource; //not set
fix with
myComboBox.Invoke(new Action(() => myComboBox.DataSource = myDataSource));
You can also try this. Before setting combobox DataSource set its BindingContext
cmbProduct.BindingContext = this.BindingContext;
This will happen if you attempt to set the SelectedIndex while there is no valid datasource. If you're resetting the default to 0, and occasionally changing the datasource list, you may see this. You don't need to default to 0 if applying a new datasource, so simple check will avoid it happening:
if (comboBox.Datasource != null) comboBox.SelectedIndex = 0;
If you have this issue:
use the Form_Activated event handler to control setting the indexes.
For me, I had a series of dynamically generated ComboBoxes I added to a Form.
I made a list of the ones where I wanted to use SetIndex=0, then iterated through them in this handler.
I also had a boolean, firstFormActivation, when called the SetIndex the one time only..
You can incidentally use this method for Focus() too, so first field in a Form gets focus when dynamically added.
Here is some code to illustrate the point:
private readonly List<ComboBox> combosToSetIndexOn = new List<ComboBox>();
private bool firstActivation = true;
private Control firstWindowsControl = null;
...
// Other code sets firstWindowsControl...
private void DynamicForm_Activated(object sender, EventArgs e)
{
if (firstActivation)
{
firstActivation = false;
bool fwcPresent = (firstWindowsControl != null);
Console.WriteLine($"DynamicForm_Activated: firstWindowControl present: {fwcPresent}");
if (fwcPresent)
{
firstWindowsControl.Focus();
}
if (combosToSetIndexOn.Count > 0)
{
foreach (ComboBox c in combosToSetIndexOn)
{
Console.WriteLine($"DynamicForm_Activated: processing: {c.Name}");
c.SelectedIndex = 0;
}
}
}
In my case the following was causing my problem
myComboBox.DataSource = myBindingSource
myBindingSource.DataSource = items.ToList() // error
The following worked
myComboBox.DataSource = null;
myBindingSource.DataSource = items.ToList();
MyComboBox.DataSource = myBindingSource;

How to disable the ability to select in a DataGridView?

I want to use my DataGridView only to show things, and I want the user not to be able to select any row, field or anything from the DataGridView.
How can I do this?
I'd go with this:
private void myDataGridView_SelectionChanged(Object sender, EventArgs e)
{
dgvSomeDataGridView.ClearSelection();
}
I don't agree with the broad assertion that no DataGridView should be unselectable. Some UIs are built for tools or touchsreens, and allowing a selection misleads the user to think that selecting will actually get them somewhere.
Setting ReadOnly = true on the control has no impact on whether a cell or row can be selected. And there are visual and functional downsides to setting Enabled = false.
Another option is to set the control selected colors to be exactly what the non-selected colors are, but if you happen to be manipulating the back color of the cell, then this method yields some nasty results as well.
You may set a transparent background color for the selected cells as following:
DataGridView.RowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
Enabled property to false
or
this.dataGridView1.DefaultCellStyle.SelectionBackColor = this.dataGridView1.DefaultCellStyle.BackColor;
this.dataGridView1.DefaultCellStyle.SelectionForeColor = this.dataGridView1.DefaultCellStyle.ForeColor;
I fixed this by setting the Enabled property to false.
If you don't need to use the information in the selected cell then clearing selection works but if you need to still use the information in the selected cell you can do this to make it appear there is no selection and the back color will still be visible.
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
dataGridView.RowsDefaultCellStyle.SelectionBackColor = row.DefaultCellStyle.BackColor;
}
}
This worked for me like a charm:
row.DataGridView.Enabled = false;
row.DefaultCellStyle.BackColor = Color.LightGray;
row.DefaultCellStyle.ForeColor = Color.DarkGray;
(where row = DataGridView.NewRow(appropriate overloads);)
I found setting all AllowUser... properties to false, ReadOnly to true, RowHeadersVisible to false, ScollBars to None, then faking the prevention of selection worked best for me. Not setting Enabled to false still allows the user to copy the data from the grid.
The following code also cleans up the look when you want a simple display grid (assuming rows are the same height):
int width = 0;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
width += dataGridView1.Columns[i].Width;
}
dataGridView1.Width = width;
dataGridView1.Height = dataGridView1.Rows[0].Height*(dataGridView1.Rows.Count+1);
you have to create a custom DataGridView
`
namespace System.Windows.Forms
{
class MyDataGridView : DataGridView
{
public bool PreventUserClick = false;
public MyDataGridView()
{
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (PreventUserClick) return;
base.OnMouseDown(e);
}
}
}
`
note that you have to first compile the program once with the added class, before you can use the new control.
then go to The .Designer.cs and change the old DataGridView to the new one without having to mess up you previous code.
private System.Windows.Forms.DataGridView dgv; // found close to the bottom
…
private void InitializeComponent() {
...
this.dgv = new System.Windows.Forms.DataGridView();
...
}
to (respective)
private System.Windows.Forms.MyDataGridView dgv;
this.dgv = new System.Windows.Forms.MyDataGridView();
I liked user4101525's answer best in theory but it doesn't actually work.
Selection is not an overlay so you see whatever is under the control
Ramgy Borja's answer doesn't deal with the fact that default style is not actually a color at all so applying it doesn't help.
This handles the default style and works if applying your own colors (which may be what edhubbell refers to as nasty results)
dgv.RowsDefaultCellStyle.SelectionBackColor = dgv.RowsDefaultCellStyle.BackColor.IsEmpty ? System.Drawing.Color.White : dgv.RowsDefaultCellStyle.BackColor;
dgv.RowsDefaultCellStyle.SelectionForeColor = dgv.RowsDefaultCellStyle.ForeColor.IsEmpty ? System.Drawing.Color.Black : dgv.RowsDefaultCellStyle.ForeColor;
Its in VB, but shouldnt be difficult to translate to C#:
If you want to lock datagridview, use dg.ReadOnly == True;
If you want to prevent user from selecting another row, just remember old selection and based on condition set or not set the row which shall be selected. Assuming, that multiselection is turned off:
Private Sub dg_SelectionChanged(sender As Object, e As EventArgs) Handles dg.SelectionChanged
Static OldSelection As Integer
If dg.Rows.Count > 0 Then
If dg.SelectedRows(0).Index <> OldSelection And YOURCONDITION Then
dg.Rows(OldSelection).Selected = True
End If
OldSelection = dg.SelectedRows(0).Index
End If
End Sub
Here's what has always worked for me to disable the default selection in a class inherited from DataGridView:
// REQUIRES: SelectionMode = DataGridViewSelectionMode.FullRowSelect
protected override void SetSelectedRowCore(int rowIndex, bool selected)
{
base.SetSelectedRowCore(rowIndex, selected && ALLOW_DEFAULT_SELECTION);
}
bool ALLOW_DEFAULT_SELECTION = false;
Usually the goal is to disable it entirely (in order to implement our own custom selection and drawing process). When the goal is to allow the default selection only at specific times the boolean can be wrapped like so:
public void SelectRowExplicitly(int index, bool selected = true)
{
try
{
ALLOW_DEFAULT_SELECTION = true;
Rows[index].Selected = selected;
}
finally
{
ALLOW_DEFAULT_SELECTION = false;
}
}
Use the DataGridView.ReadOnly property
The code in the MSDN example illustrates the use of this property in a DataGridView control intended primarily for display. In this example, the visual appearance of the control is customized in several ways and the control is configured for limited interactivity.
Observe these settings in the sample code:
// Set property values appropriate for read-only
// display and limited interactivity
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToOrderColumns = true;
dataGridView1.ReadOnly = true;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.MultiSelect = false;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dataGridView1.AllowUserToResizeColumns = false;
dataGridView1.ColumnHeadersHeightSizeMode =
DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
dataGridView1.AllowUserToResizeRows = false;
dataGridView1.RowHeadersWidthSizeMode =
DataGridViewRowHeadersWidthSizeMode.DisableResizing;

Datagridview FirstDisplayedScrollingRowIndex not working at the bottom of the grid

For scrolling the datagrid, I use following code:
dataGridView1.FirstDisplayedScrollingRowIndex = currentRowIndexInGridView;
dataGridView1.Update();
That works fine for rows not at the bottom of the grid.
If I use it for bottom rows, the setter doesnt set it to the value I wanted, when I inspect it during debugging.
E.g. I am setting FirstDisplayedScrollingRowIndex= 103, but after the assignment FirstDisplayedScrollingRowIndex has the value 90 and hence the desired row is not visible.
From a certain point it stops scrolling and I cant see the last 5 rows.
If I am adding new rows and setting them to be displayed, it scrolls by one, but I dont see the last 5 rows again.
I think it has something to do with the fact, the some of my rows have different height and some internal estiment of DisplayedRowCount fails ???
Is there a way of detecting this situation and then forcing scrolling to the bottom of the datagrid?
EDIT:
The important part of the FirstDisplayedScrollingRowIndex setter looks like this in the Reflector:
if (value > this.displayedBandsInfo.FirstDisplayedScrollingRow)
{
int rows = this.Rows.GetRowCount(DataGridViewElementStates.Visible, this.displayedBandsInfo.FirstDisplayedScrollingRow, value);
this.ScrollRowsByCount(rows, (rows > 1) ? ScrollEventType.LargeIncrement : ScrollEventType.SmallIncrement);
}
else
{
this.ScrollRowIntoView(-1, value, true, false);
}
There seems to be an error in computing the rows variable.
Call following method whenever a new row is added
private void Autoscroll()
{
if (dgv.FirstDisplayedScrollingRowIndex + dgv.DisplayedRowCount(false) < dgv.Rows.Count)
{
dgv.FirstDisplayedScrollingRowIndex += dgv.DisplayedRowCount(false);
}
else
{
dgv.FirstDisplayedScrollingRowIndex = dgv.Rows.Count - 1;
}
}
I had to force all the rows to have the same width, otherwise the
FirstDisplayedScrollingRowIndex
setter is buggy.
I have similar issue but I found how to solved.
The problem seams to be that DataGridView required windows form refresh and only after that can be set FirstDisplayedScrollingRowIndex to the new index.
What I did?
Timer refreshTimer = new Timer();
public void RefreshLog()
{
dataGridViewVesselLog.DataSource = Log.Items;
dataGridViewVesselLog.Update();
dataGridViewVesselLog.Refresh();
refreshTimer.Interval = 100;
refreshTimer.Tick += (s, e) =>
{
if (dataGridViewVesselLog.Rows.Count > 0)
{
foreach (DataGridViewRow r in dataGridViewVesselLog.SelectedRows)
r.Selected = false;
dataGridViewVesselLog.Rows[dataGridViewVesselLog.Rows.Count - 1].Selected = true;
dataGridViewVesselLog.FirstDisplayedScrollingRowIndex = (int)(dataGridViewVesselLog.Rows.Count - 1);
}
refreshTimer.Stop();
}
refreshTimer.Start();
}

Categories

Resources